public async Task When_inserting_with_transaction_rolled_back() { var fileInfo = new FileInfo(Path.GetTempFileName()); try { using (var conn = new SQLiteFileConnection(fileInfo)) { conn.Open(); await conn.ExecuteAsync(TableQuery); var person = new Person { Name = "P1", Age = 10 }; var repo = conn.GetDBContext <Person>(SQLiteDialect.Instance); using (var tran = repo.Connection.BeginTransaction()) { await repo.Insert(person); (await repo.Count(p => p.Id, transaction: tran)).ShouldBe((ulong)1); (await repo.Count(p => p.Id)).ShouldBe((ulong)1); tran.Rollback(); (await repo.Count(p => p.Id)).ShouldBe((ulong)0); } (await repo.Count(p => p.Id)).ShouldBe((ulong)0); } } finally { fileInfo.Delete(); } }
public void When_creating_connection_with_valid_connection_string_with_parameters() { using (var conn = new SQLiteFileConnection(SQLiteConnectionStringProvider.GetFileConnectionString(new FileInfo("C:\\SomeFile.db")))) { conn.ConnectionString.ShouldBe(@"data source=C:\SomeFile.db;failifmissing=False;pooling=False;binaryguid=False;datetimekind=Utc;datetimeformat=UnixEpoch;journal mode=Wal;synchronous=Off;useutf16encoding=False;read only=False;legacy format=False;page size=4096;cache size=-2000"); conn.File.FullName.ShouldBe("C:\\SomeFile.db"); conn.ConnectionTimeout.ShouldBe(15); conn.DataSource.ShouldBeNull(); conn.Database.ShouldBe("main"); conn.ServerVersion.ShouldBe("3.28.0"); conn.State.ShouldBe(ConnectionState.Closed); } }
public void When_creating_connection_with_valid_connection_string() { using (var conn = new SQLiteFileConnection("data source=C:\\SomeFile.db")) { conn.ConnectionString.ShouldBe(@"data source=C:\SomeFile.db"); conn.File.FullName.ShouldBe("C:\\SomeFile.db"); conn.ConnectionTimeout.ShouldBe(15); conn.DataSource.ShouldBeNull(); conn.Database.ShouldBe("main"); conn.ServerVersion.ShouldBe("3.28.0"); conn.State.ShouldBe(ConnectionState.Closed); } }
public async Task When_inserting_and_updating_with_transaction_with_isolation_level_rolled_back() { var fileInfo = new FileInfo(Path.GetTempFileName()); try { using (var conn = new SQLiteFileConnection(fileInfo)) { conn.Open(); await conn.ExecuteAsync(TableQuery); var person = new Person { Name = "P1", Age = 10 }; var repo = conn.GetDBContext <Person>(SQLiteDialect.Instance); await repo.Insert(person); var insertedPerson = (await repo.Get()).Single(); var updatedPerson = new Person { Id = insertedPerson.Id, Name = "P1-updated", Age = 15 }; using (var tran = repo.Connection.BeginTransaction(IsolationLevel.ReadCommitted)) { await repo.Update(updatedPerson); var snapshot1 = (await repo.Get()).Single(); snapshot1.Id.ShouldBe(insertedPerson.Id); snapshot1.Name.ShouldBe(updatedPerson.Name); snapshot1.Age.ShouldBe(updatedPerson.Age); tran.Rollback(); var snapshot2 = (await repo.Get()).Single(); snapshot2.Id.ShouldBe(insertedPerson.Id); snapshot2.Name.ShouldBe(insertedPerson.Name); snapshot2.Age.ShouldBe(insertedPerson.Age); } var snapshot3 = (await repo.Get()).Single(); snapshot3.Id.ShouldBe(insertedPerson.Id); snapshot3.Name.ShouldBe(insertedPerson.Name); snapshot3.Age.ShouldBe(insertedPerson.Age); } } finally { fileInfo.Delete(); } }
public async Task When_inserting_multiple_with_transaction_commited_one_inside_the_other_outside() { var fileInfo = new FileInfo(Path.GetTempFileName()); try { using (var conn = new SQLiteFileConnection(fileInfo)) { conn.Open(); await conn.ExecuteAsync(TableQuery); var person1 = new Person { Name = "P1", Age = 10 }; var repo = conn.GetDBContext <Person>(SQLiteDialect.Instance); await repo.Insert(person1); using (var tran = repo.Connection.BeginTransaction()) { var person2 = new Person { Name = "P2", Age = 20 }; await repo.Insert(person2); (await repo.Count(p => p.Id, transaction: tran)).ShouldBe((ulong)2); (await repo.Count(p => p.Id)).ShouldBe((ulong)2); tran.Commit(); (await repo.Count(p => p.Id)).ShouldBe((ulong)2); } (await repo.Count(p => p.Id)).ShouldBe((ulong)2); } } finally { fileInfo.Delete(); } }
public void When_changing_connection_state() { var fileInfo = new FileInfo(Path.GetTempFileName()); try { var conn = new SQLiteFileConnection(fileInfo); conn.State.ShouldBe(ConnectionState.Closed); conn.Open(); conn.State.ShouldBe(ConnectionState.Open); conn.Close(); conn.State.ShouldBe(ConnectionState.Open); conn.Dispose(); Should.Throw <ObjectDisposedException>(() => conn.State.ShouldBe(ConnectionState.Closed)) .Message.ShouldBe("Cannot access a disposed object.\r\nObject name: 'SQLiteConnection'."); } catch (Exception) { fileInfo.Delete(); } }