From bf9c64b9d137c322459a0cb78998a7daa1e3912b Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 15 Jun 2021 23:16:05 +0200 Subject: [PATCH] Adding a postgresql test context --- Kyoo.Tests/Library/RepositoryActivator.cs | 8 +- Kyoo.Tests/Library/RepositoryTests.cs | 4 +- .../Library/SpecificTests/SeasonTests.cs | 14 ++- Kyoo.Tests/Library/SpecificTests/ShowTests.cs | 20 +++- Kyoo.Tests/Library/TestContext.cs | 99 +++++++++++++------ 5 files changed, 106 insertions(+), 39 deletions(-) diff --git a/Kyoo.Tests/Library/RepositoryActivator.cs b/Kyoo.Tests/Library/RepositoryActivator.cs index df0be918..9f7f78fb 100644 --- a/Kyoo.Tests/Library/RepositoryActivator.cs +++ b/Kyoo.Tests/Library/RepositoryActivator.cs @@ -10,11 +10,13 @@ namespace Kyoo.Tests public ILibraryManager LibraryManager { get; } - private readonly DatabaseContext _database; + private readonly DatabaseContext _database; - public RepositoryActivator() + public RepositoryActivator(PostgresFixture postgres = null) { - Context = new TestContext(); + Context = postgres == null + ? new SqLiteTestContext() + : new PostgresTestContext(postgres); _database = Context.New(); ProviderRepository provider = new(_database); diff --git a/Kyoo.Tests/Library/RepositoryTests.cs b/Kyoo.Tests/Library/RepositoryTests.cs index 013eb6f2..78f52cb5 100644 --- a/Kyoo.Tests/Library/RepositoryTests.cs +++ b/Kyoo.Tests/Library/RepositoryTests.cs @@ -16,9 +16,9 @@ namespace Kyoo.Tests protected readonly RepositoryActivator Repositories; private readonly IRepository _repository; - protected RepositoryTests() + protected RepositoryTests(RepositoryActivator repositories) { - Repositories = new RepositoryActivator(); + Repositories = repositories; _repository = Repositories.LibraryManager.GetRepository(); Repositories.Context.AddTest(); if (new T() is not Show) diff --git a/Kyoo.Tests/Library/SpecificTests/SeasonTests.cs b/Kyoo.Tests/Library/SpecificTests/SeasonTests.cs index 07db9d97..04a493a9 100644 --- a/Kyoo.Tests/Library/SpecificTests/SeasonTests.cs +++ b/Kyoo.Tests/Library/SpecificTests/SeasonTests.cs @@ -5,11 +5,19 @@ using Xunit; namespace Kyoo.Tests.SpecificTests { - public class SeasonTests : RepositoryTests + public class SqLiteSeasonTests : SeasonTests + { + public SqLiteSeasonTests() + : base(new RepositoryActivator(true)) + { } + } + + public abstract class SeasonTests : RepositoryTests { private readonly ISeasonRepository _repository; - public SeasonTests() + protected SeasonTests(RepositoryActivator repositories) + : base(repositories) { _repository = Repositories.LibraryManager.SeasonRepository; } @@ -34,7 +42,7 @@ namespace Kyoo.Tests.SpecificTests { Season season = await _repository.Get(1); Assert.Equal("anohana-s1", season.Slug); - season = await _repository.Edit(new Season + await _repository.Edit(new Season { ID = 1, SeasonNumber = 2 diff --git a/Kyoo.Tests/Library/SpecificTests/ShowTests.cs b/Kyoo.Tests/Library/SpecificTests/ShowTests.cs index 9c0b5138..ea2146fc 100644 --- a/Kyoo.Tests/Library/SpecificTests/ShowTests.cs +++ b/Kyoo.Tests/Library/SpecificTests/ShowTests.cs @@ -9,11 +9,27 @@ using Xunit; namespace Kyoo.Tests.SpecificTests { - public class ShowTests : RepositoryTests + public class SqLiteShowTests : ShowTests + { + public SqLiteShowTests() + : base(new RepositoryActivator(null)) + { } + } + + [Collection(nameof(Postgresql))] + public class PostgresShowTests : ShowTests + { + public PostgresShowTests(PostgresFixture postgres) + : base(new RepositoryActivator(postgres)) + { } + } + + public abstract class ShowTests : RepositoryTests { private readonly IShowRepository _repository; - public ShowTests() + protected ShowTests(RepositoryActivator repositories) + : base(repositories) { _repository = Repositories.LibraryManager.ShowRepository; } diff --git a/Kyoo.Tests/Library/TestContext.cs b/Kyoo.Tests/Library/TestContext.cs index 30f8beb5..8a1d1a64 100644 --- a/Kyoo.Tests/Library/TestContext.cs +++ b/Kyoo.Tests/Library/TestContext.cs @@ -3,39 +3,90 @@ using System.Threading.Tasks; using Kyoo.SqLite; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using Xunit; namespace Kyoo.Tests { - /// - /// Class responsible to fill and create in memory databases for unit tests. - /// - public class TestContext : IDisposable, IAsyncDisposable + public sealed class SqLiteTestContext : TestContext { - /// - /// The context's options that specify to use an in memory Sqlite database. - /// - private readonly DbContextOptions _context; - /// /// The internal sqlite connection used by all context returned by this class. /// private readonly SqliteConnection _connection; - - /// - /// Create a new database and fill it with information. - /// - public TestContext() + + public SqLiteTestContext() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); - - _context = new DbContextOptionsBuilder() + + Context = new DbContextOptionsBuilder() .UseSqlite(_connection) .Options; using DatabaseContext context = New(); context.Database.Migrate(); } + + public override void Dispose() + { + _connection.Close(); + } + + public override async ValueTask DisposeAsync() + { + await _connection.CloseAsync(); + } + + public override DatabaseContext New() + { + return new SqLiteContext(Context); + } + } + + [CollectionDefinition(nameof(Postgresql))] + public class PostgresCollection : ICollectionFixture + {} + + public class PostgresFixture + { + + } + + public sealed class PostgresTestContext : TestContext + { + private readonly PostgresFixture _template; + + public PostgresTestContext(PostgresFixture template) + { + _template = template; + } + + public override void Dispose() + { + throw new NotImplementedException(); + } + + public override ValueTask DisposeAsync() + { + throw new NotImplementedException(); + } + + public override DatabaseContext New() + { + throw new NotImplementedException(); + } + } + + + /// + /// Class responsible to fill and create in memory databases for unit tests. + /// + public abstract class TestContext : IDisposable, IAsyncDisposable + { + /// + /// The context's options that specify to use an in memory Sqlite database. + /// + protected DbContextOptions Context; /// /// Fill the database with pre defined values using a clean context. @@ -85,20 +136,10 @@ namespace Kyoo.Tests /// Get a new database context connected to a in memory Sqlite database. /// /// A valid DatabaseContext - public DatabaseContext New() - { - return new SqLiteContext(_context); - } + public abstract DatabaseContext New(); - public void Dispose() - { - _connection.Close(); - GC.SuppressFinalize(this); - } + public abstract void Dispose(); - public async ValueTask DisposeAsync() - { - await _connection.CloseAsync(); - } + public abstract ValueTask DisposeAsync(); } }