You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tests.cs 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using Luticate2.Utils.DataAccess;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using WebApiWebSem.DataAccess;
  6. namespace Test
  7. {
  8. public class Tests
  9. {
  10. public const string RealDbConnectionString =
  11. "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=websem_project;Pooling=true;";
  12. public static IServiceProvider BuildRealDbServiceProvider()
  13. {
  14. IServiceCollection serviceCollection = new ServiceCollection();
  15. serviceCollection.AddScoped<LuEfTransactionScope>();
  16. serviceCollection.AddTransient<ArticlesDataAccess>();
  17. serviceCollection.AddDbContext<WsDbContext>(builder => builder.UseNpgsql(RealDbConnectionString),
  18. ServiceLifetime.Transient);
  19. return serviceCollection.BuildServiceProvider();
  20. }
  21. protected static void _TestRealDb(Action<IServiceProvider> func)
  22. {
  23. var serviceProvider = BuildRealDbServiceProvider();
  24. var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
  25. transactionScope.BeginTransaction<WsDbContext>(null);
  26. try
  27. {
  28. func(serviceProvider);
  29. }
  30. finally
  31. {
  32. transactionScope.RollbackTransaction<WsDbContext>();
  33. }
  34. }
  35. public static void TestRealDb<TDataAccess>(Action<TDataAccess> func)
  36. {
  37. _TestRealDb(provider =>
  38. {
  39. func(provider.GetService<TDataAccess>());
  40. });
  41. }
  42. }
  43. }