using Autofac; using System.Data; using Insight.Database; using Microsoft.VisualStudio.TestTools.UnitTesting; using PlanTempus.Database.Core.Sql; using PlanTempus.Database.Core.ConnectionFactory; namespace PlanTempus.Tests { [TestClass] public class PostgresTests : TestFixture { IDbConnectionFactory _connFactory; IDatabaseOperations _databaseOperations; [TestInitialize] public void MyTestMethod() { _connFactory = Container.Resolve(); _databaseOperations = Container.Resolve(); } [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( "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( "SELECT tablename FROM pg_tabless limit 5"); db.Success(); } catch (Exception ex) { db.Error(ex); throw; } } [TestMethod] public async Task TestSimpleDatabaseOperation() { try { await _databaseOperations.ExecuteAsync(async connection => { return await connection.QuerySqlAsync( "SELECT tablename FROM pg_tables limit 5"); }, nameof(TestSimpleDatabaseOperation)); } catch (Exception ex) { throw; } } [TestMethod] public async Task TryDbSetup() { //var conn = Container.Resolve(); //var identitySystem = new Database.Core.SetupIdentitySystem(conn); //identitySystem.CreateSystem("swp"); } [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"; // }); } } }