Esempio n. 1
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 = new ModificationCommand("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.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("Name")]);
        }
Esempio n. 2
0
        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.FindPrimaryKey().Properties[0]);

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

            command.AddEntry(entry);

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

            var batch = new ModificationCommandBatchFake();

            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
        }
Esempio n. 3
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 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);
        }
        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")]);
        }
Esempio n. 6
0
        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 transaction = Mock.Of <IRelationalTransaction>();

            await batch.ExecuteAsync(transaction, new ConcreteTypeMapper(), 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. 7
0
        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.TestProvider());

            command.AddEntry(entry);

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

            batch.AddCommand(command);

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

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

            mockReader.Verify(r => r.ReadAsync(It.IsAny <CancellationToken>()), Times.Once);
            mockReader.Verify(r => r.GetInt32(0), Times.Once);
        }
Esempio n. 8
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 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")]);
        }
        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 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 ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var entry = CreateEntry(EntityState.Added);

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

            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 stateEntry = CreateStateEntry(EntityState.Added);
            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);

            Assert.Equal(Strings.FormatUpdateConcurrencyException(1, 42),
                         (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 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);
        }
Esempio n. 16
0
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Modified, ValueGenerationOnSave.WhenInserting, ValueGenerationOnSave.WhenInsertingAndUpdating);

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

            command.AddStateEntry(stateEntry);

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

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

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

            Assert.Equal(1, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", stateEntry[stateEntry.EntityType.GetProperty("Name")]);
        }
Esempio n. 17
0
        public async Task ExecuteAsync_saves_store_generated_values_on_non_key_columns()
        {
            var stateEntry = CreateStateEntry(
                EntityState.Added, ValueGeneration.OnAdd, ValueGeneration.OnAddAndUpdate);

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

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

            command.AddStateEntry(stateEntry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1", "Col2" }, new List <object[]> {
                new object[] { 42, "FortyTwo" }
            }).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("FortyTwo", stateEntry[stateEntry.EntityType.GetProperty("Name")]);
        }
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var stateEntry = CreateStateEntry(EntityState.Added, generateKeyValues: true);

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

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

            command.AddStateEntry(stateEntry);

            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);
        }
Esempio n. 19
0
        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.TestProvider());

            command.AddEntry(entry);

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

            batch.AddCommand(command);

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

            await batch.ExecuteAsync(connection, 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 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. 21
0
        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, true, null);

            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")]);
        }
Esempio n. 22
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);
    }
        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. 24
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);
        }
Esempio n. 25
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.Relational(), new TypedValueBufferFactoryFactory());

            command.AddEntry(entry);

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

            batch.AddCommand(command);

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

            await batch.ExecuteAsync(transaction, new ConcreteTypeMapper(), new Mock <DbContext>().Object, new Mock <ILogger>().Object);

            Assert.Equal(42, 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.TestProvider());
            command.AddEntry(entry);

            var dbDataReader = CreateFakeDataReader();

            var commandBuilderFactory = new FakeCommandBuilderFactory(dbDataReader);

            var batch = new ModificationCommandBatchFake(factory: commandBuilderFactory);
            batch.AddCommand(command);

            var connection = CreateConnection();

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, dbDataReader.ReadAsyncCount);
            Assert.Equal(1, dbDataReader.GetInt32Count);
        }
        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("T1", null, 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, new Mock<SqlGenerator> { CallBase = true }.Object);

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

            Assert.Equal(42, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
        }
        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 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.Relational(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1", "Col2" }, new List<object[]> { new object[] { 42, "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(42, entry[entry.EntityType.GetProperty("Id")]);
            Assert.Equal("FortyTwo", 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.FindPrimaryKey().Properties[0]);

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

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

            var batch = new ModificationCommandBatchFake(factory: commandBuilderFactory);
            batch.AddCommand(command);

            var connection = CreateConnection();

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 0),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(connection))).Message);
        }
        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.TestProvider());
            command.AddEntry(entry);

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

            var batch = new ModificationCommandBatchFake(factory: commandBuilderFactory);
            batch.AddCommand(command);

            var connection = CreateConnection();

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 42),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(connection))).Message);
        }
        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.FindPrimaryKey().Properties[0]);

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

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

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

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("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.TestProvider(), 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 transaction = Mock.Of<IRelationalTransaction>();

            await batch.ExecuteAsync(transaction, new ConcreteTypeMapper(), new Mock<DbContext>().Object, new Mock<ILogger>().Object);

            Assert.Equal(42, entry[entry.EntityType.GetProperty("Id")]);
        }
        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.TestProvider(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

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

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

            await batch.ExecuteAsync(transaction, new ConcreteTypeMapper(), 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_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()
        {
            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[]> { new object[] { 42 } }).Object);
            batch.AddCommand(command, new Mock<SqlGenerator> { CallBase = true }.Object);

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

            Assert.Equal(42, stateEntry[stateEntry.EntityType.GetProperty("Id")]);
            Assert.Equal("Test", stateEntry[stateEntry.EntityType.GetProperty("Name")]);
        }
        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);
        }
        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.TestProvider());
            command.AddEntry(entry);

            var batch = new ModificationCommandBatchFake(CreateDataReaderMock(new[] { "Col1" }, new List<object[]> { new object[] { 42 } }).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("Test", entry[entry.EntityType.GetProperty("Name")]);
        }
        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.TestProvider(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

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

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

            await batch.ExecuteAsync(transaction, new ConcreteTypeMapper(), 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_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.TestProvider());
            command.AddEntry(entry);

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

            var batch = new ModificationCommandBatchFake(factory: commandBuilderFactory);
            batch.AddCommand(command);

            var connection = CreateConnection();

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("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.TestProvider(), new TypedValueBufferFactoryFactory());
            command.AddEntry(entry);

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

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

            Assert.Equal(Strings.UpdateConcurrencyException(1, 0),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(
                        transaction,
                        new ConcreteTypeMapper(),
                        new Mock<DbContext>().Object,
                        new Mock<ILogger>().Object))).Message);
        }
        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.FindPrimaryKey().Properties[0]);

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

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

            var batch = new ModificationCommandBatchFake(factory: commandBuilderFactory);
            batch.AddCommand(command);

            var connection = CreateConnection();

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
        }