137 lines
No EOL
3.9 KiB
C#
137 lines
No EOL
3.9 KiB
C#
using Autofac;
|
|
using Insight.Database;
|
|
using PlanTempus.Core.Sql;
|
|
using PlanTempus.Core.Sql.ConnectionFactory;
|
|
using Shouldly;
|
|
|
|
namespace PlanTempus.X.TDD;
|
|
|
|
[TestClass]
|
|
public class PostgresTests : TestFixture
|
|
{
|
|
private IDbConnectionFactory _connFactory;
|
|
private IDatabaseOperations _databaseOperations;
|
|
|
|
[TestInitialize]
|
|
public void MyTestMethod()
|
|
{
|
|
_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_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";
|
|
// });
|
|
}
|
|
} |