wip
This commit is contained in:
parent
0010a32248
commit
ddb6abc14e
14 changed files with 754 additions and 718 deletions
|
|
@ -1,95 +1,137 @@
|
|||
using Autofac;
|
||||
using System.Data;
|
||||
using Insight.Database;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using PlanTempus.Core.Sql;
|
||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||
using Shouldly;
|
||||
|
||||
namespace PlanTempus.Tests
|
||||
namespace PlanTempus.X.TDD;
|
||||
|
||||
[TestClass]
|
||||
public class PostgresTests : TestFixture
|
||||
{
|
||||
[TestClass]
|
||||
public class PostgresTests : TestFixture
|
||||
private IDbConnectionFactory _connFactory;
|
||||
private IDatabaseOperations _databaseOperations;
|
||||
|
||||
[TestInitialize]
|
||||
public void MyTestMethod()
|
||||
{
|
||||
IDbConnectionFactory _connFactory;
|
||||
IDatabaseOperations _databaseOperations;
|
||||
_connFactory = Container.Resolve<IDbConnectionFactory>();
|
||||
_databaseOperations = Container.Resolve<IDatabaseOperations>();
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void MyTestMethod()
|
||||
[TestMethod]
|
||||
public void TestDefaultConnection()
|
||||
{
|
||||
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
|
||||
|
||||
using (var conn = _connFactory.Create())
|
||||
{
|
||||
_connFactory = Container.Resolve<IDbConnectionFactory>();
|
||||
_databaseOperations = Container.Resolve<IDatabaseOperations>();
|
||||
}
|
||||
[TestMethod]
|
||||
public void TestDefaultConnection()
|
||||
{
|
||||
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
|
||||
|
||||
using (var conn = _connFactory.Create())
|
||||
conn.ExecuteSql("SELECT 1 as p");
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestScopeConnectionWithLogging()
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||
try
|
||||
{
|
||||
var user = await db.Connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tables limit 5");
|
||||
|
||||
db.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task TestScopeConnectionWithErrorLogging()
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||
try
|
||||
{
|
||||
var user = await db.Connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tabless limit 5");
|
||||
|
||||
db.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
}
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task TestSimpleDatabaseOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _databaseOperations.ExecuteAsync(async connection =>
|
||||
{
|
||||
return await connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tables limit 5");
|
||||
}, nameof(TestSimpleDatabaseOperation));
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[TestMethod]
|
||||
public void SetupPostgresql_LISTEN()
|
||||
{
|
||||
//this is not in use a the moment... kind of premature test to get my mind in to gear
|
||||
//var builder = new ConfigurationBuilder()
|
||||
// .AddPostgresConfiguration(options =>
|
||||
// {
|
||||
// options.ConnectionString = "Host=192.168.1.57;Database=ptdb01;Username=postgres;Password=3911";
|
||||
// options.Channel = "config_changes";
|
||||
// options.ConfigurationQuery = @"select * from dev.app_configuration";
|
||||
// });
|
||||
conn.ExecuteSql("SELECT 1 as p");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestScopeConnectionWithLogging()
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||
try
|
||||
{
|
||||
var user = await db.Connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tables limit 5");
|
||||
|
||||
db.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestScopeConnectionWithErrorLogging()
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||
try
|
||||
{
|
||||
var user = await db.Connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tables limit 5");
|
||||
|
||||
db.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestForUniqueUserEmail()
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(TestForUniqueUserEmail));
|
||||
try
|
||||
{
|
||||
var sql = @"
|
||||
INSERT INTO system.users(email, password_hash, security_stamp, email_confirmed,
|
||||
access_failed_count, lockout_enabled,
|
||||
is_active)
|
||||
VALUES(@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed,
|
||||
@AccessFailedCount, @LockoutEnabled, @IsActive)
|
||||
RETURNING id, created_at, email, is_active";
|
||||
|
||||
var parameters = new
|
||||
{
|
||||
Email = "elon.musk@mars.com",
|
||||
PasswordHash = "MartianRover2025",
|
||||
SecurityStamp = "MarsOrBust",
|
||||
EmailConfirmed = true,
|
||||
AccessFailedCount = 0,
|
||||
LockoutEnabled = false,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
var user = await db.Connection.QuerySqlAsync<dynamic>(sql, parameters);
|
||||
|
||||
user.ShouldNotBeNull();
|
||||
// ((string)user.email).ShouldBe("elon.musk@mars.com");
|
||||
// ((bool)user.is_active).ShouldBeTrue();
|
||||
|
||||
db.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestSimpleDatabaseOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _databaseOperations.ExecuteAsync(async connection =>
|
||||
{
|
||||
return await connection.QuerySqlAsync<string>(
|
||||
"SELECT tablename FROM pg_tables limit 5");
|
||||
}, nameof(TestSimpleDatabaseOperation));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SetupPostgresql_LISTEN()
|
||||
{
|
||||
//this is not in use a the moment... kind of premature test to get my mind in to gear
|
||||
//var builder = new ConfigurationBuilder()
|
||||
// .AddPostgresConfiguration(options =>
|
||||
// {
|
||||
// options.ConnectionString = "Host=192.168.1.57;Database=ptdb01;Username=postgres;Password=3911";
|
||||
// options.Channel = "config_changes";
|
||||
// options.ConfigurationQuery = @"select * from dev.app_configuration";
|
||||
// });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue