diff --git a/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs b/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs index add2e07b..aeaab07f 100644 --- a/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs +++ b/AsbCloudWebApi.IntegrationTests/BaseIntegrationTest.cs @@ -4,15 +4,23 @@ using Xunit; namespace AsbCloudWebApi.IntegrationTests; -public abstract class BaseIntegrationTest : IClassFixture +public abstract class BaseIntegrationTest : IClassFixture, + IDisposable { protected readonly IServiceScope scope; - - protected readonly IAsbCloudDbContext dbContext; + + protected readonly AsbCloudDbContext dbContext; protected BaseIntegrationTest(WebAppFactoryFixture factory) { scope = factory.Services.CreateScope(); - dbContext = scope.ServiceProvider.GetRequiredService(); + + dbContext = scope.ServiceProvider.GetRequiredService(); + } + + public void Dispose() + { + scope.Dispose(); + dbContext.Dispose(); } } \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs index 4142705c..a3667662 100644 --- a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs +++ b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs @@ -2,110 +2,55 @@ namespace AsbCloudWebApi.IntegrationTests.Data { - [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2211:Поля, не являющиеся константами, не должны быть видимыми", Justification = "<Ожидание>")] - public static class Defaults - { - public static Driller[] Drillers = new Driller[] - { - new() - { - Id = 1, - Name = "test1", - Surname = "test1", - Patronymic = "test1" - }, - new() - { - Id = 2, - Name = "test2", - Surname = "test2", - Patronymic = "test2" - } - }; + [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2211:Поля, не являющиеся константами, не должны быть видимыми", + Justification = "<Ожидание>")] + public static class Defaults + { + public static SimpleTimezone Timezone => new() + { + Hours = 1 + }; - public static WellOperation[] WellOperations = new WellOperation[] - { - new() - { - Id = 1, - IdWell = 1, - IdType = 1, - DateStart = DateTimeOffset.UtcNow.AddDays(-1), - CategoryInfo = "1", - Comment = "1", - DepthEnd = 20, - DepthStart = 10, - DurationHours = 1, - IdCategory = 5000, - IdPlan = null, - IdUser = 1, - IdWellSectionType = 1, - LastUpdateDate = DateTimeOffset.UtcNow - } - }; - - public static Deposit[] Deposits = new Deposit[] { - new() - { - Id = 1, - Caption = "Deposit1", - Latitude = 10, - Longitude = 20, - Timezone = GetTimezone() - } - }; - - public static Cluster[] Clusters = new Cluster[] { - new() - { - Id = 1, - IdDeposit = Deposits[0].Id, - Caption = "Cluster1", - Latitude = 10, - Longitude = 20, - Timezone = GetTimezone() - } - }; - - public static Telemetry[] Telemetries = new Telemetry[] - { - new() - { - Id = 1, - RemoteUid = "555-555-555", - TimeZone = GetTimezone() - } - }; - - public static Well[] Wells = new Well[] { - new() - { - Id = 1, - IdCluster = Clusters[0].Id, - IdWellType = 1, - IdState = 1, - Caption = "Well1", - Latitude = 10, - Longitude = 20, - IdTelemetry = Telemetries[0].Id, - Timezone = GetTimezone() - } - }; - - public static RelationCompanyWell[] RelationsCompanyWell = new RelationCompanyWell[] - { - new() { IdCompany = 1, IdWell = Wells[0].Id }, - }; - - public static RelationUserUserRole[] RelationsUserUserRole = new RelationUserUserRole[] - { - new(){ IdUserRole= 1, IdUser = 1} - }; - - private static SimpleTimezone GetTimezone() => - new () - { - Hours = 1 - }; - } -} + public static Deposit[] Deposits => new Deposit[] + { + new() + { + Caption = "Deposit1", + Latitude = 10, + Longitude = 20, + Timezone = Timezone, + Clusters = new[] + { + new Cluster + { + Caption = "Cluster1", + Latitude = 10, + Longitude = 20, + Timezone = Timezone, + Wells = new[] + { + new Well + { + IdWellType = 1, + IdState = 1, + Caption = "Well1", + Latitude = 10, + Longitude = 20, + Timezone = Timezone, + Telemetry = new Telemetry + { + RemoteUid = "555-555-555", + TimeZone = Timezone + }, + RelationCompaniesWells = new RelationCompanyWell[] + { + new() { IdCompany = 1 }, + }, + } + } + } + } + } + }; + } +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/DbConnection.cs b/AsbCloudWebApi.IntegrationTests/DbConnection.cs new file mode 100644 index 00000000..fad06ed8 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/DbConnection.cs @@ -0,0 +1,15 @@ +namespace AsbCloudWebApi.IntegrationTests; + +public class DbConnection +{ + public string Host { get; set; } = null!; + + public int Port { get; set; } + + public string Username { get; set; } = null!; + + public string Password { get; set; } = null!; + + public string GetConnectionString() => + $"Host={Host};Database={Guid.NewGuid()};Port={Port};Username={Username};Password={Password};Persist Security Info=True;Include Error Detail=True"; +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs index 44bd4b67..b990295b 100644 --- a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs +++ b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs @@ -1,4 +1,3 @@ -using AsbCloudDb; using AsbCloudDb.Model; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; @@ -8,87 +7,80 @@ using Microsoft.Extensions.DependencyInjection; using Refit; using System.Net.Http.Headers; using System.Text.Json; +using AsbCloudDb; using AsbCloudWebApi.IntegrationTests.Converters; -using Xunit; namespace AsbCloudWebApi.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory, - IAsyncLifetime + IDisposable { - private static readonly JsonSerializerOptions jsonSerializerOptions = new() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - PropertyNameCaseInsensitive = true, + private static readonly JsonSerializerOptions JsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, Converters = { new ValidationResultConverter() } - }; + }; - private static readonly RefitSettings refitSettings = new RefitSettings(new SystemTextJsonContentSerializer(jsonSerializerOptions)); + private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); - protected override void ConfigureWebHost(IWebHostBuilder builder) + protected override void ConfigureWebHost(IWebHostBuilder builder) { var configuration = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") + .AddJsonFile("appsettings.Tests.json") .Build(); - var connectionString = configuration.GetConnectionString("TestConnection")!; - builder.ConfigureServices(services => + var dbConnection = configuration.GetSection("DbConnection").Get(); + var connectionString = dbConnection?.GetConnectionString(); + + builder.ConfigureServices(services => { - var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(DbContextOptions)); + var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); - if (descriptor is not null) + if (descriptor != null) services.Remove(descriptor); services.AddDbContext(options => options.UseNpgsql(connectionString)); + + var serviceProvider = services.BuildServiceProvider(); + + using var scope = serviceProvider.CreateScope(); + var scopedServices = scope.ServiceProvider; + var dbContext = scopedServices.GetRequiredService(); + + dbContext.Database.EnsureCreatedAndMigrated(); + dbContext.Deposits.AddRange(Data.Defaults.Deposits); + dbContext.SaveChanges(); }); } - public async Task InitializeAsync() + public new void Dispose() { using var scope = Services.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); - dbContext.Database.EnsureDeleted(); - dbContext.Database.EnsureCreatedAndMigrated(); - await FillBaseDatasets(dbContext); + + dbContext.Database.EnsureDeleted(); } - private static async Task FillBaseDatasets(AsbCloudDbContext dbContext) - { - dbContext.AddRange(Data.Defaults.Deposits); - dbContext.AddRange(Data.Defaults.Clusters); - dbContext.AddRange(Data.Defaults.Wells); - dbContext.AddRange(Data.Defaults.RelationsCompanyWell); - dbContext.AddRange(Data.Defaults.Telemetries); - dbContext.AddRange(Data.Defaults.Drillers); - dbContext.AddRange(Data.Defaults.WellOperations); - await dbContext.SaveChangesAsync(); - } - - public new async Task DisposeAsync() + public T GetAuthorizedHttpClient(string uriSuffix) { - using var scope = Services.CreateScope(); - var scopedServices = scope.ServiceProvider; - var dbContext = scopedServices.GetRequiredService(); - - await dbContext.Database.EnsureDeletedAsync(); - } + var httpClient = GetAuthorizedHttpClient(); + if (string.IsNullOrEmpty(uriSuffix)) + return RestService.For(httpClient, RefitSettings); - public HttpClient GetAuthorizedHttpClient() - { - var httpClient = CreateClient(); - var jwtToken = ApiTokenHelper.GetAdminUserToken(); - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); - return httpClient; - } + if (httpClient.BaseAddress is not null) + httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix); - public T GetAuthorizedHttpClient(string uriSuffix) - { - var httpClient = GetAuthorizedHttpClient(); - if (!string.IsNullOrEmpty(uriSuffix)) - if(httpClient.BaseAddress is not null) - httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix); - return RestService.For(httpClient, refitSettings); - } + return RestService.For(httpClient, RefitSettings); + } + + private HttpClient GetAuthorizedHttpClient() + { + var httpClient = CreateClient(); + var jwtToken = ApiTokenHelper.GetAdminUserToken(); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); + return httpClient; + } } \ No newline at end of file diff --git a/AsbCloudWebApi/appsettings.Tests.json b/AsbCloudWebApi/appsettings.Tests.json new file mode 100644 index 00000000..d04cf2b1 --- /dev/null +++ b/AsbCloudWebApi/appsettings.Tests.json @@ -0,0 +1,8 @@ +{ + "DbConnection": { + "Host": "localhost", + "Port": 5433, + "Username": "postgres", + "Password": "root" + } +} \ No newline at end of file