public void ModificationCommand_initialized_correctly_for_added_entities_with_client_generated_key()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.Equal("T1", command.SchemaQualifiedName);
            Assert.Equal(EntityState.Added, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
示例#2
0
        public void ModificationCommand_initialized_correctly_for_added_entities_with_non_temp_generated_key()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, generateKeyValues: true);

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

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

            command.AddStateEntry(stateEntry);

            Assert.Equal("T1", command.SchemaQualifiedName);
            Assert.Equal(EntityState.Added, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
        public void ModificationCommand_initialized_correctly_for_added_entities_with_identity_key()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, ValueGenerationOnSave.WhenInserting);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

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

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.True(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.True(columnMod.IsRead);
            Assert.False(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
        public void ModificationCommand_initialized_correctly_for_deleted_entities_with_concurrency_token()
        {
            var stateEntry = CreateStateEntry(EntityState.Deleted, nonKeyStrategy: ValueGenerationOnSave.WhenInsertingAndUpdating);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.Equal("T1", command.SchemaQualifiedName);
            Assert.Equal(EntityState.Deleted, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.True(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.False(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.True(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.False(columnMod.IsWrite);
        }
        public void ModificationCommand_initialized_correctly_for_modified_entities_with_identity_key()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, ValueGenerationOnSave.WhenInserting);

            stateEntry.SetPropertyModified(stateEntry.EntityType.GetPrimaryKey().Properties[0], isModified: false);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.Equal("T1", command.SchemaQualifiedName);
            Assert.Equal(EntityState.Modified, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.True(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.False(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(stateEntry, columnMod.StateEntry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
示例#6
0
        public void RequiresResultPropagation_false_for_Insert_operation_if_no_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);

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

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
        public void ModificationCommand_throws_for_unknown_entities()
        {
            var stateEntry = CreateStateEntry(EntityState.Unknown);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            Assert.Equal(
                Strings.FormatModificationFunctionInvalidEntityState(EntityState.Unknown),
                Assert.Throws <NotSupportedException>(() => command.AddStateEntry(stateEntry)).Message);
        }
示例#8
0
        public void RequiresResultPropagation_false_for_Update_operation_if_no_non_key_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, ValueGenerationStrategy.StoreIdentity);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
        public void RequiresResultPropagation_false_for_Update_operation_if_no_non_key_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, ValueGenerationOnSave.WhenInserting);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
示例#10
0
        public void RequiresResultPropagation_false_for_Delete_operation()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Deleted, generateKeyValues: true, computeNonKeyValue: true);

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

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
        public void RequiresResultPropagation_true_for_Insert_operation_if_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Added, ValueGenerationOnSave.WhenInserting, ValueGenerationOnSave.WhenInsertingAndUpdating);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.True(command.RequiresResultPropagation);
        }
示例#12
0
        public void RequiresResultPropagation_true_for_Insert_operation_if_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Added, ValueGenerationStrategy.StoreIdentity, ValueGenerationStrategy.StoreComputed);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.True(command.RequiresResultPropagation);
        }
示例#13
0
        public void RequiresResultPropagation_false_for_Delete_operation()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Deleted, ValueGenerationStrategy.StoreIdentity, ValueGenerationStrategy.StoreComputed);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
示例#14
0
        public void RequiresResultPropagation_true_for_Update_operation_if_non_key_store_generated_columns_exist()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

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

            command.AddStateEntry(stateEntry);

            Assert.True(command.RequiresResultPropagation);
        }
        public void RequiresResultPropagation_false_for_Delete_operation()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Deleted, ValueGenerationOnSave.WhenInserting, ValueGenerationOnSave.WhenInsertingAndUpdating);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            Assert.False(command.RequiresResultPropagation);
        }
        public void Batch_separator_not_appended_if_batch_header_empty()
        {
            var stateEntry = CreateStateEntry(EntityState.Deleted);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch = new ModificationCommandBatch(new[] { command });

            Assert.Equal(
                "DELETE FROM [T1] WHERE [Col1] = @p0$" + Environment.NewLine,
                batch.CompileBatch(new ConcreteSqlGenerator(useBatchHeader: false)));
        }
        public void CompileBatch_compiles_deletes()
        {
            var stateEntry = CreateStateEntry(EntityState.Deleted);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch = new ModificationCommandBatch(new[] { command });

            Assert.Equal(
                "BatchHeader$" + Environment.NewLine +
                "DELETE FROM [T1] WHERE [Col1] = @p0$" + Environment.NewLine,
                batch.CompileBatch(new ConcreteSqlGenerator()));
        }
        public void CompileBatch_compiles_inserts()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch = new ModificationCommandBatch(new[] { command });

            Assert.Equal(
                "BatchHeader$" + Environment.NewLine +
                "INSERT INTO [T1] ([Col1], [Col2]) VALUES (@p0, @p1)$" + Environment.NewLine,
                batch.CompileBatch(new ConcreteSqlGenerator()));
        }
        public void CompileBatch_compiles_updates()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, ValueGenerationOnSave.WhenInserting);

            var command = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch = new ModificationCommandBatch(new[] { command });

            Assert.Equal(
                "BatchHeader$" + Environment.NewLine +
                "UPDATE [T1] SET [Col2] = @p1 WHERE [Col1] = @p0$" + Environment.NewLine,
                batch.CompileBatch(new ConcreteSqlGenerator()));
        }
        public void GenerateCommandText_compiles_deletes()
        {
            var stateEntry = CreateStateEntry(EntityState.Deleted);

            var command = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var sqlGeneratorMock = new Mock <SqlGenerator>();
            var batch            = new ModificationCommandBatchFake();

            batch.GenerateCommandTextBase(new[] { command }, sqlGeneratorMock.Object);

            sqlGeneratorMock.Verify(g => g.AppendBatchHeader(It.IsAny <StringBuilder>()));
            sqlGeneratorMock.Verify(g => g.AppendDeleteOperation(It.IsAny <StringBuilder>(), "T1", It.IsAny <IReadOnlyList <ColumnModification> >()));
        }
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);
            var command    = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator(), p => p.Relational());

            command.AddStateEntry(stateEntry);

            var mockReader = CreateDataReaderMock();
            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);

            mockReader.Verify(r => r.ReadAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockReader.Verify(r => r.GetFieldValue <int>(0), Times.Once);
        }
