1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using System;
- using Luticate2.Utils.DataAccess;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.DependencyInjection;
- using WebApiWebSem.DataAccess;
-
- namespace Test
- {
- public class Tests
- {
- public const string RealDbConnectionString =
- "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=websem_project;Pooling=true;";
-
- public static IServiceProvider BuildRealDbServiceProvider()
- {
- IServiceCollection serviceCollection = new ServiceCollection();
- serviceCollection.AddScoped<LuEfTransactionScope>();
- serviceCollection.AddTransient<ArticlesDataAccess>();
- serviceCollection.AddDbContext<WsDbContext>(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<WsDbContext>(null);
- try
- {
- func(serviceProvider);
- }
- finally
- {
- transactionScope.RollbackTransaction<WsDbContext>();
- }
- }
-
- public static void TestRealDb<TDataAccess>(Action<TDataAccess> func)
- {
- _TestRealDb(provider =>
- {
- func(provider.GetService<TDataAccess>());
- });
- }
- }
- }
|