Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Tests.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using Luticate2.Auth.Controllers;
  3. using Luticate2.Auth.DataAccess;
  4. using Luticate2.Utils.DataAccess;
  5. using Luticate2.Utils.Interfaces;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.DependencyInjection;
  8. namespace TestAuth
  9. {
  10. public class DummyNotificationsBusiness : ILuNotificationsBusiness
  11. {
  12. public void Notify(string eventName, string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
  13. {
  14. }
  15. public void NotifyCreate(string entityType, object newEntity, Func<string, bool> filter = null)
  16. {
  17. }
  18. public void NotifyUpdate(string entityType, object oldEntity, object newEntity, Func<string, bool> filter = null)
  19. {
  20. }
  21. public void NotifyDelete(string entityType, object oldEntity, Func<string, bool> filter = null)
  22. {
  23. }
  24. }
  25. public class Tests
  26. {
  27. public const string RealDbConnectionString =
  28. "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2;Pooling=true;";
  29. public static IServiceProvider BuildRealDbServiceProvider()
  30. {
  31. IServiceCollection serviceCollection = new ServiceCollection();
  32. serviceCollection.AddSingleton<ILuNotificationsBusiness, DummyNotificationsBusiness>();
  33. serviceCollection.AddLuticateAuth(dbo =>
  34. {
  35. dbo.Version = "tests";
  36. }, builder =>
  37. {
  38. builder.UseNpgsql(RealDbConnectionString);
  39. });
  40. return serviceCollection.BuildServiceProvider();
  41. }
  42. protected static void _TestRealDb(Action<IServiceProvider> func)
  43. {
  44. var serviceProvider = BuildRealDbServiceProvider();
  45. var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
  46. transactionScope.BeginTransaction<LuAuthDatabaseContext>(null);
  47. try
  48. {
  49. func(serviceProvider);
  50. }
  51. finally
  52. {
  53. transactionScope.RollbackTransaction<LuAuthDatabaseContext>();
  54. }
  55. }
  56. public static void TestRealDb<TService>(Action<TService> func)
  57. {
  58. _TestRealDb(provider =>
  59. {
  60. func(provider.GetService<TService>());
  61. });
  62. }
  63. }
  64. }