public void UpdateCommandText_compiles_inserts()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var sqlGeneratorMock = new Mock<ISqlGenerator>();
            var batch = new ModificationCommandBatchFake(sqlGeneratorMock.Object);
            batch.AddCommand(command);

            batch.UpdateCachedCommandTextBase(0);

            sqlGeneratorMock.Verify(g => g.AppendBatchHeader(It.IsAny<StringBuilder>()));
            sqlGeneratorMock.Verify(g => g.AppendInsertOperation(It.IsAny<StringBuilder>(), command));
        }
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var entry = CreateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());

            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col2" }, new List <object[]> {
                new object[] { "FortyTwo" }
            }).Object);

            batch.AddCommand(command);

            await batch.ExecuteAsync(new Mock <RelationalTransaction>().Object, new RelationalTypeMapper(), new Mock <DbContext>().Object, new Mock <ILogger>().Object);

            Assert.Equal(1, entry[entry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.GetProperty("Name")]);
        }
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var entry   = CreateEntry(EntityState.Added);
            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());

            command.AddEntry(entry);

            var mockReader = CreateDataReaderMock();
            var batch      = new ModificationCommandBatchFake(mockReader.Object);

            batch.AddCommand(command);

            var transactionMock = new Mock <RelationalTransaction>(
                Mock.Of <IRelationalConnection>(), Mock.Of <DbTransaction>(), false, Mock.Of <ILogger>());

            await batch.ExecuteAsync(transactionMock.Object, new RelationalTypeMapper(), new Mock <DbContext>().Object, new Mock <ILogger>().Object);

            mockReader.Verify(r => r.ReadAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockReader.Verify(r => r.GetInt32(0), Times.Once);
        }
Esempio n. 4
0
        private void Compare_returns_0_only_for_entries_that_have_same_key_values_generic <T>(T value1, T value2)
        {
            var model      = new Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder().UseModel(model).UseInMemoryDatabase();

            var contextServices = new DbContext(optionsBuilder.Options).GetInfrastructure();
            var stateManager    = contextServices.GetRequiredService <IStateManager>();

            var keyProperty = entityType.AddProperty("Id", typeof(T));

            keyProperty.IsNullable = false;
            entityType.GetOrSetPrimaryKey(keyProperty);

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[keyProperty] = value1;
            entry1.SetEntityState(EntityState.Modified);
            var modificationCommand1 = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommand1.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[keyProperty] = value2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommand2 = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommand2.AddEntry(entry2);

            var modificationCommand3 = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommand3.AddEntry(entry1);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 > mCC.Compare(modificationCommand1, modificationCommand2));
            Assert.True(0 < mCC.Compare(modificationCommand2, modificationCommand1));
            Assert.True(0 == mCC.Compare(modificationCommand1, modificationCommand3));
        }
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            command.AddEntry(entry);

            var dbDataReader = CreateFakeDataReader();

            var connection = CreateConnection(dbDataReader);

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, dbDataReader.ReadAsyncCount);
            Assert.Equal(1, dbDataReader.GetInt32Count);
        }
        public async Task Exception_thrown_if_rows_returned_for_command_without_store_generated_values_is_not_1()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col1" }, new List <object[]> {
                new object[] { 42 }
            }));

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 42),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await batch.ExecuteAsync(connection))).Message);
        }
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col1" }, new List <object[]>()));

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 0),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await batch.ExecuteAsync(connection))).Message);
        }
        public async Task ExecuteAsync_saves_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());

            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1" }, new List <object[]> {
                new object[] { 42 }
            }).Object);

            batch.AddCommand(command);

            await batch.ExecuteAsync(new Mock <RelationalTransaction>().Object, new RelationalTypeMapper(), new Mock <DbContext>().Object, new Mock <ILogger>().Object);

            Assert.Equal(42, entry[entry.EntityType.GetProperty("Id")]);
            Assert.Equal("Test", entry[entry.EntityType.GetProperty("Name")]);
        }
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());

            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1" }, new List <object[]>()).Object);

            batch.AddCommand(command);

            Assert.Equal(Strings.UpdateConcurrencyException(1, 0),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await batch.ExecuteAsync(
                                  new Mock <RelationalTransaction>().Object,
                                  new RelationalTypeMapper(),
                                  new Mock <DbContext>().Object,
                                  new Mock <ILogger>().Object))).Message);
        }
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var entry = CreateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col2" }, new List <object[]> {
                new object[] { "FortyTwo" }
            }));

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("Name")]);
        }
        public void ModificationCommand_initialized_correctly_for_deleted_entities()
        {
            var entry = CreateEntry(EntityState.Deleted);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.TestProvider());

            command.AddEntry(entry);

            Assert.Equal("T1", command.TableName);
            Assert.Null(command.Schema);
            Assert.Equal(EntityState.Deleted, command.EntityState);
            Assert.Equal(1, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.True(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.False(columnMod.IsWrite);
        }
        public async Task Exception_not_thrown_for_more_than_one_row_returned_for_single_command()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());

            command.AddEntry(entry);

            var mockReader = CreateDataReaderMock(new[] { "Col1" }, new List <object[]>
            {
                new object[] { 42 },
                new object[] { 43 }
            });
            var batch = new ModificationCommandBatchFake(mockReader.Object);

            batch.AddCommand(command);

            await batch.ExecuteAsync(new Mock <RelationalTransaction>().Object, new RelationalTypeMapper(), new Mock <DbContext>().Object, new Mock <ILogger>().Object);

            Assert.Equal(42, entry[entry.EntityType.GetProperty("Id")]);
        }
