public void CreateForSingleIDLookup()
        {
            var expectedSelectedColumns = _tableDefinition1.GetAllColumns().ToArray();
            var expectedComparedColumns =
                new[] { new ColumnValue(StoragePropertyDefinitionTestHelper.GetIDColumnDefinition(_tableDefinition1.ObjectIDProperty), _objectID1.Value) };

            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub => stub.CreateForSelect(
                    Arg.Is(_tableDefinition1),
                    Arg <IEnumerable <ColumnDefinition> > .List.Equal(expectedSelectedColumns),
                    Arg <IEnumerable <ColumnValue> > .List.Equal(expectedComparedColumns),
                    Arg <IEnumerable <OrderedColumn> > .List.Equal(new OrderedColumn[0])))
            .Return(_dbCommandBuilder1Stub);

            _objectReaderFactoryStrictMock
            .Expect(
                mock => mock.CreateDataContainerReader(
                    Arg.Is((IRdbmsStorageEntityDefinition)_tableDefinition1),
                    Arg <IEnumerable <ColumnDefinition> > .List.Equal(expectedSelectedColumns)))
            .Return(_dataContainerReader1Stub);
            _objectReaderFactoryStrictMock.Replay();

            var result = _factory.CreateForSingleIDLookup(_objectID1);

            _objectReaderFactoryStrictMock.VerifyAllExpectations();

            Assert.That(result, Is.TypeOf <SingleDataContainerAssociateWithIDCommand <IRdbmsProviderCommandExecutionContext> >());

            var associateCommand = (SingleDataContainerAssociateWithIDCommand <IRdbmsProviderCommandExecutionContext>)result;

            Assert.That(associateCommand.ExpectedObjectID, Is.EqualTo(_objectID1));
            Assert.That(associateCommand.InnerCommand, Is.TypeOf(typeof(SingleObjectLoadCommand <DataContainer>)));

            var loadCommand = ((SingleObjectLoadCommand <DataContainer>)associateCommand.InnerCommand);

            Assert.That(loadCommand.DbCommandBuilder, Is.SameAs(_dbCommandBuilder1Stub));
            Assert.That(loadCommand.ObjectReader, Is.SameAs(_dataContainerReader1Stub));
        }
        public void CreateForSave_New()
        {
            var dataContainerNew1 = DataContainer.CreateNew(DomainObjectIDs.Computer1);

            SetPropertyValue(dataContainerNew1, typeof(Computer), "SerialNumber", "123456");
            var dataContainerNew2 = DataContainer.CreateNew(DomainObjectIDs.Computer2);

            SetPropertyValue(dataContainerNew2, typeof(Computer), "SerialNumber", "654321");

            var dataContainerNewWithoutRelations = DataContainer.CreateNew(DomainObjectIDs.Official1);

            var insertDbCommandBuilderNew1 = MockRepository.GenerateStub <IDbCommandBuilder>();
            var insertDbCommandBuilderNew2 = MockRepository.GenerateStub <IDbCommandBuilder>();
            var insertDbCommandBuilderNew3 = MockRepository.GenerateStub <IDbCommandBuilder>();
            var updateDbCommandBuilderNew1 = MockRepository.GenerateStub <IDbCommandBuilder>();
            var updateDbCommandBuilderNew2 = MockRepository.GenerateStub <IDbCommandBuilder>();

            var tableDefinition1 = (TableDefinition)dataContainerNew1.ID.ClassDefinition.StorageEntityDefinition;
            var tableDefinition2 = (TableDefinition)dataContainerNew2.ID.ClassDefinition.StorageEntityDefinition;
            var tableDefinition3 = (TableDefinition)dataContainerNewWithoutRelations.ID.ClassDefinition.StorageEntityDefinition;

            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub =>
                stub.CreateForInsert(
                    Arg.Is(tableDefinition1),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckInsertedComputerColumns(c, dataContainerNew1))))
            .Return(insertDbCommandBuilderNew1).Repeat.Once();
            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub =>
                stub.CreateForInsert(
                    Arg.Is(tableDefinition2),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckInsertedComputerColumns(c, dataContainerNew2))))
            .Return(insertDbCommandBuilderNew2).Repeat.Once();
            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub =>
                stub.CreateForInsert(
                    Arg.Is(tableDefinition3),
                    Arg <IEnumerable <ColumnValue> > .Is.Anything))
            .Return(insertDbCommandBuilderNew3).Repeat.Once();
            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub => stub.CreateForUpdate(
                    Arg.Is(tableDefinition1),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckUpdatedComputerColumns(c, dataContainerNew1, true, false, false)),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckComparedColumns(c, dataContainerNew1, tableDefinition1))))
            .Return(updateDbCommandBuilderNew1).Repeat.Once();
            _dbCommandBuilderFactoryStrictMock
            .Stub(
                stub => stub.CreateForUpdate(
                    Arg.Is(tableDefinition2),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckUpdatedComputerColumns(c, dataContainerNew2, true, false, false)),
                    Arg <IEnumerable <ColumnValue> > .Matches(c => CheckComparedColumns(c, dataContainerNew2, tableDefinition2))))
            .Return(updateDbCommandBuilderNew2).Repeat.Once();

            StubTableDefinitionFinder(dataContainerNew1.ID, tableDefinition1);
            StubTableDefinitionFinder(dataContainerNew2.ID, tableDefinition2);
            StubTableDefinitionFinder(dataContainerNewWithoutRelations.ID, tableDefinition3);
            _tableDefinitionFinderStrictMock.Replay();

            var result = _factory.CreateForSave(
                new[]
            {
                dataContainerNew1,
                dataContainerNew2,
                dataContainerNewWithoutRelations
            });

            _tableDefinitionFinderStrictMock.VerifyAllExpectations();
            Assert.That(result, Is.TypeOf(typeof(MultiDataContainerSaveCommand)));
            var tuples = ((MultiDataContainerSaveCommand)result).Tuples.ToList();

            Assert.That(tuples.Count, Is.EqualTo(5));
            Assert.That(tuples[0].Item1, Is.EqualTo(dataContainerNew1.ID));
            Assert.That(tuples[0].Item2, Is.SameAs(insertDbCommandBuilderNew1));
            Assert.That(tuples[1].Item1, Is.EqualTo(dataContainerNew2.ID));
            Assert.That(tuples[1].Item2, Is.SameAs(insertDbCommandBuilderNew2));
            Assert.That(tuples[2].Item1, Is.EqualTo(dataContainerNewWithoutRelations.ID));
            Assert.That(tuples[2].Item2, Is.SameAs(insertDbCommandBuilderNew3));
            Assert.That(tuples[3].Item1, Is.EqualTo(dataContainerNew1.ID));
            Assert.That(tuples[3].Item2, Is.SameAs(updateDbCommandBuilderNew1));
            Assert.That(tuples[4].Item1, Is.EqualTo(dataContainerNew2.ID));
            Assert.That(tuples[4].Item2, Is.SameAs(updateDbCommandBuilderNew2));
        }