123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using Luticate2.Utils.DataAccess;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.DependencyInjection;
- using TestUtils.DataAccess;
-
- namespace TestUtils
- {
- public class Tests
- {
- public const string ConnectionString =
- "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
-
- public static IServiceProvider BuildServiceProvider()
- {
- IServiceCollection serviceCollection = new ServiceCollection();
- serviceCollection.AddScoped<LuEfTransactionScope>();
- serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
- serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
- serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
- serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
- ServiceLifetime.Transient);
- return serviceCollection.BuildServiceProvider();
- }
-
- protected static void _TestRealDb(Action<IServiceProvider> func)
- {
- var serviceProvider = BuildServiceProvider();
- var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
- transactionScope.BeginTransaction<LuUtilsDbContext>(null);
- try
- {
- func(serviceProvider);
- }
- finally
- {
- transactionScope.RollbackTransaction<LuUtilsDbContext>();
- }
- }
-
- public static void TestRealDb<TDataAccess>(Action<TDataAccess> func)
- {
- _TestRealDb(provider =>
- {
- func(provider.GetService<TDataAccess>());
- });
- }
-
- public static void TestRealDb<TDataAccess, TDataAccess2>(Action<TDataAccess, TDataAccess2> func)
- {
- _TestRealDb(provider =>
- {
- func(provider.GetService<TDataAccess>(), provider.GetService<TDataAccess2>());
- });
- }
- }
- }
|