mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding a postgresql test context
This commit is contained in:
parent
1bb29be134
commit
bf9c64b9d1
@ -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);
|
||||
|
@ -16,9 +16,9 @@ namespace Kyoo.Tests
|
||||
protected readonly RepositoryActivator Repositories;
|
||||
private readonly IRepository<T> _repository;
|
||||
|
||||
protected RepositoryTests()
|
||||
protected RepositoryTests(RepositoryActivator repositories)
|
||||
{
|
||||
Repositories = new RepositoryActivator();
|
||||
Repositories = repositories;
|
||||
_repository = Repositories.LibraryManager.GetRepository<T>();
|
||||
Repositories.Context.AddTest<Show>();
|
||||
if (new T() is not Show)
|
||||
|
@ -5,11 +5,19 @@ using Xunit;
|
||||
|
||||
namespace Kyoo.Tests.SpecificTests
|
||||
{
|
||||
public class SeasonTests : RepositoryTests<Season>
|
||||
public class SqLiteSeasonTests : SeasonTests
|
||||
{
|
||||
public SqLiteSeasonTests()
|
||||
: base(new RepositoryActivator(true))
|
||||
{ }
|
||||
}
|
||||
|
||||
public abstract class SeasonTests : RepositoryTests<Season>
|
||||
{
|
||||
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
|
||||
|
@ -9,11 +9,27 @@ using Xunit;
|
||||
|
||||
namespace Kyoo.Tests.SpecificTests
|
||||
{
|
||||
public class ShowTests : RepositoryTests<Show>
|
||||
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<Show>
|
||||
{
|
||||
private readonly IShowRepository _repository;
|
||||
|
||||
public ShowTests()
|
||||
protected ShowTests(RepositoryActivator repositories)
|
||||
: base(repositories)
|
||||
{
|
||||
_repository = Repositories.LibraryManager.ShowRepository;
|
||||
}
|
||||
|
@ -3,39 +3,90 @@ using System.Threading.Tasks;
|
||||
using Kyoo.SqLite;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace Kyoo.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class responsible to fill and create in memory databases for unit tests.
|
||||
/// </summary>
|
||||
public class TestContext : IDisposable, IAsyncDisposable
|
||||
public sealed class SqLiteTestContext : TestContext
|
||||
{
|
||||
/// <summary>
|
||||
/// The context's options that specify to use an in memory Sqlite database.
|
||||
/// </summary>
|
||||
private readonly DbContextOptions<DatabaseContext> _context;
|
||||
|
||||
/// <summary>
|
||||
/// The internal sqlite connection used by all context returned by this class.
|
||||
/// </summary>
|
||||
private readonly SqliteConnection _connection;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new database and fill it with information.
|
||||
/// </summary>
|
||||
public TestContext()
|
||||
|
||||
public SqLiteTestContext()
|
||||
{
|
||||
_connection = new SqliteConnection("DataSource=:memory:");
|
||||
_connection.Open();
|
||||
|
||||
_context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
|
||||
Context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
.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<PostgresFixture>
|
||||
{}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Class responsible to fill and create in memory databases for unit tests.
|
||||
/// </summary>
|
||||
public abstract class TestContext : IDisposable, IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The context's options that specify to use an in memory Sqlite database.
|
||||
/// </summary>
|
||||
protected DbContextOptions<DatabaseContext> Context;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <returns>A valid DatabaseContext</returns>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user