Fix tests

This commit is contained in:
Zoe Roux 2023-11-27 02:49:08 +01:00
parent ee4cc6706e
commit 2b0f6837a8
3 changed files with 24 additions and 51 deletions

View File

@ -19,6 +19,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Design; using System.ComponentModel.Design;
using System.Data.Common;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
@ -37,7 +38,7 @@ namespace Kyoo.Tests.Database
public TestContext Context { get; } public TestContext Context { get; }
public ILibraryManager LibraryManager { get; } public ILibraryManager LibraryManager { get; }
private readonly List<DatabaseContext> _databases = new(); private readonly List<IAsyncDisposable> _databases = new();
private readonly IBaseRepository[] _repositories; private readonly IBaseRepository[] _repositories;
@ -54,7 +55,7 @@ namespace Kyoo.Tests.Database
MovieRepository movies = new(_NewContext(), studio, people, thumbs.Object); MovieRepository movies = new(_NewContext(), studio, people, thumbs.Object);
ShowRepository show = new(_NewContext(), studio, people, thumbs.Object); ShowRepository show = new(_NewContext(), studio, people, thumbs.Object);
SeasonRepository season = new(_NewContext(), thumbs.Object); SeasonRepository season = new(_NewContext(), thumbs.Object);
LibraryItemRepository libraryItem = new(_NewContext()); LibraryItemRepository libraryItem = new(_NewConnection());
EpisodeRepository episode = new(_NewContext(), show, thumbs.Object); EpisodeRepository episode = new(_NewContext(), show, thumbs.Object);
UserRepository user = new(_NewContext(), thumbs.Object); UserRepository user = new(_NewContext(), thumbs.Object);
@ -101,9 +102,16 @@ namespace Kyoo.Tests.Database
return context; return context;
} }
private DbConnection _NewConnection()
{
DbConnection context = Context.NewConnection();
_databases.Add(context);
return context;
}
public void Dispose() public void Dispose()
{ {
foreach (DatabaseContext context in _databases) foreach (IDisposable context in _databases)
context.Dispose(); context.Dispose();
Context.Dispose(); Context.Dispose();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@ -111,7 +119,7 @@ namespace Kyoo.Tests.Database
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()
{ {
foreach (DatabaseContext context in _databases) foreach (IAsyncDisposable context in _databases)
await context.DisposeAsync(); await context.DisposeAsync();
await Context.DisposeAsync(); await Context.DisposeAsync();
} }

View File

@ -17,7 +17,6 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
@ -133,54 +132,11 @@ namespace Kyoo.Tests.Database
// await Assert.ThrowsAsync<ItemNotFoundException>(() => _repository.Edit(new T { Id = 56 })); // await Assert.ThrowsAsync<ItemNotFoundException>(() => _repository.Edit(new T { Id = 56 }));
// } // }
[Fact]
public async Task GetExpressionIDTest()
{
KAssert.DeepEqual(TestSample.Get<T>(), await _repository.Get(x => x.Id == TestSample.Get<T>().Id));
}
[Fact]
public async Task GetExpressionSlugTest()
{
KAssert.DeepEqual(TestSample.Get<T>(), await _repository.Get(x => x.Slug == TestSample.Get<T>().Slug));
}
[Fact]
public async Task GetExpressionNotFoundTest()
{
await Assert.ThrowsAsync<ItemNotFoundException>(() => _repository.Get(x => x.Slug == "non-existing"));
}
[Fact] [Fact]
public async Task GetOrDefaultTest() public async Task GetOrDefaultTest()
{ {
Assert.Null(await _repository.GetOrDefault(56)); Assert.Null(await _repository.GetOrDefault(56));
Assert.Null(await _repository.GetOrDefault("non-existing")); Assert.Null(await _repository.GetOrDefault("non-existing"));
Assert.Null(await _repository.GetOrDefault(x => x.Slug == "non-existing"));
}
[Fact]
public async Task GetCountWithFilterTest()
{
string slug = TestSample.Get<T>().Slug[2..4];
Assert.Equal(1, await _repository.GetCount(x => x.Slug.Contains(slug)));
}
[Fact]
public async Task GetAllTest()
{
string slug = TestSample.Get<T>().Slug[2..4];
ICollection<T> ret = await _repository.GetAll(x => x.Slug.Contains(slug));
Assert.Single(ret);
KAssert.DeepEqual(TestSample.Get<T>(), ret.First());
}
[Fact]
public async Task DeleteAllTest()
{
string slug = TestSample.Get<T>().Slug[2..4];
await _repository.DeleteAll(x => x.Slug.Contains(slug));
Assert.Equal(0, await _repository.GetCount());
} }
} }
} }

View File

@ -17,6 +17,7 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Data.Common;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Postgresql; using Kyoo.Postgresql;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -68,22 +69,23 @@ namespace Kyoo.Tests
public sealed class PostgresTestContext : TestContext public sealed class PostgresTestContext : TestContext
{ {
private readonly string _database;
private readonly DbContextOptions<DatabaseContext> _context; private readonly DbContextOptions<DatabaseContext> _context;
public PostgresTestContext(PostgresFixture template, ITestOutputHelper output) public PostgresTestContext(PostgresFixture template, ITestOutputHelper output)
{ {
string id = Guid.NewGuid().ToString().Replace('-', '_'); string id = Guid.NewGuid().ToString().Replace('-', '_');
string database = $"kyoo_test_{id}"; _database = $"kyoo_test_{id}";
using (NpgsqlConnection connection = new(template.Connection)) using (NpgsqlConnection connection = new(template.Connection))
{ {
connection.Open(); connection.Open();
using NpgsqlCommand cmd = new($"CREATE DATABASE {database} WITH TEMPLATE {template.Template}", connection); using NpgsqlCommand cmd = new($"CREATE DATABASE {_database} WITH TEMPLATE {template.Template}", connection);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
_context = new DbContextOptionsBuilder<DatabaseContext>() _context = new DbContextOptionsBuilder<DatabaseContext>()
.UseNpgsql(GetConnectionString(database)) .UseNpgsql(GetConnectionString(_database))
.UseLoggerFactory(LoggerFactory.Create(x => .UseLoggerFactory(LoggerFactory.Create(x =>
{ {
x.ClearProviders(); x.ClearProviders();
@ -119,6 +121,11 @@ namespace Kyoo.Tests
{ {
return new PostgresContext(_context); return new PostgresContext(_context);
} }
public override DbConnection NewConnection()
{
return new NpgsqlConnection(GetConnectionString(_database));
}
} }
/// <summary> /// <summary>
@ -154,6 +161,8 @@ namespace Kyoo.Tests
/// <returns>A valid DatabaseContext</returns> /// <returns>A valid DatabaseContext</returns>
public abstract DatabaseContext New(); public abstract DatabaseContext New();
public abstract DbConnection NewConnection();
public abstract void Dispose(); public abstract void Dispose();
public abstract ValueTask DisposeAsync(); public abstract ValueTask DisposeAsync();