public void WritePrimaryKeyOperations_Test()
        {
            // Arrange

            var primaryKeyOperations = new[]
            {
                new Mock<InsertRecord>(this.insertRecordService, null, this.peers, null),
                new Mock<InsertRecord>(this.insertRecordService, null, this.peers, null),
            };

            // Act

            var breaker = new CircularReferenceBreaker();
            var currentOrder = new Counter();
            var orderedOperations = new AbstractRepositoryOperation[0];

            this.insertRecordService.WritePrimaryKeyOperations(this.writerMock.Object,
                primaryKeyOperations.Select(m => m.Object), breaker, currentOrder, orderedOperations);

            // Assert

            primaryKeyOperations.ToList()
                .ForEach(
                    o =>
                        o.Verify(m => m.Write(breaker, this.writerMock.Object, currentOrder, orderedOperations),
                            Times.Once));
        }
        public void Write_Test()
        {
            // Arrange

            var orderedOperations = new AbstractRepositoryOperation[1];
            var primaryKeyOperations = new List<InsertRecord>();
            var currentOrder = new Counter();
            var regularColumns = new List<Column>();
            var foreignKeyColumns = new List<ExtendedColumnSymbol>();
            var columnList = regularColumns.Concat(Helpers.ColumnSymbolToColumn(foreignKeyColumns));
            string tableName = typeof (SubjectClass).Name;

            this.serviceMock.Setup(m => m.GetPrimaryKeyOperations(this.peers)).Returns(primaryKeyOperations);
            this.serviceMock.Setup(m => m.GetRegularColumns(this.writePrimitivesMock.Object)).Returns(regularColumns);
            this.serviceMock.Setup(m => m.GetForeignKeyColumns(primaryKeyOperations)).Returns(foreignKeyColumns);

            // Act

            this.insertRecord.Write(this.breakerMock.Object, this.writePrimitivesMock.Object, currentOrder, orderedOperations);

            // Assert

            this.breakerMock.Verify(m => m.Push<IWritePrimitives, Counter, AbstractRepositoryOperation[]>(this.insertRecord.Write), Times.Once);
            this.serviceMock.Verify(m => m.GetPrimaryKeyOperations(this.peers), Times.Once);
            this.serviceMock.Verify(
                m =>
                    m.WritePrimaryKeyOperations(this.writePrimitivesMock.Object, primaryKeyOperations,
                        this.breakerMock.Object, currentOrder, orderedOperations), Times.Once);

            this.serviceMock.Verify(m => m.GetRegularColumns(this.writePrimitivesMock.Object), Times.Once);
            this.serviceMock.Verify(m => m.GetForeignKeyColumns(primaryKeyOperations), Times.Once);

            Assert.AreEqual(this.insertRecord, orderedOperations[0]);

            this.serviceMock.Verify(
                m =>
                    m.WritePrimitives(this.writePrimitivesMock.Object, null, It.IsAny<string>(), tableName, columnList,
                        It.Is<List<ColumnSymbol>>(l => l.Count == 0)), Times.Once);

            this.serviceMock.Verify(m => m.CopyPrimaryToForeignKeyColumns(It.Is<IEnumerable<Column>>(c => !c.Any())), Times.Once());
        }
        public void WriteOperationOrder_Test()
        {
            // Arrange

            var orderedOpertations = new AbstractRepositoryOperation[2];
            var secondObject = new SubjectClass();
            var secondrecordReferenceMock = new Mock<RecordReference>(null, this.attributeDecorator);
            secondrecordReferenceMock.Setup(m => m.RecordObject).Returns(secondObject);
            secondrecordReferenceMock.Setup(m => m.RecordType).Returns(secondObject.GetType());

            var secondInsertRecord = new InsertRecord(this.serviceMock.Object, secondrecordReferenceMock.Object, this.peers, this.attributeDecorator);
            var primaryKeyOperations = new List<InsertRecord> { secondInsertRecord };

            var regularColumns = new List<Column>();
            var foreignKeyColumns = new List<ExtendedColumnSymbol>();

            this.serviceMock.Setup(m => m.GetPrimaryKeyOperations(this.peers)).Returns(primaryKeyOperations);
            this.serviceMock.Setup(m => m.GetRegularColumns(this.writePrimitivesMock.Object)).Returns(regularColumns);
            this.serviceMock.Setup(m => m.GetForeignKeyColumns(primaryKeyOperations)).Returns(foreignKeyColumns);

            this.serviceMock.Setup(
                m =>
                    m.WritePrimaryKeyOperations(It.IsAny<IWritePrimitives>(), It.IsAny<IEnumerable<AbstractRepositoryOperation>>(),
                        It.IsAny<CircularReferenceBreaker>(), It.IsAny<Counter>(),
                        It.IsAny<AbstractRepositoryOperation[]>()))
                .Callback
                <IWritePrimitives, IEnumerable<AbstractRepositoryOperation>, CircularReferenceBreaker, Counter,
                    AbstractRepositoryOperation[]>(
                        (writer, secondPrimaryKeyOperations, breaker, secondCurrentOrder, secondOrderedOperations) =>
                        {
                            this.serviceMock.Setup(
                                n =>
                                    n.WritePrimaryKeyOperations(It.IsAny<IWritePrimitives>(),
                                        It.IsAny<IEnumerable<InsertRecord>>(),
                                        It.IsAny<CircularReferenceBreaker>(), It.IsAny<Counter>(),
                                        It.IsAny<AbstractRepositoryOperation[]>()));

                            secondInsertRecord.Write(breaker, writer, secondCurrentOrder, secondOrderedOperations);
                        });

            // Act

            this.insertRecord.Write(this.breakerMock.Object, this.writePrimitivesMock.Object, new Counter(), orderedOpertations);

            // Assert

            Assert.AreEqual(secondInsertRecord, orderedOpertations[0]);
            Assert.AreEqual(this.insertRecord, orderedOpertations[1]);
        }
        public void WriteIsDone_FilterPasses_Test()
        {
            // Arrange

            var orderedOpertations = new AbstractRepositoryOperation[1];

            // Act

            this.insertRecord.Write(this.breakerMock.Object, this.writePrimitivesMock.Object, new Counter(), orderedOpertations);

            // Assert

            this.breakerMock.Verify(m => m.Push<IWritePrimitives, Counter, AbstractRepositoryOperation[]>(this.insertRecord.Write), Times.Once);
            Assert.AreEqual(this.insertRecord, orderedOpertations[0]);
        }
        public virtual void Persist(IEnumerable<RecordReference> recordReferences)
        {
            SqlClientPersistence.Logger.Debug("Entering Persist");

            recordReferences = recordReferences.ToList();

            if (!recordReferences.Any())
            {
                SqlClientPersistence.Logger.Debug("Empty recordReference collection. Exiting.");
                return;
            }

            SqlClientPersistence.Logger.Debug(
                $"Records: {string.Join(", ", recordReferences.Select(r => r?.RecordObject))}");

            this.deferredValueGenerator.Execute(recordReferences);

            var operations = new List<AbstractRepositoryOperation>();

            foreach (RecordReference recordReference in recordReferences)
            {
                operations.Add(
                    new InsertRecord(
                        new InsertRecordService(recordReference, this.attributeDecorator, this.enforceKeyReferenceCheck),
                        recordReference, operations, this.attributeDecorator));
            }

            var orderedOperations = new AbstractRepositoryOperation[operations.Count];

            var currentOrder = new Counter();

            foreach (AbstractRepositoryOperation operation in operations)
            {
                operation.Write(new CircularReferenceBreaker(), this.writePrimitives, currentOrder,
                    orderedOperations);
            }

            var readStreamPointer = new Counter();

            object[] returnValues = this.writePrimitives.Execute();

            foreach (AbstractRepositoryOperation orderedOperation in orderedOperations)
            {
                orderedOperation.Read(readStreamPointer, returnValues);
            }

            SqlClientPersistence.Logger.Debug("Exiting Persist");
        }
 public abstract void Write(CircularReferenceBreaker breaker, IWritePrimitives writer, Counter order, AbstractRepositoryOperation[] orderedOperations);