Adding some tests

This commit is contained in:
Zoe Roux 2021-05-29 18:28:38 +02:00
parent d05e7a24b7
commit 81945b4e37
5 changed files with 250 additions and 96 deletions

14
Kyoo.Tests/KAssert.cs Normal file
View File

@ -0,0 +1,14 @@
using System.Reflection;
using Xunit;
namespace Kyoo.Tests
{
public static class KAssert
{
public static void DeepEqual<T>(T expected, T value)
{
foreach (PropertyInfo property in typeof(T).GetProperties())
Assert.Equal(property.GetValue(expected), property.GetValue(value));
}
}
}

View File

@ -0,0 +1,110 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Kyoo.Controllers;
using Kyoo.Models;
using Xunit;
namespace Kyoo.Tests
{
public class RepositoryActivator : IDisposable, IAsyncDisposable
{
public TestContext Context { get; init; }
public ILibraryManager LibraryManager { get; init; }
private readonly DatabaseContext _database;
public RepositoryActivator()
{
Context = new TestContext();
_database = Context.New();
ProviderRepository provider = new(_database);
LibraryRepository library = new(_database, provider);
CollectionRepository collection = new(_database);
GenreRepository genre = new(_database);
StudioRepository studio = new(_database);
PeopleRepository people = new(_database, provider,
new Lazy<IShowRepository>(() => LibraryManager.ShowRepository));
ShowRepository show = new(_database, studio, people, genre, provider,
new Lazy<ISeasonRepository>(() => LibraryManager.SeasonRepository),
new Lazy<IEpisodeRepository>(() => LibraryManager.EpisodeRepository));
SeasonRepository season = new(_database, provider, show,
new Lazy<IEpisodeRepository>(() => LibraryManager.EpisodeRepository));
LibraryItemRepository libraryItem = new(_database,
new Lazy<ILibraryRepository>(() => LibraryManager.LibraryRepository),
new Lazy<IShowRepository>(() => LibraryManager.ShowRepository),
new Lazy<ICollectionRepository>(() => LibraryManager.CollectionRepository));
TrackRepository track = new(_database);
EpisodeRepository episode = new(_database, provider, show, track);
LibraryManager = new LibraryManager(new IBaseRepository[] {
provider,
library,
libraryItem,
collection,
show,
season,
episode,
track,
people,
studio,
genre
});
Context.AddTest<Show>();
}
public void Dispose()
{
_database.Dispose();
Context.Dispose();
GC.SuppressFinalize(this);
}
public async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
await Context.DisposeAsync();
}
}
public abstract class RepositoryTests<T> : IClassFixture<RepositoryActivator>
where T : class, IResource
{
private readonly RepositoryActivator _repositories;
private readonly IRepository<T> _repository;
protected RepositoryTests(RepositoryActivator repositories)
{
_repositories = repositories;
_repository = _repositories.LibraryManager.GetRepository<T>();
}
// TODO test libraries & repositories via a on-memory SQLite database.
[Fact]
public async Task FillTest()
{
await using DatabaseContext database = _repositories.Context.New();
Assert.Equal(1, database.Shows.Count());
}
[Fact]
public async Task GetByIdTest()
{
T value = await _repository.Get(TestSample.Get<T>().Slug);
KAssert.DeepEqual(TestSample.Get<T>(), value);
}
}
public class ShowTests : RepositoryTests<Show>
{
public ShowTests(RepositoryActivator repositories)
: base(repositories)
{}
}
}

View File

@ -1,17 +0,0 @@
namespace Kyoo.Tests
{
public class SetupTests
{
// TODO test libraries & repositories via a on-memory SQLite database.
// TODO Requires: Kyoo should be database agonistic and database implementations should be available via a plugin.
// [Fact]
// public void Get_Test()
// {
// TestContext context = new();
// using DatabaseContext database = context.New();
//
// Assert.Equal(1, database.Shows.Count());
// }
}
}

View File

