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.3KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Linq;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using TestUtils.DataAccess;
  6. namespace TestUtils
  7. {
  8. public class Tests
  9. {
  10. public const string ConnectionString =
  11. "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
  12. public static void TestRealDb(Action<IServiceProvider> func)
  13. {
  14. IServiceCollection serviceCollection = new ServiceCollection();
  15. serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
  16. ServiceLifetime.Transient);
  17. var serviceProvider = serviceCollection.BuildServiceProvider();
  18. try
  19. {
  20. func(serviceProvider);
  21. }
  22. finally
  23. {
  24. var dbContext = (LuUtilsDbContext)serviceProvider.GetService(typeof(LuUtilsDbContext));
  25. dbContext.pk_bigserial.RemoveRange(dbContext.pk_bigserial);
  26. dbContext.pk_guid.RemoveRange(dbContext.pk_guid);
  27. dbContext.fk_pk_guids.RemoveRange(dbContext.fk_pk_guids);
  28. dbContext.SaveChanges();
  29. dbContext.Dispose();
  30. }
  31. }
  32. }
  33. }