Esempio n. 13
0
        public void ModificationCommand_initialized_correctly_for_added_entities_with_non_temp_generated_key()
        {
            var entry = Createentry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0], isTemporary: false);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource());

            command.AddEntry(entry);

            Assert.Equal("T1", command.TableName);
            Assert.Null(command.SchemaName);
            Assert.Equal(EntityState.Added, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
            Assert.Null(columnMod.BoxedValueReader);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
            Assert.Null(columnMod.BoxedValueReader);
        }
Esempio n. 14
0
        public async Task ExecuteAsync_saves_store_generated_values_on_non_key_columns()
        {
            var entry = CreateEntry(
                EntityState.Added, generateKeyValues: true, computeNonKeyValue: true);

            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.TestProvider());

            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1", "Col2" }, new List <object[]> {
                new object[] { 42, "FortyTwo" }
            }).Object);

            batch.AddCommand(command);

            var connection = Mock.Of <IRelationalConnection>();

            await batch.ExecuteAsync(connection, new Mock <ILogger>().Object);

            Assert.Equal(42, entry[entry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.GetProperty("Name")]);
        }
        public async Task ExecuteAsync_saves_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col1" }, new List <object[]> {
                new object[] { 42 }
            }));

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("Test", entry[entry.EntityType.FindProperty("Name")]);
        }
