public async Task RepositoryBaseTests_OpenAnOpenConnection(string server, ushort port, string?database = default) { var config = new Config(server, port, database !, _username, _password, _passwordFile); using var sut = new TestRepository(config); using (var transaction = sut.BeginTransaction()) { await sut.GetDateTimeAsync(); } using (var transaction = sut.BeginTransaction()) { await sut.GetDateTimeAsync(); } }
public async Task RepositoryBaseTests_TransactionTest(string server, ushort port, string database, string tableName) { var config = new Config(server, port, database, _username, _password, _passwordFile); using var sut = new TestRepository(config); await sut.SafeDropTableAsync(tableName); Assert.False(await sut.CheckTableExistsAsync(tableName)); // make a table await sut.SafeCreateTableAsync( tableName, $@"CREATE TABLE `test`.`{tableName}` ( `id` SMALLINT NOT NULL, `name` VARCHAR(100) NOT NULL, PRIMARY KEY (`id`) );" ); IEnumerable <(short id, string name)> results; using var transaction = sut.BeginTransaction(); // check it's empty results = await sut.QueryAsync <(short id, string name)>($"select * from `test`.`{tableName}`").ToListAsync(); Assert.Empty(results); // add to it await sut.ExecuteAsync( $@"insert `test`.`{tableName}` (id, name) values (@id, @name);", new { id = 1, name = "test", }, transaction); // check it's not empty results = await sut.QueryAsync <(short id, string name)>($"select * from `test`.`{tableName}`").ToListAsync(); Assert.NotEmpty(results); // rollback the transaction transaction.Rollback(); // check the table is empty results = await sut.QueryAsync <(short id, string name)>($"select * from `test`.`{tableName}`").ToListAsync(); Assert.Empty(results); await sut.SafeDropTableAsync(tableName); // drop the table Assert.False(await sut.CheckTableExistsAsync(tableName)); await sut.SafeDropDatabaseAsync("test"); }