Initial commit: SWP.Core enterprise framework with multi-tenant architecture, configuration management, security, telemetry and comprehensive test suite
This commit is contained in:
commit
5275a75502
87 changed files with 6140 additions and 0 deletions
80
Tests/PostgresTests.cs
Normal file
80
Tests/PostgresTests.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
using Autofac;
|
||||
using Insight.Database;
|
||||
using Shouldly;
|
||||
using SWP.Core.Database;
|
||||
using SWP.Core.Database.ConnectionFactory;
|
||||
|
||||
namespace SWP.Core.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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
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)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue