mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding some tests
This commit is contained in:
parent
d05e7a24b7
commit
81945b4e37
14
Kyoo.Tests/KAssert.cs
Normal file
14
Kyoo.Tests/KAssert.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
110
Kyoo.Tests/Library/RepositoryTests.cs
Normal file
110
Kyoo.Tests/Library/RepositoryTests.cs
Normal 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)
|
||||
{}
|
||||
}
|
||||
}
|
@ -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());
|
||||
// }
|
||||
}
|
||||
}
|
@ -1,79 +1,81 @@
|
||||
// using Kyoo.Models;
|
||||
// using Microsoft.Data.Sqlite;
|
||||
// using Microsoft.EntityFrameworkCore;
|
||||
//
|
||||
// namespace Kyoo.Tests
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// Class responsible to fill and create in memory databases for unit tests.
|
||||
// /// </summary>
|
||||
// public class TestContext
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// The context's options that specify to use an in memory Sqlite database.
|
||||
// /// </summary>
|
||||
// private readonly DbContextOptions<DatabaseContext> _context;
|
||||
//
|
||||
// /// <summary>
|
||||
// /// Create a new database and fill it with information.
|
||||
// /// </summary>
|
||||
// public TestContext()
|
||||
// {
|
||||
// SqliteConnection connection = new("DataSource=:memory:");
|
||||
// connection.Open();
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// _context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
// .UseSqlite(connection)
|
||||
// .Options;
|
||||
// FillDatabase();
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// connection.Close();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// Fill the database with pre defined values using a clean context.
|
||||
// /// </summary>
|
||||
// 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
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// Get a new database context connected to a in memory Sqlite database.
|
||||
// /// </summary>
|
||||
// /// <returns>A valid DatabaseContext</returns>
|
||||
// public DatabaseContext New()
|
||||
// {
|
||||
// return new(_context);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Kyoo.SqLite;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// 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()
|
||||
{
|
||||
_connection = new SqliteConnection("DataSource=:memory:");
|
||||
_connection.Open();
|
||||
|
||||
_context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
.UseSqlite(_connection)
|
||||
.Options;
|
||||
|
||||
using DatabaseContext context = New();
|
||||
context.Database.Migrate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill the database with pre defined values using a clean context.
|
||||
/// </summary>
|
||||
public async Task AddTestAsync<T>()
|
||||
where T : class
|
||||
{
|
||||
await using DatabaseContext context = New();
|
||||
await context.Set<T>().AddAsync(TestSample.Get<T>());
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill the database with pre defined values using a clean context.
|
||||
/// </summary>
|
||||
public void AddTest<T>()
|
||||
where T : class
|
||||
{
|
||||
using DatabaseContext context = New();
|
||||
context.Set<T>().Add(TestSample.Get<T>());
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 void Dispose()
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _connection.CloseAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
Kyoo.Tests/Library/TestSample.cs
Normal file
45
Kyoo.Tests/Library/TestSample.cs
Normal 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)];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user