public void WriteRecord_MultipleRecordsWithoutTransactionAndNoCommit_ChangesAreCommitted() { var people = Person.Faker.Generate(2); var commands = new SqlCommand[] { DatabaseWriterTests.GenerateInsertPersonCommand(people[0]), DatabaseWriterTests.GenerateInsertPersonCommand(people[1]) }; var commandFactory = MockRepository.GenerateMock <IDatabaseWriterCommandFactory>(); commandFactory.Stub(x => x.Create(Arg <object> .Is.Equal(people[0]))).Return(commands[0]); commandFactory.Stub(x => x.Create(Arg <object> .Is.Equal(people[1]))).Return(commands[1]); using (var databaseWriter = new DatabaseWriter(commandFactory, DatabaseWriterTests.DbConnectionString, false)) { databaseWriter.Open(); databaseWriter.WriteRecord(people[0]); databaseWriter.WriteRecord(people[1]); } using (var testDb = new TestDb(DatabaseWriterTests.DbConnectionString)) { var records = testDb.Delete("dbo.Person", people.ToArray()); Assert.AreEqual(2, records.Length); } }
public void SqlBulkCopyWriter_WriteMultipleRecordsWithCommit_RecordsAreWrittenToDatabase() { var records = Person.Faker.Generate(10); var columnMappingProvider = MockRepository.GenerateMock <ISqlBulkCopyColumnMappingProvider>(); columnMappingProvider.Stub(x => x.GetColumnMappings()).Return(SqlBulkCopyWriterTests.ColumnMappings); using (var sqlBulkCopyWriter = new SqlBulkCopyWriter(columnMappingProvider, SqlBulkCopyWriterTests.DbConnectionString, "dbo.Person")) { sqlBulkCopyWriter.Open(); foreach (var record in records) { sqlBulkCopyWriter.WriteRecord(record); } sqlBulkCopyWriter.Commit(); } using (var testDb = new TestDb(SqlBulkCopyWriterTests.DbConnectionString)) { var recordsInDb = testDb.Delete("dbo.Person", records.ToArray()); Assert.AreEqual(records.Count, recordsInDb.Length); } }
public void WriteRecord_MultipleRecords_RecordsAreWritten() { var person1 = Person.Faker.Generate(); var person2 = Person.Faker.Generate(); var person3 = Person.Faker.Generate(); var connection = new SqlConnection(DatabaseBatchWriterTests.DbConnectionString); var command = new SqlCommand("dbo.InsertPeople", connection); command.CommandType = CommandType.StoredProcedure; var tvpParameter = command.Parameters.Add("People", SqlDbType.Structured); tvpParameter.TypeName = "dbo.PersonType"; using (var databaseBatchWriter = new DatabaseBatchWriter(command)) { databaseBatchWriter.Open(); databaseBatchWriter.WriteRecord(person1); databaseBatchWriter.WriteRecord(person2); databaseBatchWriter.WriteRecord(person3); databaseBatchWriter.Commit(); } using (var testDb = new TestDb(DatabaseBatchWriterTests.DbConnectionString)) { var personRecords = testDb.Delete("dbo.Person", person1, person2, person3); Assert.AreEqual(3, personRecords.Length); } }