Beispiel #1
0
    public async Task Exception_not_thrown_for_more_than_one_row_returned_for_single_command()
    {
        var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

        entry.SetTemporaryValue(entry.EntityType.FindPrimaryKey().Properties[0], -1);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

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

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        await batch.ExecuteAsync(connection);

        Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
    }
Beispiel #2
0
    public void PopulateParameters_does_not_create_parameter_for_read_ModificationCommand()
    {
        var entry    = CreateEntry(EntityState.Added, generateKeyValues: true);
        var property = entry.EntityType.FindProperty("Id");

        entry.SetTemporaryValue(property, -1);

        var batch = new ModificationCommandBatchFake();
        var parameterNameGenerator = new ParameterNameGenerator();

        batch.TryAddCommand(
            CreateModificationCommand(
                "T1",
                null,
                true,
                new[]
        {
            new ColumnModificationParameters(
                entry,
                property,
                property.GetTableColumnMappings().Single().Column,
                parameterNameGenerator.GenerateNext,
                property.GetTableColumnMappings().Single().TypeMapping,
                valueIsRead: true, valueIsWrite: false, columnIsKey: false, columnIsCondition: false,
                sensitiveLoggingEnabled: true)
        }));

        batch.Complete();

        var storeCommand = batch.StoreCommand;

        Assert.Equal(0, storeCommand.RelationalCommand.Parameters.Count);
    }
Beispiel #3
0
    public async Task OperationCanceledException_is_not_wrapped_with_DbUpdateException(bool async)
    {
        var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

        var originalException = new OperationCanceledException();

        var connection = CreateConnection(
            new FakeCommandExecutor(
                executeReaderAsync: (c, b, ct) => throw originalException,
                executeReader: (c, b) => throw originalException));

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        var actualException = async
            ? await Assert.ThrowsAsync <OperationCanceledException>(() => batch.ExecuteAsync(connection))
            : Assert.Throws <OperationCanceledException>(() => batch.Execute(connection));

        Assert.Same(originalException, actualException);
    }
Beispiel #4
0
    public async Task Exception_thrown_if_rows_returned_for_command_without_store_generated_values_is_not_1(bool async)
    {
        var entry = CreateEntry(EntityState.Modified);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

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

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        var exception = async
            ? await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => batch.ExecuteAsync(connection))
            : Assert.Throws <DbUpdateConcurrencyException>(() => batch.Execute(connection));

        Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 42), exception.Message);
    }
Beispiel #5
0
    public async Task ExecuteAsync_saves_store_generated_values_when_updating()
    {
        var entry = CreateEntry(
            EntityState.Modified, generateKeyValues: true, overrideKeyValues: true, computeNonKeyValue: true);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

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

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        await batch.ExecuteAsync(connection);

        Assert.Equal(1, entry[entry.EntityType.FindProperty("Id")]);
        Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("Name")]);
    }
Beispiel #6
0
    public async Task ExecuteAsync_saves_store_generated_values_on_non_key_columns()
    {
        var entry = CreateEntry(
            EntityState.Added, generateKeyValues: true, computeNonKeyValue: true);

        entry.SetTemporaryValue(entry.EntityType.FindPrimaryKey().Properties[0], -1);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

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

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        await batch.ExecuteAsync(connection);

        Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
        Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("Name")]);
    }
Beispiel #7
0
    public void AddCommand_does_not_add_command_batch_is_invalid()
    {
        var parameterNameGenerator = new ParameterNameGenerator();

        var entry1    = CreateEntry(EntityState.Modified);
        var property1 = entry1.EntityType.FindProperty("Name") !;
        var command1  = CreateModificationCommand(
            "T1",
            null,
            true,
            new[]
        {
            new ColumnModificationParameters(
                entry1,
                property1,
                property1.GetTableColumnMappings().Single().Column,
                parameterNameGenerator.GenerateNext,
                property1.GetTableColumnMappings().Single().TypeMapping,
                false, true, false, false, true)
        });

        var entry2    = CreateEntry(EntityState.Modified);
        var property2 = entry1.EntityType.FindProperty("Name") !;
        var command2  = CreateModificationCommand(
            "T2",
            null,
            true,
            new[]
        {
            new ColumnModificationParameters(
                entry2,
                property2,
                property2.GetTableColumnMappings().Single().Column,
                parameterNameGenerator.GenerateNext,
                property2.GetTableColumnMappings().Single().TypeMapping,
                false, true, false, false, true)
        });

        var batch = new ModificationCommandBatchFake();

        Assert.True(batch.TryAddCommand(command1));
        batch.ShouldBeValid = false;

        Assert.False(batch.TryAddCommand(command2));
        batch.Complete();

        Assert.Same(command1, Assert.Single(batch.ModificationCommands));

        Assert.Equal(@"UPDATE ""T1"" SET ""Col2"" = @p0
RETURNING 1;
",
                     batch.CommandText,
                     ignoreLineEndingDifferences: true);

        Assert.Equal(1, batch.StoreCommand.RelationalCommand.Parameters.Count);
        Assert.Equal(1, batch.StoreCommand.ParameterValues.Count);
    }
