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; }
|
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();
|
_database = Context.New();
|
||||||
|
|
||||||
ProviderRepository provider = new(_database);
|
ProviderRepository provider = new(_database);
|
||||||
|
@ -16,9 +16,9 @@ namespace Kyoo.Tests
|
|||||||
protected readonly RepositoryActivator Repositories;
|
protected readonly RepositoryActivator Repositories;
|
||||||
private readonly IRepository<T> _repository;
|
private readonly IRepository<T> _repository;
|
||||||
|
|
||||||
protected RepositoryTests()
|
protected RepositoryTests(RepositoryActivator repositories)
|
||||||
{
|
{
|
||||||
Repositories = new RepositoryActivator();
|
Repositories = repositories;
|
||||||
_repository = Repositories.LibraryManager.GetRepository<T>();
|
_repository = Repositories.LibraryManager.GetRepository<T>();
|
||||||
Repositories.Context.AddTest<Show>();
|
Repositories.Context.AddTest<Show>();
|
||||||
if (new T() is not Show)
|
if (new T() is not Show)
|
||||||
|
@ -5,11 +5,19 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Kyoo.Tests.SpecificTests
|
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;
|
private readonly ISeasonRepository _repository;
|
||||||
|
|
||||||
public SeasonTests()
|
protected SeasonTests(RepositoryActivator repositories)
|
||||||
|
: base(repositories)
|
||||||
{
|
{
|
||||||
_repository = Repositories.LibraryManager.SeasonRepository;
|
_repository = Repositories.LibraryManager.SeasonRepository;
|
||||||
}
|
}
|
||||||
@ -34,7 +42,7 @@ namespace Kyoo.Tests.SpecificTests
|
|||||||
{
|
{
|
||||||
Season season = await _repository.Get(1);
|
Season season = await _repository.Get(1);
|
||||||
Assert.Equal("anohana-s1", season.Slug);
|
Assert.Equal("anohana-s1", season.Slug);
|
||||||
season = await _repository.Edit(new Season
|
await _repository.Edit(new Season
|
||||||
{
|
{
|
||||||
ID = 1,
|
ID = 1,
|
||||||
SeasonNumber = 2
|
SeasonNumber = 2
|
||||||
|
@ -9,11 +9,27 @@ using Xunit;
|
|||||||
|
|
||||||
namespace Kyoo.Tests.SpecificTests
|
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;
|
private readonly IShowRepository _repository;
|
||||||
|
|
||||||
public ShowTests()
|
protected ShowTests(RepositoryActivator repositories)
|
||||||
|
: base(repositories)
|
||||||
{
|
{
|
||||||
_repository = Repositories.LibraryManager.ShowRepository;
|
_repository = Repositories.LibraryManager.ShowRepository;
|
||||||
}
|
}
|
||||||
|
@ -3,39 +3,90 @@ using System.Threading.Tasks;
|
|||||||
using Kyoo.SqLite;
|
using Kyoo.SqLite;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
namespace Kyoo.Tests
|
namespace Kyoo.Tests
|
||||||
{
|
{
|
||||||
/// <summary>
|
public sealed class SqLiteTestContext : TestContext
|
||||||
/// Class responsible to fill and create in memory databases for unit tests.
|
|
||||||
/// </summary>
|
|
||||||
public class TestContext : IDisposable, IAsyncDisposable
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// The context's options that specify to use an in memory Sqlite database.
|
|
||||||
/// </summary>
|
|
||||||
private readonly DbContextOptions<DatabaseContext> _context;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The internal sqlite connection used by all context returned by this class.
|
/// The internal sqlite connection used by all context returned by this class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly SqliteConnection _connection;
|
private readonly SqliteConnection _connection;
|
||||||
|
|
||||||
/// <summary>
|
public SqLiteTestContext()
|
||||||
/// Create a new database and fill it with information.
|
|
||||||
/// </summary>
|
|
||||||
public TestContext()
|
|
||||||
{
|
{
|
||||||
_connection = new SqliteConnection("DataSource=:memory:");
|
_connection = new SqliteConnection("DataSource=:memory:");
|
||||||
_connection.Open();
|
_connection.Open();
|
||||||
|
|
||||||
_context = new DbContextOptionsBuilder<DatabaseContext>()
|
Context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||||
.UseSqlite(_connection)
|
.UseSqlite(_connection)
|
||||||
.Options;
|
.Options;
|
||||||
|
|
||||||
using DatabaseContext context = New();
|
using DatabaseContext context = New();
|
||||||
context.Database.Migrate();
|
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>
|
/// <summary>
|
||||||
/// Fill the database with pre defined values using a clean 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.
|
/// Get a new database context connected to a in memory Sqlite database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A valid DatabaseContext</returns>
|
/// <returns>A valid DatabaseContext</returns>
|
||||||
public DatabaseContext New()
|
public abstract DatabaseContext New();
|
||||||
{
|
|
||||||
return new SqLiteContext(_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public abstract void Dispose();
|
||||||
{
|
|
||||||
_connection.Close();
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
public abstract ValueTask DisposeAsync();
|
||||||
{
|
|
||||||
await _connection.CloseAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user