Fix tests

This commit is contained in:
Zoe Roux
2023-08-08 12:08:51 +09:00
parent 7018915686
commit 30d52c6061
25 changed files with 77 additions and 165 deletions
@@ -186,14 +186,11 @@ namespace Kyoo.Abstractions.Models
/// If you don't know it or this is a movie, use null
/// </param>
/// <returns>The slug corresponding to the given arguments</returns>
/// <exception cref="ArgumentNullException">The given show slug was null.</exception>
public static string GetSlug(string showSlug,
int? seasonNumber,
int? episodeNumber,
int? absoluteNumber = null)
{
if (showSlug == null)
throw new ArgumentNullException(nameof(showSlug));
return seasonNumber switch
{
null when absoluteNumber == null => showSlug,
@@ -59,8 +59,6 @@ namespace Kyoo.Abstractions.Models.Utils
/// <param name="slug">The slug of the resource.</param>
public Identifier(string slug)
{
if (slug == null)
throw new ArgumentNullException(nameof(slug));
_slug = slug;
}
@@ -33,16 +33,10 @@ namespace Kyoo.Utils
/// <param name="self">The enumerable to check</param>
/// <param name="action">The action to execute is the list is empty</param>
/// <typeparam name="T">The type of items inside the list</typeparam>
/// <exception cref="ArgumentNullException">The iterable and the action can't be null.</exception>
/// <returns>The iterator proxied, there is no dual iterations.</returns>
[LinqTunnel]
public static IEnumerable<T> IfEmpty<T>(this IEnumerable<T> self, Action action)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
if (action == null)
throw new ArgumentNullException(nameof(action));
static IEnumerable<T> Generator(IEnumerable<T> self, Action action)
{
using IEnumerator<T> enumerator = self.GetEnumerator();
+1 -1
View File
@@ -58,7 +58,7 @@ namespace Kyoo.Utils
hasChanged = false;
if (second == null)
return first;
hasChanged = second.Any(x => x.Value?.Equals(first[x.Key]) == false);
hasChanged = second.Any(x => !first.ContainsKey(x.Key) || x.Value?.Equals(first[x.Key]) == false);
foreach ((T key, T2 value) in first)
second.TryAdd(key, value);
return second;
@@ -104,13 +104,8 @@ namespace Kyoo.Utils
/// <param name="type">The type to check</param>
/// <param name="genericType">The generic type to check against (Only generic types are supported like typeof(IEnumerable&lt;&gt;).</param>
/// <returns>True if obj inherit from genericType. False otherwise</returns>
/// <exception cref="ArgumentNullException">obj and genericType can't be null</exception>
public static bool IsOfGenericType(Type type, Type genericType)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (genericType == null)
throw new ArgumentNullException(nameof(genericType));
if (!genericType.IsGenericType)
throw new ArgumentException($"{nameof(genericType)} is not a generic type.");
@@ -128,14 +123,9 @@ namespace Kyoo.Utils
/// <param name="type">The type to check</param>
/// <param name="genericType">The generic type to check against (Only generic types are supported like typeof(IEnumerable&lt;&gt;).</param>
/// <returns>The generic definition of genericType that type inherit or null if type does not implement the generic type.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type"/> and <paramref name="genericType"/> can't be null</exception>
/// <exception cref="ArgumentException"><paramref name="genericType"/> must be a generic type</exception>
public static Type? GetGenericDefinition(Type type, Type genericType)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (genericType == null)
throw new ArgumentNullException(nameof(genericType));
if (!genericType.IsGenericType)
throw new ArgumentException($"{nameof(genericType)} is not a generic type.");
@@ -272,12 +262,6 @@ namespace Kyoo.Utils
Type[] types,
params object?[] args)
{
if (owner == null)
throw new ArgumentNullException(nameof(owner));
if (methodName == null)
throw new ArgumentNullException(nameof(methodName));
if (types == null)
throw new ArgumentNullException(nameof(types));
if (types.Length < 1)
throw new ArgumentException($"The {nameof(types)} array is empty. At least one type is needed.");
MethodInfo method = GetMethod(owner, BindingFlags.Static, methodName, types, args);
@@ -209,8 +209,6 @@ namespace Kyoo.Core.Controllers
where T : class, IResource
where T2 : class, IResource
{
if (member == null)
throw new ArgumentNullException(nameof(member));
return Load(obj, Utility.GetPropertyName(member), force);
}
@@ -219,8 +217,6 @@ namespace Kyoo.Core.Controllers
where T : class, IResource
where T2 : class
{
if (member == null)
throw new ArgumentNullException(nameof(member));
return Load(obj, Utility.GetPropertyName(member), force);
}
@@ -82,9 +82,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public override async Task Delete(Collection obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted;
await _database.SaveChangesAsync();
await base.Delete(obj);
@@ -170,9 +170,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public override async Task Delete(Episode obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
int epCount = await _database.Episodes.Where(x => x.ShowId == obj.ShowId).Take(2).CountAsync();
_database.Entry(obj).State = EntityState.Deleted;
await _database.SaveChangesAsync();
@@ -30,6 +30,7 @@ using Kyoo.Abstractions.Models.Exceptions;
using Kyoo.Core.Api;
using Kyoo.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace Kyoo.Core.Controllers
{
@@ -338,14 +339,15 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc/>
public virtual async Task<T> Create(T obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
await Validate(obj);
if (obj is IThumbnails thumbs)
{
Database.Entry(thumbs).Reference(x => x.Poster).IsModified = thumbs.Poster != null;
Database.Entry(thumbs).Reference(x => x.Thumbnail).IsModified = thumbs.Thumbnail != null;
Database.Entry(thumbs).Reference(x => x.Logo).IsModified = thumbs.Logo != null;
if (thumbs.Poster != null)
Database.Entry(thumbs).Reference(x => x.Poster).TargetEntry.State = EntityState.Added;
if (thumbs.Thumbnail != null)
Database.Entry(thumbs).Reference(x => x.Thumbnail).TargetEntry.State = EntityState.Added;
if (thumbs.Logo != null)
Database.Entry(thumbs).Reference(x => x.Logo).TargetEntry.State = EntityState.Added;
}
return obj;
}
@@ -395,7 +397,7 @@ namespace Kyoo.Core.Controllers
T old = await GetWithTracking(edited.Id);
Merger.Complete(old, edited, x => x.GetCustomAttribute<LoadableRelationAttribute>() == null);
await EditRelations(old, edited, true);
await EditRelations(old, edited);
await Database.SaveChangesAsync();
OnEdited?.Invoke(old);
return old;
@@ -418,6 +420,7 @@ namespace Kyoo.Core.Controllers
if (!await patch(resource))
throw new ArgumentException("Could not patch resource");
await Database.SaveChangesAsync();
OnEdited?.Invoke(resource);
return resource;
@@ -439,11 +442,8 @@ namespace Kyoo.Core.Controllers
/// The new version of <paramref name="resource"/>.
/// This item will be saved on the database and replace <paramref name="resource"/>
/// </param>
/// <param name="resetOld">
/// A boolean to indicate if all values of resource should be discarded or not.
/// </param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected virtual Task EditRelations(T resource, T changed, bool resetOld)
protected virtual Task EditRelations(T resource, T changed)
{
if (resource is IThumbnails thumbs && changed is IThumbnails chng)
{
@@ -22,7 +22,6 @@ using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Postgresql;
using Kyoo.Utils;
using Microsoft.EntityFrameworkCore;
namespace Kyoo.Core.Controllers
@@ -111,17 +110,17 @@ namespace Kyoo.Core.Controllers
}
/// <inheritdoc />
protected override async Task EditRelations(Movie resource, Movie changed, bool resetOld)
protected override async Task EditRelations(Movie resource, Movie changed)
{
await Validate(changed);
if (changed.Studio != null || resetOld)
if (changed.Studio != null || changed.StudioID == null)
{
await Database.Entry(resource).Reference(x => x.Studio).LoadAsync();
resource.Studio = changed.Studio;
}
if (changed.People != null || resetOld)
if (changed.People != null)
{
await Database.Entry(resource).Collection(x => x.People).LoadAsync();
resource.People = changed.People;
@@ -99,11 +99,11 @@ namespace Kyoo.Core.Controllers
}
/// <inheritdoc />
protected override async Task EditRelations(People resource, People changed, bool resetOld)
protected override async Task EditRelations(People resource, People changed)
{
await Validate(changed);
if (changed.Roles != null || resetOld)
if (changed.Roles != null)
{
await Database.Entry(resource).Collection(x => x.Roles).LoadAsync();
resource.Roles = changed.Roles;
@@ -113,9 +113,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public override async Task Delete(People obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted;
obj.Roles.ForEach(x => _database.Entry(x).State = EntityState.Deleted);
await _database.SaveChangesAsync();
@@ -137,9 +137,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc/>
public override async Task Delete(Season obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_database.Remove(obj);
await _database.SaveChangesAsync();
await base.Delete(obj);
@@ -113,17 +113,17 @@ namespace Kyoo.Core.Controllers
}
/// <inheritdoc />
protected override async Task EditRelations(Show resource, Show changed, bool resetOld)
protected override async Task EditRelations(Show resource, Show changed)
{
await Validate(changed);
if (changed.Studio != null || resetOld)
if (changed.Studio != null || changed.StudioId == null)
{
await Database.Entry(resource).Reference(x => x.Studio).LoadAsync();
resource.Studio = changed.Studio;
}
if (changed.People != null || resetOld)
if (changed.People != null)
{
await Database.Entry(resource).Collection(x => x.People).LoadAsync();
resource.People = changed.People;
@@ -82,9 +82,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public override async Task Delete(Studio obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted;
await _database.SaveChangesAsync();
await base.Delete(obj);
@@ -76,9 +76,6 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public override async Task Delete(User obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted;
await _database.SaveChangesAsync();
await base.Delete(obj);