|
1 | 1 | using System.Net; |
2 | 2 | using FluentAssertions; |
| 3 | +using Melodee.Common.Data; |
| 4 | +using Melodee.Common.Models.SearchEngines.ArtistSearchEngineServiceData; |
| 5 | +using Melodee.Common.Plugins.SearchEngine.MusicBrainz.Data; |
3 | 6 | using Microsoft.AspNetCore.Mvc.Testing; |
| 7 | +using Microsoft.EntityFrameworkCore; |
| 8 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Options; |
4 | 12 |
|
5 | 13 | namespace Melodee.Tests.Blazor; |
6 | 14 |
|
7 | | -public class ScalarIntegrationTests : IClassFixture<WebApplicationFactory<Program>> |
| 15 | +public class ScalarIntegrationTests : IAsyncLifetime |
8 | 16 | { |
9 | 17 | private readonly WebApplicationFactory<Program> _factory; |
10 | 18 |
|
11 | | - public ScalarIntegrationTests(WebApplicationFactory<Program> factory) |
| 19 | + public ScalarIntegrationTests() |
12 | 20 | { |
13 | | - _factory = factory; |
| 21 | + var inMemoryProvider = new ServiceCollection() |
| 22 | + .AddEntityFrameworkInMemoryDatabase() |
| 23 | + .BuildServiceProvider(); |
| 24 | + |
| 25 | + _factory = new WebApplicationFactory<Program>() |
| 26 | + .WithWebHostBuilder(builder => |
| 27 | + { |
| 28 | + builder.ConfigureAppConfiguration((_, config) => |
| 29 | + { |
| 30 | + var settings = new Dictionary<string, string?> |
| 31 | + { |
| 32 | + ["ConnectionStrings:DefaultConnection"] = "Host=localhost;Database=melodee_test;Username=test;Password=test", |
| 33 | + ["ConnectionStrings:ArtistSearchEngineConnection"] = "Data Source=:memory:", |
| 34 | + ["ConnectionStrings:MusicBrainzConnection"] = "Data Source=:memory:", |
| 35 | + ["Jwt:Key"] = new string('k', 64), |
| 36 | + ["Jwt:Issuer"] = "melodee-tests", |
| 37 | + ["Jwt:Audience"] = "melodee-tests", |
| 38 | + ["security.secretKey"] = new string('s', 32), |
| 39 | + ["QuartzDisabled"] = "true" |
| 40 | + }; |
| 41 | + |
| 42 | + config.AddInMemoryCollection(settings); |
| 43 | + }); |
| 44 | + |
| 45 | + builder.ConfigureServices(services => |
| 46 | + { |
| 47 | + var descriptors = services.Where(d => |
| 48 | + d.ServiceType == typeof(DbContextOptions<MelodeeDbContext>) || |
| 49 | + d.ServiceType == typeof(IDbContextFactory<MelodeeDbContext>) || |
| 50 | + d.ServiceType == typeof(IDbContextOptionsConfiguration<MelodeeDbContext>) || |
| 51 | + d.ServiceType == typeof(IConfigureOptions<DbContextOptions<MelodeeDbContext>>)) |
| 52 | + .ToList(); |
| 53 | + |
| 54 | + foreach (var descriptor in descriptors) |
| 55 | + { |
| 56 | + services.Remove(descriptor); |
| 57 | + } |
| 58 | + |
| 59 | + services.AddDbContextFactory<MelodeeDbContext>(options => |
| 60 | + { |
| 61 | + options.UseInMemoryDatabase("ScalarIntegrationTests"); |
| 62 | + options.UseInternalServiceProvider(inMemoryProvider); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public Task InitializeAsync() => Task.CompletedTask; |
| 69 | + |
| 70 | + public async Task DisposeAsync() |
| 71 | + { |
| 72 | + await _factory.DisposeAsync(); |
14 | 73 | } |
15 | 74 |
|
16 | 75 | [Fact] |
|
0 commit comments