Kavita/API.Tests/Helpers/GenreHelperTests.cs
Joe Milazzo 8a0a2f0961
Scanner Performance Improvements (#1774)
* Refactored the Genre code to be faster and used a dictonary to avoid some lookups. May fix the rare foreign constraint issue.

* Refactored tag to the same implementation as Genre. Ensure when grabbing tags from ComicInfo, we normalize and throw out duplicates.

* Removed an internal "external" field that was planned for Genres and Tags, but now with new plugin architecture, not needed.
2023-02-03 04:52:51 -08:00

118 lines
3.0 KiB
C#

using System.Collections.Generic;
using API.Data;
using API.Entities;
using API.Helpers;
using Xunit;
namespace API.Tests.Helpers;
public class GenreHelperTests
{
[Fact]
public void UpdateGenre_ShouldAddNewGenre()
{
var allGenres = new List<Genre>
{
DbFactory.Genre("Action"),
DbFactory.Genre("action"),
DbFactory.Genre("Sci-fi"),
};
var genreAdded = new List<Genre>();
GenreHelper.UpdateGenre(allGenres, new[] {"Action", "Adventure"}, genre =>
{
genreAdded.Add(genre);
});
Assert.Equal(2, genreAdded.Count);
Assert.Equal(4, allGenres.Count);
}
[Fact]
public void UpdateGenre_ShouldNotAddDuplicateGenre()
{
var allGenres = new List<Genre>
{
DbFactory.Genre("Action"),
DbFactory.Genre("action"),
DbFactory.Genre("Sci-fi"),
};
var genreAdded = new List<Genre>();
GenreHelper.UpdateGenre(allGenres, new[] {"Action", "Scifi"}, genre =>
{
genreAdded.Add(genre);
});
Assert.Equal(3, allGenres.Count);
Assert.Equal(2, genreAdded.Count);
}
[Fact]
public void AddGenre_ShouldAddOnlyNonExistingGenre()
{
var existingGenres = new List<Genre>
{
DbFactory.Genre("Action"),
DbFactory.Genre("action"),
DbFactory.Genre("Sci-fi"),
};
GenreHelper.AddGenreIfNotExists(existingGenres, DbFactory.Genre("Action"));
Assert.Equal(3, existingGenres.Count);
GenreHelper.AddGenreIfNotExists(existingGenres, DbFactory.Genre("action"));
Assert.Equal(3, existingGenres.Count);
GenreHelper.AddGenreIfNotExists(existingGenres, DbFactory.Genre("Shonen"));
Assert.Equal(4, existingGenres.Count);
}
[Fact]
public void KeepOnlySamePeopleBetweenLists()
{
var existingGenres = new List<Genre>
{
DbFactory.Genre("Action"),
DbFactory.Genre("Sci-fi"),
};
var peopleFromChapters = new List<Genre>
{
DbFactory.Genre("Action"),
};
var genreRemoved = new List<Genre>();
GenreHelper.KeepOnlySameGenreBetweenLists(existingGenres,
peopleFromChapters, genre =>
{
genreRemoved.Add(genre);
});
Assert.Equal(1, genreRemoved.Count);
}
[Fact]
public void RemoveEveryoneIfNothingInRemoveAllExcept()
{
var existingGenres = new List<Genre>
{
DbFactory.Genre("Action"),
DbFactory.Genre("Sci-fi"),
};
var peopleFromChapters = new List<Genre>();
var genreRemoved = new List<Genre>();
GenreHelper.KeepOnlySameGenreBetweenLists(existingGenres,
peopleFromChapters, genre =>
{
genreRemoved.Add(genre);
});
Assert.Equal(2, genreRemoved.Count);
}
}