From 81945b4e370c3e407aa3f356f542fcdf0e716d89 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 29 May 2021 18:28:38 +0200 Subject: [PATCH] Adding some tests --- Kyoo.Tests/KAssert.cs | 14 +++ Kyoo.Tests/Library/RepositoryTests.cs | 110 ++++++++++++++++++ Kyoo.Tests/Library/SetupTests.cs | 17 --- Kyoo.Tests/Library/TestContext.cs | 160 +++++++++++++------------- Kyoo.Tests/Library/TestSample.cs | 45 ++++++++ 5 files changed, 250 insertions(+), 96 deletions(-) create mode 100644 Kyoo.Tests/KAssert.cs create mode 100644 Kyoo.Tests/Library/RepositoryTests.cs delete mode 100644 Kyoo.Tests/Library/SetupTests.cs create mode 100644 Kyoo.Tests/Library/TestSample.cs diff --git a/Kyoo.Tests/KAssert.cs b/Kyoo.Tests/KAssert.cs new file mode 100644 index 00000000..b6168251 --- /dev/null +++ b/Kyoo.Tests/KAssert.cs @@ -0,0 +1,14 @@ +using System.Reflection; +using Xunit; + +namespace Kyoo.Tests +{ + public static class KAssert + { + public static void DeepEqual(T expected, T value) + { + foreach (PropertyInfo property in typeof(T).GetProperties()) + Assert.Equal(property.GetValue(expected), property.GetValue(value)); + } + } +} \ No newline at end of file diff --git a/Kyoo.Tests/Library/RepositoryTests.cs b/Kyoo.Tests/Library/RepositoryTests.cs new file mode 100644 index 00000000..9261d56f --- /dev/null +++ b/Kyoo.Tests/Library/RepositoryTests.cs @@ -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(() => LibraryManager.ShowRepository)); + ShowRepository show = new(_database, studio, people, genre, provider, + new Lazy(() => LibraryManager.SeasonRepository), + new Lazy(() => LibraryManager.EpisodeRepository)); + SeasonRepository season = new(_database, provider, show, + new Lazy(() => LibraryManager.EpisodeRepository)); + LibraryItemRepository libraryItem = new(_database, + new Lazy(() => LibraryManager.LibraryRepository), + new Lazy(() => LibraryManager.ShowRepository), + new Lazy(() => 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(); + } + + public void Dispose() + { + _database.Dispose(); + Context.Dispose(); + GC.SuppressFinalize(this); + } + + public async ValueTask DisposeAsync() + { + await _database.DisposeAsync(); + await Context.DisposeAsync(); + } + } + + + public abstract class RepositoryTests : IClassFixture + where T : class, IResource + { + private readonly RepositoryActivator _repositories; + private readonly IRepository _repository; + + protected RepositoryTests(RepositoryActivator repositories) + { + _repositories = repositories; + _repository = _repositories.LibraryManager.GetRepository(); + } + + // 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().Slug); + KAssert.DeepEqual(TestSample.Get(), value); + } + } + + public class ShowTests : RepositoryTests + { + public ShowTests(RepositoryActivator repositories) + : base(repositories) + {} + } +} \ No newline at end of file diff --git a/Kyoo.Tests/Library/SetupTests.cs b/Kyoo.Tests/Library/SetupTests.cs deleted file mode 100644 index ec9ed12a..00000000 --- a/Kyoo.Tests/Library/SetupTests.cs +++ /dev/null @@ -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()); - // } - } -} \ No newline at end of file diff --git a/Kyoo.Tests/Library/TestContext.cs b/Kyoo.Tests/Library/TestContext.cs index e3cabc03..50db0b6b 100644 --- a/Kyoo.Tests/Library/TestContext.cs +++ b/Kyoo.Tests/Library/TestContext.cs @@ -1,79 +1,81 @@ -// using Kyoo.Models; -// using Microsoft.Data.Sqlite; -// using Microsoft.EntityFrameworkCore; -// -// namespace Kyoo.Tests -// { -// /// -// /// Class responsible to fill and create in memory databases for unit tests. -// /// -// public class TestContext -// { -// /// -// /// The context's options that specify to use an in memory Sqlite database. -// /// -// private readonly DbContextOptions _context; -// -// /// -// /// Create a new database and fill it with information. -// /// -// public TestContext() -// { -// SqliteConnection connection = new("DataSource=:memory:"); -// connection.Open(); -// -// try -// { -// _context = new DbContextOptionsBuilder() -// .UseSqlite(connection) -// .Options; -// FillDatabase(); -// } -// finally -// { -// connection.Close(); -// } -// } -// -// /// -// /// Fill the database with pre defined values using a clean context. -// /// -// private void FillDatabase() -// { -// using DatabaseContext context = new(_context); -// context.Shows.Add(new Show -// { -// ID = 67, -// 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 -// }); -// } -// -// /// -// /// Get a new database context connected to a in memory Sqlite database. -// /// -// /// A valid DatabaseContext -// public DatabaseContext New() -// { -// return new(_context); -// } -// } -// } \ No newline at end of file +using System; +using System.Threading.Tasks; +using Kyoo.SqLite; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace Kyoo.Tests +{ + /// + /// Class responsible to fill and create in memory databases for unit tests. + /// + public class TestContext : IDisposable, IAsyncDisposable + { + /// + /// 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() + { + _connection = new SqliteConnection("DataSource=:memory:"); + _connection.Open(); + + _context = new DbContextOptionsBuilder() + .UseSqlite(_connection) + .Options; + + using DatabaseContext context = New(); + context.Database.Migrate(); + } + + /// + /// Fill the database with pre defined values using a clean context. + /// + public async Task AddTestAsync() + where T : class + { + await using DatabaseContext context = New(); + await context.Set().AddAsync(TestSample.Get()); + await context.SaveChangesAsync(); + } + + /// + /// Fill the database with pre defined values using a clean context. + /// + public void AddTest() + where T : class + { + using DatabaseContext context = New(); + context.Set().Add(TestSample.Get()); + context.SaveChanges(); + } + + /// + /// Get a new database context connected to a in memory Sqlite database. + /// + /// A valid DatabaseContext + public DatabaseContext New() + { + return new SqLiteContext(_context); + } + + public void Dispose() + { + _connection.Close(); + } + + public async ValueTask DisposeAsync() + { + await _connection.CloseAsync(); + } + } +} diff --git a/Kyoo.Tests/Library/TestSample.cs b/Kyoo.Tests/Library/TestSample.cs new file mode 100644 index 00000000..5642054b --- /dev/null +++ b/Kyoo.Tests/Library/TestSample.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using Kyoo.Models; + +namespace Kyoo.Tests +{ + public static class TestSample + { + private static readonly Dictionary 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() + { + return (T)Samples[typeof(T)]; + } + } +} \ No newline at end of file