Testing tvdb convertors

This commit is contained in:
Zoe Roux 2021-07-24 00:36:11 +02:00
parent 03bf2c4f10
commit c206512e3b
14 changed files with 198 additions and 33 deletions

View File

@ -43,7 +43,7 @@ namespace Kyoo.Models
/// <summary>
/// Is this show airing, not aired yet or finished?
/// </summary>
public Status? Status { get; set; }
public Status Status { get; set; }
/// <summary>
/// An URL to a trailer. This could be any path supported by the <see cref="IFileSystem"/>.

View File

@ -208,7 +208,7 @@ namespace Kyoo.Controllers
}
catch (DuplicatedItemException)
{
return await GetOrDefault(obj.Slug);
return await Get(obj.Slug);
}
}

View File

@ -233,7 +233,7 @@ namespace Kyoo.Tests
provider.ID = 0;
context.Providers.Add(provider);
Models.Library library = Get<Models.Library>();
Library library = Get<Library>();
library.ID = 0;
library.Collections = new List<Collection> {collection};
library.Providers = new List<Provider> {provider};

View File

@ -0,0 +1,160 @@
using System;
using System.Linq;
using Kyoo.Models;
using Kyoo.TheTvdb;
using TvDbSharper.Dto;
using Xunit;
namespace Kyoo.Tests.Identifier.Tvdb
{
public class ConvertorTests
{
[Fact]
public void SeriesSearchToShow()
{
SeriesSearchResult result = new()
{
Slug = "slug",
SeriesName = "name",
Aliases = new[] { "Aliases" },
Overview = "overview",
Status = "Ended",
FirstAired = "2021-07-23",
Poster = "/poster",
Id = 5
};
Provider provider = TestSample.Get<Provider>();
Show show = result.ToShow(provider);
Assert.Equal("slug", show.Slug);
Assert.Equal("name", show.Title);
Assert.Single(show.Aliases);
Assert.Equal("Aliases", show.Aliases[0]);
Assert.Equal("overview", show.Overview);
Assert.Equal(new DateTime(2021, 7, 23), show.StartAir);
Assert.Equal("https://www.thetvdb.com/poster", show.Poster);
Assert.Single(show.ExternalIDs);
Assert.Equal("5", show.ExternalIDs.First().DataID);
Assert.Equal(provider, show.ExternalIDs.First().Provider);
Assert.Equal("https://www.thetvdb.com/series/slug", show.ExternalIDs.First().Link);
Assert.Equal(Status.Finished, show.Status);
}
[Fact]
public void SeriesSearchToShowInvalidDate()
{
SeriesSearchResult result = new()
{
Slug = "slug",
SeriesName = "name",
Aliases = new[] { "Aliases" },
Overview = "overview",
Status = "ad",
FirstAired = "2e021-07-23",
Poster = "/poster",
Id = 5
};
Provider provider = TestSample.Get<Provider>();
Show show = result.ToShow(provider);
Assert.Equal("slug", show.Slug);
Assert.Equal("name", show.Title);
Assert.Single(show.Aliases);
Assert.Equal("Aliases", show.Aliases[0]);
Assert.Equal("overview", show.Overview);
Assert.Null(show.StartAir);
Assert.Equal("https://www.thetvdb.com/poster", show.Poster);
Assert.Single(show.ExternalIDs);
Assert.Equal("5", show.ExternalIDs.First().DataID);
Assert.Equal(provider, show.ExternalIDs.First().Provider);
Assert.Equal("https://www.thetvdb.com/series/slug", show.ExternalIDs.First().Link);
Assert.Equal(Status.Unknown, show.Status);
}
[Fact]
public void SeriesToShow()
{
Series result = new()
{
Slug = "slug",
SeriesName = "name",
Aliases = new[] { "Aliases" },
Overview = "overview",
Status = "Continuing",
FirstAired = "2021-07-23",
Poster = "poster",
FanArt = "fanart",
Id = 5,
Genre = new []
{
"Action",
"Test With Spéàacial characters"
}
};
Provider provider = TestSample.Get<Provider>();
Show show = result.ToShow(provider);
Assert.Equal("slug", show.Slug);
Assert.Equal("name", show.Title);
Assert.Single(show.Aliases);
Assert.Equal("Aliases", show.Aliases[0]);
Assert.Equal("overview", show.Overview);
Assert.Equal(new DateTime(2021, 7, 23), show.StartAir);
Assert.Equal("https://www.thetvdb.com/banners/poster", show.Poster);
Assert.Equal("https://www.thetvdb.com/banners/fanart", show.Backdrop);
Assert.Single(show.ExternalIDs);
Assert.Equal("5", show.ExternalIDs.First().DataID);
Assert.Equal(provider, show.ExternalIDs.First().Provider);
Assert.Equal("https://www.thetvdb.com/series/slug", show.ExternalIDs.First().Link);
Assert.Equal(Status.Airing, show.Status);
Assert.Equal(2, show.Genres.Count);
Assert.Equal("action", show.Genres.ToArray()[0].Slug);
Assert.Equal("Action", show.Genres.ToArray()[0].Name);
Assert.Equal("Test With Spéàacial characters", show.Genres.ToArray()[1].Name);
Assert.Equal("test-with-speaacial-characters", show.Genres.ToArray()[1].Slug);
}
[Fact]
public void ActorToPeople()
{
Actor actor = new()
{
Id = 5,
Image = "image",
Name = "Name",
Role = "role"
};
Provider provider = TestSample.Get<Provider>();
PeopleRole people = actor.ToPeopleRole(provider);
Assert.Equal("name", people.Slug);
Assert.Equal("Name", people.People.Name);
Assert.Equal("role", people.Role);
Assert.Equal("https://www.thetvdb.com/banners/image", people.People.Poster);
}
[Fact]
public void EpisodeRecordToEpisode()
{
EpisodeRecord record = new()
{
Id = 5,
AiredSeason = 2,
AiredEpisodeNumber = 3,
AbsoluteNumber = 23,
EpisodeName = "title",
Overview = "overview",
Filename = "thumb"
};
Provider provider = TestSample.Get<Provider>();
Episode episode = record.ToEpisode(provider);
Assert.Equal("title", episode.Title);
Assert.Equal(2, episode.SeasonNumber);
Assert.Equal(3, episode.EpisodeNumber);
Assert.Equal(23, episode.AbsoluteNumber);
Assert.Equal("overview", episode.Overview);
Assert.Equal("https://www.thetvdb.com/banners/thumb", episode.Thumb);
}
}
}