Esempio n. 16
0
        public async Task Exception_thrown_if_rows_returned_for_command_without_store_generated_values_is_not_1()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());

            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1" }, new List <object[]> {
                new object[] { 42 }
            }).Object);

            batch.AddCommand(command);

            var transaction = Mock.Of <IRelationalTransaction>();

            Assert.Equal(Strings.UpdateConcurrencyException(1, 42),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await batch.ExecuteAsync(
                                  transaction,
                                  new ConcreteTypeMapper(),
                                  new Mock <DbContext>().Object,
                                  new Mock <ILogger>().Object))).Message);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected virtual IEnumerable <ModificationCommand> CreateModificationCommands(
            [NotNull] IReadOnlyList <IUpdateEntry> entries,
            [NotNull] Func <string> generateParameterName)
        {
            // TODO: Handle multiple state entries that update the same row

            Func <IProperty, IRelationalPropertyAnnotations> getAnnotationsDelegate = _annotationProvider.For;

            foreach (var entry in entries)
            {
                var command = new ModificationCommand(
                    _annotationProvider.For(entry.EntityType).TableName,
                    _annotationProvider.For(entry.EntityType).Schema,
                    generateParameterName,
                    getAnnotationsDelegate);

                command.AddEntry(entry);
                if (command.EntityState != EntityState.Modified ||
                    command.ColumnModifications.Any(m => m.IsWrite))
                {
                    yield return(command);
                }
            }
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used 
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected virtual IEnumerable<ModificationCommand> CreateModificationCommands(
            [NotNull] IReadOnlyList<IUpdateEntry> entries,
            [NotNull] Func<string> generateParameterName)
        {
            // TODO: Handle multiple state entries that update the same row

            Func<IProperty, IRelationalPropertyAnnotations> getAnnotationsDelegate = _annotationProvider.For;
            foreach (var entry in entries)
            {
                var command = new ModificationCommand(
                    _annotationProvider.For(entry.EntityType).TableName,
                    _annotationProvider.For(entry.EntityType).Schema,
                    generateParameterName,
                    getAnnotationsDelegate);

                command.AddEntry(entry);
                if (command.EntityState != EntityState.Modified
                    || command.ColumnModifications.Any(m => m.IsWrite))
                {
                    yield return command;
                }
            }
        }
        public void UpdateCommandText_compiles_multiple_commands()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var fakeSqlGenerator = new FakeSqlGenerator();
            var batch = new ModificationCommandBatchFake(fakeSqlGenerator);
            batch.AddCommand(command);
            batch.AddCommand(command);

            Assert.Equal("..", batch.CommandText);

            Assert.Equal(1, fakeSqlGenerator.AppendBatchHeaderCalls);
        }
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var entry = CreateEntry(EntityState.Added);
            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var mockReader = CreateDataReaderMock();
            var batch = new ModificationCommandBatchFake(mockReader.Object);
            batch.AddCommand(command);

            var transactionMock = new Mock<RelationalTransaction>(
                Mock.Of<IRelationalConnection>(), Mock.Of<DbTransaction>(), false, Mock.Of<ILogger>());

            await batch.ExecuteAsync(transactionMock.Object, new RelationalTypeMapper(), new Mock<DbContext>().Object, new Mock<ILogger>().Object);

            mockReader.Verify(r => r.ReadAsync(It.IsAny<CancellationToken>()), Times.Once);
            mockReader.Verify(r => r.GetInt32(0), Times.Once);
        }
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var entry = CreateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col2" }, new List<object[]> { new object[] { "FortyTwo" } }).Object);
            batch.AddCommand(command);

            var transactionMock = new Mock<RelationalTransaction>(
                Mock.Of<IRelationalConnection>(), Mock.Of<DbTransaction>(), false, Mock.Of<ILogger>());

            await batch.ExecuteAsync(transactionMock.Object, new RelationalTypeMapper(), new Mock<DbContext>().Object, new Mock<ILogger>().Object);

            Assert.Equal(1, entry[entry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.GetProperty("Name")]);
        }
        public async Task Exception_not_thrown_for_more_than_one_row_returned_for_single_command()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);
            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var mockReader = CreateDataReaderMock(new[] { "Col1" }, new List<object[]>
                {
                    new object[] { 42 },
                    new object[] { 43 }
                });
            var batch = new ModificationCommandBatchFake(mockReader.Object);
            batch.AddCommand(command);

            var transactionMock = new Mock<RelationalTransaction>(
                Mock.Of<IRelationalConnection>(), Mock.Of<DbTransaction>(), false, Mock.Of<ILogger>());

            await batch.ExecuteAsync(transactionMock.Object, new RelationalTypeMapper(), new Mock<DbContext>().Object, new Mock<ILogger>().Object);

            Assert.Equal(42, entry[entry.EntityType.GetProperty("Id")]);
        }
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);
            entry.MarkAsTemporary(entry.EntityType.GetPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1" }, new List<object[]>()).Object);
            batch.AddCommand(command);

            var transactionMock = new Mock<RelationalTransaction>(
                Mock.Of<IRelationalConnection>(), Mock.Of<DbTransaction>(), false, Mock.Of<ILogger>());

            Assert.Equal(Strings.UpdateConcurrencyException(1, 0),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(
                        transactionMock.Object,
                        new RelationalTypeMapper(),
                        new Mock<DbContext>().Object,
                        new Mock<ILogger>().Object))).Message);
        }
Esempio n. 24
0
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model      = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new EntityOptionsBuilder()
                                 .UseModel(model);

            optionsBuilder.UseInMemoryStore(persist: false);

            var contextServices = ((IAccessor <IServiceProvider>) new DbContext(optionsBuilder.Options)).Service;
            var stateManager    = contextServices.GetRequiredService <IStateManager>();

            var key = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);

            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());

            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()), null));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
Esempio n. 25
0
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model      = new Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder()
                                 .UseModel(model);

            optionsBuilder.UseInMemoryDatabase();

            var contextServices = new DbContext(optionsBuilder.Options).GetInfrastructure();
            var stateManager    = contextServices.GetRequiredService <IStateManager>();

            var key = entityType.AddProperty("Id", typeof(int));

            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());

            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()), null));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                            new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }