mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding tests
This commit is contained in:
parent
cc672876ae
commit
d61f3538fe
@ -225,7 +225,7 @@ namespace Kyoo.Controllers
|
|||||||
T old = await GetWithTracking(edited.ID);
|
T old = await GetWithTracking(edited.ID);
|
||||||
|
|
||||||
if (resetOld)
|
if (resetOld)
|
||||||
Merger.Nullify(old);
|
old = Merger.Nullify(old);
|
||||||
Merger.Complete(old, edited, x => x.GetCustomAttribute<LoadableRelationAttribute>() == null);
|
Merger.Complete(old, edited, x => x.GetCustomAttribute<LoadableRelationAttribute>() == null);
|
||||||
await EditRelations(old, edited, resetOld);
|
await EditRelations(old, edited, resetOld);
|
||||||
await Database.SaveChangesAsync();
|
await Database.SaveChangesAsync();
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Controllers;
|
using Kyoo.Controllers;
|
||||||
using Kyoo.Models;
|
|
||||||
|
|
||||||
namespace Kyoo.Tests
|
namespace Kyoo.Tests
|
||||||
{
|
{
|
||||||
public class RepositoryActivator : IDisposable, IAsyncDisposable
|
public class RepositoryActivator : IDisposable, IAsyncDisposable
|
||||||
{
|
{
|
||||||
public TestContext Context { get; init; }
|
public TestContext Context { get; }
|
||||||
public ILibraryManager LibraryManager { get; init; }
|
public ILibraryManager LibraryManager { get; }
|
||||||
|
|
||||||
|
|
||||||
private readonly DatabaseContext _database;
|
private readonly DatabaseContext _database;
|
||||||
@ -50,8 +49,6 @@ namespace Kyoo.Tests
|
|||||||
studio,
|
studio,
|
||||||
genre
|
genre
|
||||||
});
|
});
|
||||||
|
|
||||||
Context.AddTest<Show>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
@ -13,10 +13,11 @@ namespace Kyoo.Tests
|
|||||||
protected readonly RepositoryActivator Repositories;
|
protected readonly RepositoryActivator Repositories;
|
||||||
private readonly IRepository<T> _repository;
|
private readonly IRepository<T> _repository;
|
||||||
|
|
||||||
protected RepositoryTests(RepositoryActivator repositories)
|
protected RepositoryTests()
|
||||||
{
|
{
|
||||||
Repositories = repositories;
|
Repositories = new RepositoryActivator();
|
||||||
_repository = Repositories.LibraryManager.GetRepository<T>();
|
_repository = Repositories.LibraryManager.GetRepository<T>();
|
||||||
|
Repositories.Context.AddTest<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -66,5 +67,12 @@ namespace Kyoo.Tests
|
|||||||
await _repository.Delete(TestSample.Get<T>().Slug);
|
await _repository.Delete(TestSample.Get<T>().Slug);
|
||||||
Assert.Equal(0, await _repository.GetCount());
|
Assert.Equal(0, await _repository.GetCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteByValueTest()
|
||||||
|
{
|
||||||
|
await _repository.Delete(TestSample.Get<T>());
|
||||||
|
Assert.Equal(0, await _repository.GetCount());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
32
Kyoo.Tests/Library/SpecificTests/GlobalTests.cs
Normal file
32
Kyoo.Tests/Library/SpecificTests/GlobalTests.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Kyoo.Models;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Kyoo.Tests.SpecificTests
|
||||||
|
{
|
||||||
|
public class GlobalTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteShowWithEpisodeAndSeason()
|
||||||
|
{
|
||||||
|
RepositoryActivator repositories = new();
|
||||||
|
Show show = TestSample.Get<Show>();
|
||||||
|
show.Seasons = new[]
|
||||||
|
{
|
||||||
|
TestSample.Get<Season>()
|
||||||
|
};
|
||||||
|
show.Seasons.First().Episodes = new[]
|
||||||
|
{
|
||||||
|
TestSample.Get<Episode>()
|
||||||
|
};
|
||||||
|
await repositories.Context.AddAsync(show);
|
||||||
|
|
||||||
|
Assert.Equal(1, await repositories.LibraryManager.ShowRepository.GetCount());
|
||||||
|
await repositories.LibraryManager.ShowRepository.Delete(show);
|
||||||
|
Assert.Equal(0, await repositories.LibraryManager.ShowRepository.GetCount());
|
||||||
|
Assert.Equal(0, await repositories.LibraryManager.SeasonRepository.GetCount());
|
||||||
|
Assert.Equal(0, await repositories.LibraryManager.EpisodeRepository.GetCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Controllers;
|
using Kyoo.Controllers;
|
||||||
@ -12,7 +13,6 @@ namespace Kyoo.Tests.SpecificTests
|
|||||||
private readonly IShowRepository _repository;
|
private readonly IShowRepository _repository;
|
||||||
|
|
||||||
public ShowTests()
|
public ShowTests()
|
||||||
: base(new RepositoryActivator())
|
|
||||||
{
|
{
|
||||||
_repository = Repositories.LibraryManager.ShowRepository;
|
_repository = Repositories.LibraryManager.ShowRepository;
|
||||||
}
|
}
|
||||||
@ -51,26 +51,132 @@ namespace Kyoo.Tests.SpecificTests
|
|||||||
Assert.Equal(value.Genres.Select(x => new{x.Slug, x.Name}), show.Genres.Select(x => new{x.Slug, x.Name}));
|
Assert.Equal(value.Genres.Select(x => new{x.Slug, x.Name}), show.Genres.Select(x => new{x.Slug, x.Name}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Fact]
|
[Fact]
|
||||||
// public async Task EditPeopleTest()
|
public async Task EditStudioTest()
|
||||||
// {
|
{
|
||||||
// Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
||||||
// value.People = new[] {new People
|
value.Studio = new Studio("studio");
|
||||||
// {
|
Show edited = await _repository.Edit(value, false);
|
||||||
// Name = "test"
|
|
||||||
// }};
|
Assert.Equal(value.Slug, edited.Slug);
|
||||||
// Show edited = await _repository.Edit(value, false);
|
Assert.Equal("studio", edited.Studio.Slug);
|
||||||
//
|
|
||||||
// Assert.Equal(value.Slug, edited.Slug);
|
await using DatabaseContext database = Repositories.Context.New();
|
||||||
// Assert.Equal(value.Genres.Select(x => new{x.Slug, x.Name}), edited.Genres.Select(x => new{x.Slug, x.Name}));
|
Show show = await database.Shows
|
||||||
//
|
.Include(x => x.Genres)
|
||||||
// await using DatabaseContext database = Repositories.Context.New();
|
.FirstAsync();
|
||||||
// Show show = await database.Shows
|
|
||||||
// .Include(x => x.Genres)
|
Assert.Equal(value.Slug, show.Slug);
|
||||||
// .FirstAsync();
|
Assert.Equal("studio", edited.Studio.Slug);
|
||||||
//
|
}
|
||||||
// Assert.Equal(value.Slug, show.Slug);
|
|
||||||
// Assert.Equal(value.Genres.Select(x => new{x.Slug, x.Name}), show.Genres.Select(x => new{x.Slug, x.Name}));
|
[Fact]
|
||||||
// }
|
public async Task EditAliasesTest()
|
||||||
|
{
|
||||||
|
Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
||||||
|
value.Aliases = new[] {"NiceNewAlias", "SecondAlias"};
|
||||||
|
Show edited = await _repository.Edit(value, false);
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, edited.Slug);
|
||||||
|
Assert.Equal(value.Aliases, edited.Aliases);
|
||||||
|
|
||||||
|
await using DatabaseContext database = Repositories.Context.New();
|
||||||
|
Show show = await database.Shows.FirstAsync();
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, show.Slug);
|
||||||
|
Assert.Equal(value.Aliases, edited.Aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EditPeopleTest()
|
||||||
|
{
|
||||||
|
Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
||||||
|
value.People = new[]
|
||||||
|
{
|
||||||
|
new PeopleRole
|
||||||
|
{
|
||||||
|
Show = value,
|
||||||
|
People = TestSample.Get<People>(),
|
||||||
|
ForPeople = false,
|
||||||
|
Type = "Actor",
|
||||||
|
Role = "NiceCharacter"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Show edited = await _repository.Edit(value, false);
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, edited.Slug);
|
||||||
|
Assert.Equal(edited.People.First().ShowID, value.ID);
|
||||||
|
Assert.Equal(
|
||||||
|
value.People.Select(x => new{x.Role, x.Slug, x.People.Name}),
|
||||||
|
edited.People.Select(x => new{x.Role, x.Slug, x.People.Name}));
|
||||||
|
|
||||||
|
await using DatabaseContext database = Repositories.Context.New();
|
||||||
|
Show show = await database.Shows
|
||||||
|
.Include(x => x.People)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, show.Slug);
|
||||||
|
Assert.Equal(
|
||||||
|
value.People.Select(x => new{x.Role, x.Slug, x.People.Name}),
|
||||||
|
edited.People.Select(x => new{x.Role, x.Slug, x.People.Name}));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EditExternalIDsTest()
|
||||||
|
{
|
||||||
|
Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
||||||
|
value.ExternalIDs = new[]
|
||||||
|
{
|
||||||
|
new MetadataID<Show>()
|
||||||
|
{
|
||||||
|
First = value,
|
||||||
|
Second = new Provider("test", "test.png"),
|
||||||
|
DataID = "1234"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Show edited = await _repository.Edit(value, false);
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, edited.Slug);
|
||||||
|
Assert.Equal(
|
||||||
|
value.ExternalIDs.Select(x => new {x.DataID, x.Second.Slug}),
|
||||||
|
edited.ExternalIDs.Select(x => new {x.DataID, x.Second.Slug}));
|
||||||
|
|
||||||
|
await using DatabaseContext database = Repositories.Context.New();
|
||||||
|
Show show = await database.Shows
|
||||||
|
.Include(x => x.ExternalIDs)
|
||||||
|
.ThenInclude(x => x.Second)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
Assert.Equal(value.Slug, show.Slug);
|
||||||
|
Assert.Equal(
|
||||||
|
value.ExternalIDs.Select(x => new {x.DataID, x.Second.Slug}),
|
||||||
|
show.ExternalIDs.Select(x => new {x.DataID, x.Second.Slug}));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task EditResetOldTest()
|
||||||
|
{
|
||||||
|
Show value = await _repository.Get(TestSample.Get<Show>().Slug);
|
||||||
|
Show newValue = new()
|
||||||
|
{
|
||||||
|
ID = value.ID,
|
||||||
|
Title = "Reset"
|
||||||
|
};
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<ArgumentException>(() => _repository.Edit(newValue, true));
|
||||||
|
|
||||||
|
newValue.Slug = "reset";
|
||||||
|
Show edited = await _repository.Edit(newValue, true);
|
||||||
|
|
||||||
|
Assert.Equal(value.ID, edited.ID);
|
||||||
|
Assert.Null(edited.Overview);
|
||||||
|
Assert.Equal("reset", edited.Slug);
|
||||||
|
Assert.Equal("Reset", edited.Title);
|
||||||
|
Assert.Null(edited.Aliases);
|
||||||
|
Assert.Null(edited.ExternalIDs);
|
||||||
|
Assert.Null(edited.People);
|
||||||
|
Assert.Null(edited.Genres);
|
||||||
|
Assert.Null(edited.Studio);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -37,6 +37,17 @@ namespace Kyoo.Tests
|
|||||||
context.Database.Migrate();
|
context.Database.Migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
/// <summary>
|
||||||
/// Fill the database with pre defined values using a clean context.
|
/// Fill the database with pre defined values using a clean context.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -49,15 +60,26 @@ namespace Kyoo.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fill the database with pre defined values using a clean context.
|
/// Add an arbitrary data to the test context.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddTest<T>()
|
public void Add<T>(T obj)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
using DatabaseContext context = New();
|
using DatabaseContext context = New();
|
||||||
context.Set<T>().Add(TestSample.Get<T>());
|
context.Set<T>().Add(obj);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add an arbitrary data to the test context.
|
||||||
|
/// </summary>
|
||||||
|
public async Task AddAsync<T>(T obj)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
await using DatabaseContext context = New();
|
||||||
|
await context.Set<T>().AddAsync(obj);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a new database context connected to a in memory Sqlite database.
|
/// Get a new database context connected to a in memory Sqlite database.
|
||||||
|
@ -34,6 +34,49 @@ namespace Kyoo.Tests
|
|||||||
IsMovie = false,
|
IsMovie = false,
|
||||||
Studio = null
|
Studio = null
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
typeof(Season),
|
||||||
|
new Season
|
||||||
|
{
|
||||||
|
ID = 1,
|
||||||
|
ShowSlug = "anohana",
|
||||||
|
ShowID = 1,
|
||||||
|
SeasonNumber = 1,
|
||||||
|
Title = "Season 1",
|
||||||
|
Overview = "The first season",
|
||||||
|
StartDate = new DateTime(2020, 06, 05),
|
||||||
|
EndDate = new DateTime(2020, 07, 05),
|
||||||
|
Poster = "poster"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
typeof(Episode),
|
||||||
|
new Episode
|
||||||
|
{
|
||||||
|
ID = 1,
|
||||||
|
ShowSlug = "anohana",
|
||||||
|
ShowID = 1,
|
||||||
|
SeasonID = 1,
|
||||||
|
SeasonNumber = 1,
|
||||||
|
EpisodeNumber = 1,
|
||||||
|
AbsoluteNumber = 1,
|
||||||
|
Path = "/home/kyoo/anohana-s1e1",
|
||||||
|
Thumb = "thumbnail",
|
||||||
|
Title = "Episode 1",
|
||||||
|
Overview = "Summary of the first episode",
|
||||||
|
ReleaseDate = new DateTime(2020, 06, 05)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
typeof(People),
|
||||||
|
new People
|
||||||
|
{
|
||||||
|
ID = 1,
|
||||||
|
Slug = "the-actor",
|
||||||
|
Name = "The Actor",
|
||||||
|
Poster = "NicePoster"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
21
Kyoo.Tests/Utility/MergerTests.cs
Normal file
21
Kyoo.Tests/Utility/MergerTests.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using Kyoo.Models;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Kyoo.Tests
|
||||||
|
{
|
||||||
|
public class MergerTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void NullifyTest()
|
||||||
|
{
|
||||||
|
Genre genre = new("test")
|
||||||
|
{
|
||||||
|
ID = 5
|
||||||
|
};
|
||||||
|
Merger.Nullify(genre);
|
||||||
|
Assert.Equal(0, genre.ID);
|
||||||
|
Assert.Null(genre.Name);
|
||||||
|
Assert.Null(genre.Slug);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -131,6 +131,12 @@ namespace Kyoo.Controllers
|
|||||||
if (changed.Aliases != null || resetOld)
|
if (changed.Aliases != null || resetOld)
|
||||||
resource.Aliases = changed.Aliases;
|
resource.Aliases = changed.Aliases;
|
||||||
|
|
||||||
|
if (changed.Studio != null || resetOld)
|
||||||
|
{
|
||||||
|
await Database.Entry(resource).Reference(x => x.Studio).LoadAsync();
|
||||||
|
resource.Studio = changed.Studio;
|
||||||
|
}
|
||||||
|
|
||||||
if (changed.Genres != null || resetOld)
|
if (changed.Genres != null || resetOld)
|
||||||
{
|
{
|
||||||
await Database.Entry(resource).Collection(x => x.GenreLinks).LoadAsync();
|
await Database.Entry(resource).Collection(x => x.GenreLinks).LoadAsync();
|
||||||
@ -189,18 +195,9 @@ namespace Kyoo.Controllers
|
|||||||
throw new ArgumentNullException(nameof(obj));
|
throw new ArgumentNullException(nameof(obj));
|
||||||
|
|
||||||
_database.Entry(obj).State = EntityState.Deleted;
|
_database.Entry(obj).State = EntityState.Deleted;
|
||||||
|
|
||||||
|
|
||||||
if (obj.People != null)
|
|
||||||
foreach (PeopleRole entry in obj.People)
|
|
||||||
_database.Entry(entry).State = EntityState.Deleted;
|
|
||||||
|
|
||||||
if (obj.ExternalIDs != null)
|
|
||||||
foreach (MetadataID<Show> entry in obj.ExternalIDs)
|
|
||||||
_database.Entry(entry).State = EntityState.Deleted;
|
|
||||||
|
|
||||||
await _database.SaveChangesAsync();
|
await _database.SaveChangesAsync();
|
||||||
|
|
||||||
|
// TODO handle that with events maybe. (for now, seasons & episodes might not be loaded)
|
||||||
if (obj.Seasons != null)
|
if (obj.Seasons != null)
|
||||||
await _seasons.Value.DeleteRange(obj.Seasons);
|
await _seasons.Value.DeleteRange(obj.Seasons);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user