PlanTempusApp/Tests/PostgresTests.cs

95 lines
3 KiB
C#
Raw Normal View History

using Autofac;
using System.Data;
using Insight.Database;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PlanTempus.Database.Core.Sql;
2025-02-21 17:03:49 +01:00
using PlanTempus.Database.Core.ConnectionFactory;
namespace PlanTempus.Tests
{
[TestClass]
public class PostgresTests : TestFixture
{
IDbConnectionFactory _connFactory;
IDatabaseOperations _databaseOperations;
[TestInitialize]
public void MyTestMethod()
{
2025-02-21 17:03:49 +01:00
_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";
// });
}
}
}