示例#22
0
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, ValueGenerationOnSave.WhenInserting);
            var command    = new ModificationCommand("T1", null, new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

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

            batch.AddCommand(command, new Mock <SqlGenerator> {
                CallBase = true
            }.Object);

            Assert.Equal(Strings.FormatUpdateConcurrencyException(1, 0),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await batch.ExecuteAsync(new Mock <RelationalTransaction>().Object, new RelationalTypeMapper()))).Message);
        }
        public void UpdateCommandText_compiles_updates()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, generateKeyValues: true);

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

            command.AddStateEntry(stateEntry);

            var sqlGeneratorMock = new Mock <SqlGenerator>();
            var batch            = new ModificationCommandBatchFake(sqlGeneratorMock.Object);

            batch.AddCommand(command);

            batch.UpdateCachedCommandTextBase(0);

            sqlGeneratorMock.Verify(g => g.AppendBatchHeader(It.IsAny <StringBuilder>()));
            sqlGeneratorMock.Verify(g => g.AppendUpdateOperation(It.IsAny <StringBuilder>(), command));
        }
示例#24
0
        public void GenerateCommandText_compiles_updates()
        {
            var stateEntry = CreateStateEntry(EntityState.Modified, ValueGenerationOnSave.WhenInserting);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var sqlGeneratorMock = new Mock <SqlGenerator>();
            var batch            = new ModificationCommandBatchFake();

            batch.AddCommand(command, sqlGeneratorMock.Object);

            batch.GenerateCommandTextBase(sqlGeneratorMock.Object);

            sqlGeneratorMock.Verify(g => g.AppendBatchHeader(It.IsAny <StringBuilder>()));
            sqlGeneratorMock.Verify(g => g.AppendUpdateOperation(It.IsAny <StringBuilder>(), "T1", It.IsAny <IReadOnlyList <ColumnModification> >()));
        }
        public async Task ExecuteAsync_saves_store_generated_values()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, ValueGenerationOnSave.WhenInserting);
            var command    = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            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, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
            Assert.Equal("Test", stateEntry[stateEntry.EntityType.GetProperty("Name")]);
        }
        public async void Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, ValueGenerationOnSave.WhenInserting);
            var command    = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch      = new ModificationCommandBatch(new[] { command });
            var mockReader = SetupMockDataReader(new[] { "Col1" });
            var connection = SetupMockConnection(mockReader.Object);

            var executor = new BatchExecutor(new Mock <SqlGenerator> {
                CallBase = true
            }.Object, connection, new RelationalTypeMapper());

            Assert.Equal(Strings.FormatUpdateConcurrencyException(1, 0),
                         (await Assert.ThrowsAsync <DbUpdateConcurrencyException>(
                              async() => await executor.ExecuteAsync(new[] { batch }))).Message);
        }
示例#27
0
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);
            var command    = new ModificationCommand("T1", null, new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

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

            batch.AddCommand(command, new Mock <SqlGenerator> {
                CallBase = true
            }.Object);

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

            mockReader.Verify(r => r.ReadAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
            mockReader.Verify(r => r.NextResultAsync(It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

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

            command.AddStateEntry(stateEntry);

            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, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", stateEntry[stateEntry.EntityType.GetProperty("Name")]);
        }
        public async void ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var stateEntry = CreateStateEntry(EntityState.Added);
            var command    = new ModificationCommand("T1", new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            var batch      = new ModificationCommandBatch(new[] { command });
            var mockReader = SetupMockDataReader();
            var connection = SetupMockConnection(mockReader.Object);

            var executor = new BatchExecutor(new Mock <SqlGenerator> {
                CallBase = true
            }.Object, connection, new RelationalTypeMapper());

            await executor.ExecuteAsync(new[] { batch });

            mockReader.Verify(r => r.ReadAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockReader.Verify(r => r.NextResultAsync(It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task Exception_not_thrown_for_more_than_one_row_returned_for_single_command()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, ValueGenerationOnSave.WhenInserting);
            var command    = new ModificationCommand(new SchemaQualifiedName("T1"), new ParameterNameGenerator());

            command.AddStateEntry(stateEntry);

            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, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
        }