// Kyoo - A portable and vast media library solution. // Copyright (c) Kyoo. // // See AUTHORS.md and LICENSE file in the project root for full license information. // // Kyoo is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // Kyoo is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models.Exceptions; using Kyoo.Database; using Xunit; namespace Kyoo.Tests.Database { public abstract class RepositoryTests : IDisposable, IAsyncDisposable where T : class, IResource, new() { protected readonly RepositoryActivator Repositories; private readonly IRepository _repository; protected RepositoryTests(RepositoryActivator repositories) { Repositories = repositories; _repository = Repositories.LibraryManager.GetRepository(); } public void Dispose() { Repositories.Dispose(); GC.SuppressFinalize(this); } public ValueTask DisposeAsync() { return Repositories.DisposeAsync(); } [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().ID); KAssert.DeepEqual(TestSample.Get(), value); } [Fact] public async Task GetBySlugTest() { T value = await _repository.Get(TestSample.Get().Slug); KAssert.DeepEqual(TestSample.Get(), value); } [Fact] public async Task GetByFakeIdTest() { await Assert.ThrowsAsync(() => _repository.Get(2)); } [Fact] public async Task GetByFakeSlugTest() { await Assert.ThrowsAsync(() => _repository.Get("non-existent")); } [Fact] public async Task DeleteByIdTest() { await _repository.Delete(TestSample.Get().ID); Assert.Equal(0, await _repository.GetCount()); } [Fact] public async Task DeleteBySlugTest() { await _repository.Delete(TestSample.Get().Slug); Assert.Equal(0, await _repository.GetCount()); } [Fact] public async Task DeleteByValueTest() { await _repository.Delete(TestSample.Get()); Assert.Equal(0, await _repository.GetCount()); } [Fact] public async Task CreateTest() { await Assert.ThrowsAsync(() => _repository.Create(TestSample.Get())); await _repository.Delete(TestSample.Get()); T expected = TestSample.Get(); expected.ID = 0; await _repository.Create(expected); KAssert.DeepEqual(expected, await _repository.Get(expected.Slug)); } [Fact] public async Task CreateNullTest() { await Assert.ThrowsAsync(() => _repository.Create(null!)); } [Fact] public async Task CreateIfNotExistNullTest() { await Assert.ThrowsAsync(() => _repository.CreateIfNotExists(null!)); } [Fact] public async Task CreateIfNotExistTest() { T expected = TestSample.Get(); KAssert.DeepEqual(expected, await _repository.CreateIfNotExists(TestSample.Get())); await _repository.Delete(TestSample.Get()); KAssert.DeepEqual(expected, await _repository.CreateIfNotExists(TestSample.Get())); } [Fact] public async Task EditNullTest() { await Assert.ThrowsAsync(() => _repository.Edit(null!, false)); } [Fact] public async Task EditNonExistingTest() { await Assert.ThrowsAsync(() => _repository.Edit(new T { ID = 56 }, false)); } [Fact] public async Task GetExpressionIDTest() { KAssert.DeepEqual(TestSample.Get(), await _repository.Get(x => x.ID == TestSample.Get().ID)); } [Fact] public async Task GetExpressionSlugTest() { KAssert.DeepEqual(TestSample.Get(), await _repository.Get(x => x.Slug == TestSample.Get().Slug)); } [Fact] public async Task GetExpressionNotFoundTest() { await Assert.ThrowsAsync(() => _repository.Get(x => x.Slug == "non-existing")); } [Fact] public async Task GetExpressionNullTest() { await Assert.ThrowsAsync(() => _repository.Get((Expression>)null!)); } [Fact] public async Task GetOrDefaultTest() { Assert.Null(await _repository.GetOrDefault(56)); 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().Slug[2..4]; Assert.Equal(1, await _repository.GetCount(x => x.Slug.Contains(slug))); } [Fact] public async Task GetAllTest() { string slug = TestSample.Get().Slug[2..4]; ICollection ret = await _repository.GetAll(x => x.Slug.Contains(slug)); Assert.Equal(1, ret.Count); KAssert.DeepEqual(TestSample.Get(), ret.First()); } [Fact] public async Task DeleteAllTest() { string slug = TestSample.Get().Slug[2..4]; await _repository.DeleteAll(x => x.Slug.Contains(slug)); Assert.Equal(0, await _repository.GetCount()); } } }