Beispiel #8
0
    public void CreateStoreCommand_creates_parameters_for_each_ModificationCommand()
    {
        var entry    = CreateEntry(EntityState.Added, generateKeyValues: true);
        var property = entry.EntityType.FindProperty("Id");

        entry.SetTemporaryValue(property, 1);

        var batch = new ModificationCommandBatchFake();
        var parameterNameGenerator = new ParameterNameGenerator();

        batch.TryAddCommand(
            CreateModificationCommand(
                "T1",
                null,
                true,
                new[]
        {
            new ColumnModificationParameters(
                entry,
                property,
                property.GetTableColumnMappings().Single().Column,
                parameterNameGenerator.GenerateNext,
                property.GetTableColumnMappings().Single().TypeMapping,
                false, true, false, false, true)
        }));

        batch.TryAddCommand(
            CreateModificationCommand(
                "T1",
                null,
                true,
                new[]
        {
            new ColumnModificationParameters(
                entry,
                property,
                property.GetTableColumnMappings().Single().Column,
                parameterNameGenerator.GenerateNext,
                property.GetTableColumnMappings().Single().TypeMapping,
                false, true, false, false, true)
        }));

        batch.Complete();

        var storeCommand = batch.StoreCommand;

        Assert.Equal(2, storeCommand.RelationalCommand.Parameters.Count);
        Assert.Equal("p0", storeCommand.RelationalCommand.Parameters[0].InvariantName);
        Assert.Equal("p1", storeCommand.RelationalCommand.Parameters[1].InvariantName);

        Assert.Equal(2, storeCommand.ParameterValues.Count);
        Assert.Equal(1, storeCommand.ParameterValues["p0"]);
        Assert.Equal(1, storeCommand.ParameterValues["p1"]);
    }
Beispiel #9
0
    public void UpdateCommandText_compiles_deletes()
    {
        var entry = CreateEntry(EntityState.Deleted);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        Assert.Equal(1, batch.FakeSqlGenerator.AppendBatchHeaderCalls);
        Assert.Equal(1, batch.FakeSqlGenerator.AppendDeleteOperationCalls);
    }
Beispiel #10
0
    public void UpdateCommandText_compiles_multiple_commands()
    {
        var entry = CreateEntry(EntityState.Added);

        var parameterNameGenerator = new ParameterNameGenerator();
        var command1 = CreateModificationCommand("T1", null, parameterNameGenerator.GenerateNext, true, null);

        command1.AddEntry(entry, true);
        var command2 = CreateModificationCommand("T1", null, parameterNameGenerator.GenerateNext, true, null);

        command2.AddEntry(entry, true);

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command1);
        batch.TryAddCommand(command2);
        batch.Complete();

        Assert.Equal(1, batch.FakeSqlGenerator.AppendBatchHeaderCalls);
        Assert.Equal(2, batch.FakeSqlGenerator.AppendInsertOperationCalls);
    }
Beispiel #11
0
    public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
    {
        var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

        var dbDataReader = CreateFakeDataReader();

        var connection = CreateConnection(dbDataReader);

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        await batch.ExecuteAsync(connection);

        Assert.Equal(1, dbDataReader.ReadAsyncCount);
        Assert.Equal(1, dbDataReader.GetInt32Count);
    }
Beispiel #12
0
    public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values(bool async)
    {
        var entry = CreateEntry(EntityState.Added, generateKeyValues: true);

        entry.SetTemporaryValue(entry.EntityType.FindPrimaryKey().Properties[0], -1);

        var command = CreateModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, true, null);

        command.AddEntry(entry, true);

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

        var batch = new ModificationCommandBatchFake();

        batch.TryAddCommand(command);
        batch.Complete();

        var exception = async
            ? await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => batch.ExecuteAsync(connection))
            : Assert.Throws <DbUpdateConcurrencyException>(() => batch.Execute(connection));

        Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 0), exception.Message);
    }