123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.IO;
- using Luticate2.Utils.DataAccess;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.DependencyInjection;
- using TestUtils.DataAccess;
-
- namespace TestUtils
- {
- public class Tests
- {
- public const string RealDbConnectionString =
- "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
-
- public const string RealFsDir =
- "/tmp/luticate2/";
-
- public static IServiceProvider BuildRealDbServiceProvider()
- {
- IServiceCollection serviceCollection = new ServiceCollection();
- serviceCollection.AddScoped<LuEfTransactionScope>();
- serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
- serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
- serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
- serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(RealDbConnectionString),
- ServiceLifetime.Transient);
- return serviceCollection.BuildServiceProvider();
- }
-
- protected static void _TestRealDb(Action<IServiceProvider> func)
- {
- var serviceProvider = BuildRealDbServiceProvider();
- 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>());
- });
- }
-
- public static void TestRealFs(Action<LuFsFilesCrudDataAccess> func)
- {
- var tmp = Path.Combine(RealFsDir, "RealFsDir-" + new Random().Next());
- try
- {
- Directory.CreateDirectory(tmp);
- func(new LuTmpFsFilesCrudDataAccess(tmp));
- }
- finally
- {
- Directory.Delete(tmp, true);
- }
- }
- }
- }
|