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(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddDbContext(builder => builder.UseNpgsql(ConnectionString), ServiceLifetime.Transient); return serviceCollection.BuildServiceProvider(); } protected static void _TestRealDb(Action func) { var serviceProvider = BuildServiceProvider(); var transactionScope = serviceProvider.GetService(); transactionScope.BeginTransaction(null); try { func(serviceProvider); } finally { transactionScope.RollbackTransaction(); } } public static void TestRealDb(Action func) { _TestRealDb(provider => { func(provider.GetService()); }); } public static void TestRealDb(Action func) { _TestRealDb(provider => { func(provider.GetService(), provider.GetService()); }); } } }