Setting up a db factory with logging

Important so we can log all commands
This commit is contained in:
Janus C. H. Knudsen 2025-02-21 00:30:04 +01:00
parent ad4ed12f00
commit 1501ff442a
10 changed files with 412 additions and 177 deletions

View file

@ -1,58 +1,84 @@
using Autofac;
using System.Data;
using Insight.Database;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PlanTempus.Database.Core.Sql;
namespace PlanTempus.Tests
{
[TestClass]
public class PostgresTests : TestFixture
{
Database.ModuleRegistry.IDbConnectionFactory _connFactory;
IDatabaseOperations _databaseOperations;
[TestInitialize]
public void MyTestMethod()
{
_connFactory = Container.Resolve<Database.ModuleRegistry.IDbConnectionFactory>();
_databaseOperations = Container.Resolve<IDatabaseOperations>();
}
[TestMethod]
public void TestDefaultConnection()
{
var conn = Container.Resolve<IDbConnection>();
//https://www.reddit.com/r/dotnet/comments/6wdoyn/how_to_properly_register_dapper_on_net_core_2_di/
//https://www.code4it.dev/blog/postgres-crud-dapper/
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
conn.ExecuteSql("SELECT 1 as p");
using (var conn = _connFactory.Create())
conn.ExecuteSql("SELECT 1 as p");
}
//static HttpClient _client = new HttpClient();
//[TestMethod]
//public async Task MyTestMethod()
//{
// _client.BaseAddress = new Uri("http://localhost:5341");
// var l = new SeqLogger(_client, "", "");
// for (int i = 0; i < 20; i++)
// {
// await l.LogToSeq(
// "Bruger {UserId} loggede ind",
// "Debug",
// new Dictionary<string, object> { { "UserId", "12345" }, { "Counter", i++ } }
// );
// }
// var logger = Container.Resolve<Microsoft.ApplicationInsights.TelemetryClient>();
// for (int i = 0; i < 5; i++)
// {
// logger.TrackTrace("Hello 23", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
// }
//}
[TestMethod]
public void TryOrganizationSetupService()
public async Task TestScopeConnectionWithLogging()
{
var conn = Container.Resolve<IDbConnection>();
}
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);
throw;
}
}
[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 async Task TryDbSetup()
{
@ -65,6 +91,7 @@ namespace PlanTempus.Tests
[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 =>
// {