View File

@ -18,6 +18,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="TvDbSharper" Version="3.2.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@ -33,6 +34,7 @@
<ProjectReference Include="../Kyoo.CommonAPI/Kyoo.CommonAPI.csproj" />
<ProjectReference Include="../Kyoo.Common/Kyoo.Common.csproj" />
<ProjectReference Include="../Kyoo/Kyoo.csproj" />
<ProjectReference Include="..\Kyoo.TheTvdb\Kyoo.TheTvdb.csproj" />
</ItemGroup>
</Project>

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Kyoo.Tests
namespace Kyoo.Tests.Utility
{
public class EnumerableTests
{

View File

@ -5,7 +5,7 @@ using Kyoo.Models;
using Kyoo.Models.Attributes;
using Xunit;
namespace Kyoo.Tests
namespace Kyoo.Tests.Utility
{
public class MergerTests
{

View File

@ -3,7 +3,7 @@ using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Kyoo.Tests
namespace Kyoo.Tests.Utility
{
public class TaskTests
{

View File

@ -4,7 +4,9 @@ using System.Reflection;
using Kyoo.Models;
using Xunit;
namespace Kyoo.Tests
using Utils = Kyoo.Utility;
namespace Kyoo.Tests.Utility
{
public class UtilityTests
{
@ -14,12 +16,12 @@ namespace Kyoo.Tests
Expression<Func<Show, int>> member = x => x.ID;
Expression<Func<Show, object>> memberCast = x => x.ID;
Assert.False(Utility.IsPropertyExpression(null));
Assert.True(Utility.IsPropertyExpression(member));
Assert.True(Utility.IsPropertyExpression(memberCast));
Assert.False(Utils.IsPropertyExpression(null));
Assert.True(Utils.IsPropertyExpression(member));
Assert.True(Utils.IsPropertyExpression(memberCast));
Expression<Func<Show, object>> call = x => x.GetID("test");
Assert.False(Utility.IsPropertyExpression(call));
Assert.False(Utils.IsPropertyExpression(call));
}
[Fact]
@ -28,15 +30,15 @@ namespace Kyoo.Tests
Expression<Func<Show, int>> member = x => x.ID;
Expression<Func<Show, object>> memberCast = x => x.ID;
Assert.Equal("ID", Utility.GetPropertyName(member));
Assert.Equal("ID", Utility.GetPropertyName(memberCast));
Assert.Throws<ArgumentException>(() => Utility.GetPropertyName(null));
Assert.Equal("ID", Utils.GetPropertyName(member));
Assert.Equal("ID", Utils.GetPropertyName(memberCast));
Assert.Throws<ArgumentException>(() => Utils.GetPropertyName(null));
}
[Fact]
public void GetMethodTest()
{
MethodInfo method = Utility.GetMethod(typeof(UtilityTests),
MethodInfo method = Utils.GetMethod(typeof(UtilityTests),
BindingFlags.Instance | BindingFlags.Public,
nameof(GetMethodTest),
Array.Empty<Type>(),
@ -47,17 +49,17 @@ namespace Kyoo.Tests
[Fact]
public void GetMethodInvalidGenericsTest()
{
Assert.Throws<ArgumentException>(() => Utility.GetMethod(typeof(UtilityTests),
Assert.Throws<ArgumentException>(() => Utils.GetMethod(typeof(UtilityTests),
BindingFlags.Instance | BindingFlags.Public,
nameof(GetMethodTest),
new [] { typeof(Utility) },
new [] { typeof(Utils) },
Array.Empty<object>()));
}
[Fact]
public void GetMethodInvalidParamsTest()
{
Assert.Throws<ArgumentException>(() => Utility.GetMethod(typeof(UtilityTests),
Assert.Throws<ArgumentException>(() => Utils.GetMethod(typeof(UtilityTests),
BindingFlags.Instance | BindingFlags.Public,
nameof(GetMethodTest),
Array.Empty<Type>(),
@ -67,7 +69,7 @@ namespace Kyoo.Tests
[Fact]
public void GetMethodTest2()
{
MethodInfo method = Utility.GetMethod(typeof(Merger),
MethodInfo method = Utils.GetMethod(typeof(Merger),
BindingFlags.Static | BindingFlags.Public,
nameof(Merger.MergeLists),
new [] { typeof(string) },

View File

@ -16,13 +16,13 @@ namespace Kyoo.TheTvdb
/// </summary>
/// <param name="status">The string representing the status.</param>
/// <returns>A kyoo <see cref="Status"/> value or null.</returns>
private static Status? GetStatus(string status)
private static Status _GetStatus(string status)
{
return status switch
{
"Ended" => Status.Finished,
"Continuing" => Status.Airing,
_ => null
_ => Status.Unknown
};
}
@ -31,11 +31,12 @@ namespace Kyoo.TheTvdb
/// </summary>
/// <param name="date">The date string to parse</param>
/// <returns>The parsed <see cref="DateTime"/> or null.</returns>
private static DateTime ParseDate(string date)
private static DateTime? _ParseDate(string date)
{
DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsed);
return parsed;
return DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsed)
? parsed
: null;
}
/// <summary>
@ -52,8 +53,8 @@ namespace Kyoo.TheTvdb
Title = result.SeriesName,
Aliases = result.Aliases,
Overview = result.Overview,
Status = GetStatus(result.Status),
StartAir = ParseDate(result.FirstAired),
Status = _GetStatus(result.Status),
StartAir = _ParseDate(result.FirstAired),
Poster = result.Poster != null ? $"https://www.thetvdb.com{result.Poster}" : null,
ExternalIDs = new[]
{
@ -81,8 +82,8 @@ namespace Kyoo.TheTvdb
Title = series.SeriesName,
Aliases = series.Aliases,
Overview = series.Overview,
Status = GetStatus(series.Status),
StartAir = ParseDate(series.FirstAired),
Status = _GetStatus(series.Status),
StartAir = _ParseDate(series.FirstAired),
Poster = series.Poster != null ? $"https://www.thetvdb.com/banners/{series.Poster}" : null,
Backdrop = series.FanArt != null ? $"https://www.thetvdb.com/banners/{series.FanArt}" : null,
Genres = series.Genre.Select(y => new Genre(y)).ToList(),

View File

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using Autofac;
using Kyoo.Authentication.Models;
using Kyoo.Controllers;
using Kyoo.Models.Attributes;
using Kyoo.TheTvdb.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

View File

@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Kyoo.Authentication.Models;
using Kyoo.Controllers;
using Kyoo.Models;
using Kyoo.TheTvdb.Models;
using Microsoft.Extensions.Options;
using TvDbSharper;
using TvDbSharper.Dto;

View File

@ -1,4 +1,4 @@
namespace Kyoo.Authentication.Models
namespace Kyoo.TheTvdb.Models
{
/// <summary>
/// The option containing the api key for the tvdb.

@ -1 +1 @@
Subproject commit 87783a5bfdd79f21d72bad13da1d96ec8e08cd42
Subproject commit c037270d3339fcf0075984a089f353c5c332a751