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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.IO;
  3. using Luticate2.Utils.DataAccess;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using TestUtils.DataAccess;
  7. namespace TestUtils
  8. {
  9. public class Tests
  10. {
  11. public const string RealDbConnectionString =
  12. "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
  13. public const string RealFsDir =
  14. "/tmp/luticate2/";
  15. public static IServiceProvider BuildRealDbServiceProvider()
  16. {
  17. IServiceCollection serviceCollection = new ServiceCollection();
  18. serviceCollection.AddScoped<LuEfTransactionScope>();
  19. serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
  20. serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
  21. serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
  22. serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(RealDbConnectionString),
  23. ServiceLifetime.Transient);
  24. return serviceCollection.BuildServiceProvider();
  25. }
  26. protected static void _TestRealDb(Action<IServiceProvider> func)
  27. {
  28. var serviceProvider = BuildRealDbServiceProvider();
  29. var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
  30. transactionScope.BeginTransaction<LuUtilsDbContext>(null);
  31. try
  32. {
  33. func(serviceProvider);
  34. }
  35. finally
  36. {
  37. transactionScope.RollbackTransaction<LuUtilsDbContext>();
  38. }
  39. }
  40. public static void TestRealDb<TDataAccess>(Action<TDataAccess> func)
  41. {
  42. _TestRealDb(provider =>
  43. {
  44. func(provider.GetService<TDataAccess>());
  45. });
  46. }
  47. public static void TestRealDb<TDataAccess, TDataAccess2>(Action<TDataAccess, TDataAccess2> func)
  48. {
  49. _TestRealDb(provider =>
  50. {
  51. func(provider.GetService<TDataAccess>(), provider.GetService<TDataAccess2>());
  52. });
  53. }
  54. public static void TestRealFs(Action<LuFsFilesCrudDataAccess> func)
  55. {
  56. var tmp = Path.Combine(RealFsDir, "RealFsDir-" + new Random().Next());
  57. try
  58. {
  59. Directory.CreateDirectory(tmp);
  60. func(new LuTmpFsFilesCrudDataAccess(tmp));
  61. }
  62. finally
  63. {
  64. Directory.Delete(tmp, true);
  65. }
  66. }
  67. }
  68. }