2025-02-02 23:13:17 +01:00
|
|
|
using Autofac;
|
2025-02-05 18:38:29 +01:00
|
|
|
using Insight.Database;
|
2025-03-10 15:56:22 +01:00
|
|
|
using PlanTempus.Components.Users.Exceptions;
|
|
|
|
|
using PlanTempus.Core.Database;
|
|
|
|
|
using PlanTempus.Core.Database.ConnectionFactory;
|
2025-03-07 00:17:08 +01:00
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace PlanTempus.X.TDD;
|
2025-02-02 23:13:17 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestClass]
|
|
|
|
|
public class PostgresTests : TestFixture
|
2025-02-02 23:13:17 +01:00
|
|
|
{
|
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-02-02 23:13:17 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
|
2025-02-21 00:30:04 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
using (var conn = _connFactory.Create())
|
2025-02-21 00:30:04 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
conn.ExecuteSql("SELECT 1 as p");
|
2025-02-21 00:30:04 +01:00
|
|
|
}
|
2025-03-07 00:17:08 +01:00
|
|
|
}
|
2025-02-02 23:13:17 +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");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
db.Error(ex);
|
|
|
|
|
throw;
|
2025-02-02 23:13:17 +01:00
|
|
|
}
|
2025-03-07 00:17:08 +01:00
|
|
|
}
|
2025-02-06 17:48:24 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public async Task TestScopeConnectionWithErrorLogging()
|
|
|
|
|
{
|
|
|
|
|
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
|
|
|
|
try
|
2025-02-21 00:30:04 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
var user = await db.Connection.QuerySqlAsync<string>(
|
|
|
|
|
"SELECT tablename FROM pg_tables limit 5");
|
2025-02-21 00:30:04 +01:00
|
|
|
}
|
2025-03-07 00:17:08 +01:00
|
|
|
catch (Exception ex)
|
2025-02-02 23:13:17 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
db.Error(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public async Task TestForUniqueUserEmail()
|
|
|
|
|
{
|
|
|
|
|
using var db = _databaseOperations.CreateScope(nameof(TestForUniqueUserEmail));
|
2026-01-08 21:51:43 +01:00
|
|
|
try
|
2025-03-07 00:17:08 +01:00
|
|
|
{
|
2026-01-08 21:51:43 +01:00
|
|
|
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)
|
2025-03-07 00:17:08 +01:00
|
|
|
RETURNING id, created_at, email, is_active";
|
2025-02-21 00:30:04 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
var parameters = new
|
2025-02-21 00:30:04 +01:00
|
|
|
{
|
2026-01-08 21:51:43 +01:00
|
|
|
Email = $"{GetRandomWord()}@mars.com",
|
2025-03-07 00:17:08 +01:00
|
|
|
PasswordHash = "MartianRover2025",
|
|
|
|
|
SecurityStamp = "MarsOrBust",
|
|
|
|
|
EmailConfirmed = true,
|
|
|
|
|
AccessFailedCount = 0,
|
|
|
|
|
LockoutEnabled = false,
|
|
|
|
|
IsActive = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var user = await db.Connection.QuerySqlAsync<dynamic>(sql, parameters);
|
|
|
|
|
|
2026-01-08 21:51:43 +01:00
|
|
|
//EmailAlreadyRegistreredException
|
2025-03-10 15:56:22 +01:00
|
|
|
//try insert, to test exception
|
2026-01-08 21:51:43 +01:00
|
|
|
var ex = await Should.ThrowAsync<Npgsql.PostgresException>(async () =>
|
|
|
|
|
await db.Connection.QuerySqlAsync<dynamic>(sql, parameters));
|
|
|
|
|
ex.ConstraintName.ShouldBe("users_email_key");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
db.Error(ex);
|
|
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public async Task TestSimpleDatabaseOperation()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _databaseOperations.ExecuteAsync(async connection =>
|
2025-02-21 00:30:04 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
return await connection.QuerySqlAsync<string>(
|
|
|
|
|
"SELECT tablename FROM pg_tables limit 5");
|
|
|
|
|
}, nameof(TestSimpleDatabaseOperation));
|
2025-02-21 00:30:04 +01:00
|
|
|
}
|
2025-03-07 00:17:08 +01:00
|
|
|
catch (Exception ex)
|
2025-02-02 23:13:17 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
throw;
|
2025-02-02 23:13:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
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";
|
|
|
|
|
// });
|
|
|
|
|
}
|
2025-02-02 23:13:17 +01:00
|
|
|
}
|