@ -1,79 +1,81 @@
// using Kyoo.Models; using System;
// using Microsoft.Data.Sqlite; using System.Threading.Tasks;
// using Microsoft.EntityFrameworkCore; using Kyoo.SqLite;
// using Microsoft.Data.Sqlite;
// namespace Kyoo.Tests using Microsoft.EntityFrameworkCore;
// {
// /// <summary> namespace Kyoo.Tests
// /// Class responsible to fill and create in memory databases for unit tests. {
// /// </summary> /// <summary>
// public class TestContext /// Class responsible to fill and create in memory databases for unit tests.
// { /// </summary>
// /// <summary> public class TestContext : IDisposable, IAsyncDisposable
// /// The context's options that specify to use an in memory Sqlite database. {
// /// </summary> /// <summary>
// private readonly DbContextOptions<DatabaseContext> _context; /// The context's options that specify to use an in memory Sqlite database.
// /// </summary>
// /// <summary> private readonly DbContextOptions<DatabaseContext> _context;
// /// Create a new database and fill it with information.
// /// </summary> /// <summary>
// public TestContext() /// The internal sqlite connection used by all context returned by this class.
// { /// </summary>
// SqliteConnection connection = new("DataSource=:memory:"); private readonly SqliteConnection _connection;
// connection.Open();
// /// <summary>
// try /// Create a new database and fill it with information.
// { /// </summary>
// _context = new DbContextOptionsBuilder<DatabaseContext>() public TestContext()
// .UseSqlite(connection) {
// .Options; _connection = new SqliteConnection("DataSource=:memory:");
// FillDatabase(); _connection.Open();
// }
// finally _context = new DbContextOptionsBuilder<DatabaseContext>()
// { .UseSqlite(_connection)
// connection.Close(); .Options;
// }
// } using DatabaseContext context = New();
// context.Database.Migrate();
// /// <summary> }
// /// Fill the database with pre defined values using a clean context.
// /// </summary> /// <summary>
// private void FillDatabase() /// Fill the database with pre defined values using a clean context.
// { /// </summary>
// using DatabaseContext context = new(_context); public async Task AddTestAsync<T>()
// context.Shows.Add(new Show where T : class
// { {
// ID = 67, await using DatabaseContext context = New();
// Slug = "anohana", await context.Set<T>().AddAsync(TestSample.Get<T>());
// Title = "Anohana: The Flower We Saw That Day", await context.SaveChangesAsync();
// Aliases = new[] }
// {
// "Ano Hi Mita Hana no Namae o Bokutachi wa Mada Shiranai.", /// <summary>
// "AnoHana", /// Fill the database with pre defined values using a clean context.
// "We Still Don't Know the Name of the Flower We Saw That Day." /// </summary>
// }, public void AddTest<T>()
// Overview = "When Yadomi Jinta was a child, he was a central piece in a group of close friends. " + where T : class
// "In time, however, these childhood friends drifted apart, and when they became high " + {
// "school students, they had long ceased to think of each other as friends.", using DatabaseContext context = New();
// Status = Status.Finished, context.Set<T>().Add(TestSample.Get<T>());
// TrailerUrl = null, context.SaveChanges();
// StartYear = 2011, }
// EndYear = 2011,
// Poster = "poster", /// <summary>
// Logo = "logo", /// Get a new database context connected to a in memory Sqlite database.
// Backdrop = "backdrop", /// </summary>
// IsMovie = false, /// <returns>A valid DatabaseContext</returns>
// Studio = null public DatabaseContext New()
// }); {
// } return new SqLiteContext(_context);
// }
// /// <summary>
// /// Get a new database context connected to a in memory Sqlite database. public void Dispose()
// /// </summary> {
// /// <returns>A valid DatabaseContext</returns> _connection.Close();
// public DatabaseContext New() }
// {
// return new(_context); public async ValueTask DisposeAsync()
// } {
// } await _connection.CloseAsync();
// } }
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Kyoo.Models;
namespace Kyoo.Tests
{
public static class TestSample
{
private static readonly Dictionary<Type, object> Samples = new()
{
{
typeof(Show),
new Show
{
ID = 1,
Slug = "anohana",
Title = "Anohana: The Flower We Saw That Day",
Aliases = new[]
{
"Ano Hi Mita Hana no Namae o Bokutachi wa Mada Shiranai.",
"AnoHana",
"We Still Don't Know the Name of the Flower We Saw That Day."
},
Overview = "When Yadomi Jinta was a child, he was a central piece in a group of close friends. " +
"In time, however, these childhood friends drifted apart, and when they became high " +
"school students, they had long ceased to think of each other as friends.",
Status = Status.Finished,
TrailerUrl = null,
StartYear = 2011,
EndYear = 2011,
Poster = "poster",
Logo = "logo",
Backdrop = "backdrop",
IsMovie = false,
Studio = null
}
}
};
public static T Get<T>()
{
return (T)Samples[typeof(T)];
}
}
}