PlanTempusApp/Tests/PostgresTests.cs

137 lines
3.9 KiB
C#
Raw Normal View History

using Autofac;
using Insight.Database;
2025-03-03 17:40:16 +01:00
using PlanTempus.Core.Sql;
2025-03-07 00:17:08 +01:00
using PlanTempus.Core.Sql.ConnectionFactory;
using Shouldly;
namespace PlanTempus.X.TDD;
2025-03-07 00:17:08 +01:00
[TestClass]
public class PostgresTests : TestFixture
{
2025-03-07 00:17:08 +01:00
private IDbConnectionFactory _connFactory;
private IDatabaseOperations _databaseOperations;
[TestInitialize]
public void MyTestMethod()
{
_connFactory = Container.Resolve<IDbConnectionFactory>();
_databaseOperations = Container.Resolve<IDatabaseOperations>();
}
[TestMethod]
public void TestDefaultConnection()
{
2025-03-07 00:17:08 +01:00
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
2025-03-07 00:17:08 +01:00
using (var conn = _connFactory.Create())
{
2025-03-07 00:17:08 +01:00
conn.ExecuteSql("SELECT 1 as p");
}
2025-03-07 00:17:08 +01:00
}
2025-03-07 00:17:08 +01:00
[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");
2025-03-07 00:17:08 +01:00
db.Success();
}
catch (Exception ex)
{
db.Error(ex);
throw;
}
2025-03-07 00:17:08 +01:00
}
2025-03-07 00:17:08 +01:00
[TestMethod]
public async Task TestScopeConnectionWithErrorLogging()
{
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
try
{
2025-03-07 00:17:08 +01:00
var user = await db.Connection.QuerySqlAsync<string>(
"SELECT tablename FROM pg_tables limit 5");
2025-03-07 00:17:08 +01:00
db.Success();
}
2025-03-07 00:17:08 +01:00
catch (Exception ex)
{
2025-03-07 00:17:08 +01:00
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";
2025-03-07 00:17:08 +01:00
var parameters = new
{
2025-03-07 00:17:08 +01:00
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();
}
2025-03-07 00:17:08 +01:00
catch (Exception ex)
{
2025-03-07 00:17:08 +01:00
db.Error(ex);
}
}
[TestMethod]
public async Task TestSimpleDatabaseOperation()
{
try
{
await _databaseOperations.ExecuteAsync(async connection =>
{
2025-03-07 00:17:08 +01:00
return await connection.QuerySqlAsync<string>(
"SELECT tablename FROM pg_tables limit 5");
}, nameof(TestSimpleDatabaseOperation));
}
2025-03-07 00:17:08 +01:00
catch (Exception ex)
{
2025-03-07 00:17:08 +01:00
throw;
}
}
2025-03-07 00:17:08 +01:00
[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";
// });
}
}