mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-21 06:20:33 -04:00
* 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.
126 lines
3.0 KiB
C#
126 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 TagHelperTests
|
|
{
|
|
[Fact]
|
|
public void UpdateTag_ShouldAddNewTag()
|
|
{
|
|
var allTags = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
DbFactory.Tag("action"),
|
|
DbFactory.Tag("Sci-fi"),
|
|
};
|
|
var tagAdded = new List<Tag>();
|
|
|
|
TagHelper.UpdateTag(allTags, new[] {"Action", "Adventure"}, (tag, added) =>
|
|
{
|
|
if (added)
|
|
{
|
|
tagAdded.Add(tag);
|
|
}
|
|
|
|
});
|
|
|
|
Assert.Equal(1, tagAdded.Count);
|
|
Assert.Equal(4, allTags.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateTag_ShouldNotAddDuplicateTag()
|
|
{
|
|
var allTags = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
DbFactory.Tag("action"),
|
|
DbFactory.Tag("Sci-fi"),
|
|
|
|
};
|
|
var tagAdded = new List<Tag>();
|
|
|
|
TagHelper.UpdateTag(allTags, new[] {"Action", "Scifi"}, (tag, added) =>
|
|
{
|
|
if (added)
|
|
{
|
|
tagAdded.Add(tag);
|
|
}
|
|
TagHelper.AddTagIfNotExists(allTags, tag);
|
|
});
|
|
|
|
Assert.Equal(3, allTags.Count);
|
|
Assert.Empty(tagAdded);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddTag_ShouldAddOnlyNonExistingTag()
|
|
{
|
|
var existingTags = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
DbFactory.Tag("action"),
|
|
DbFactory.Tag("Sci-fi"),
|
|
};
|
|
|
|
|
|
TagHelper.AddTagIfNotExists(existingTags, DbFactory.Tag("Action"));
|
|
Assert.Equal(3, existingTags.Count);
|
|
|
|
TagHelper.AddTagIfNotExists(existingTags, DbFactory.Tag("action"));
|
|
Assert.Equal(3, existingTags.Count);
|
|
|
|
TagHelper.AddTagIfNotExists(existingTags, DbFactory.Tag("Shonen"));
|
|
Assert.Equal(4, existingTags.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void KeepOnlySamePeopleBetweenLists()
|
|
{
|
|
var existingTags = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
DbFactory.Tag("Sci-fi"),
|
|
};
|
|
|
|
var peopleFromChapters = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
};
|
|
|
|
var tagRemoved = new List<Tag>();
|
|
TagHelper.KeepOnlySameTagBetweenLists(existingTags,
|
|
peopleFromChapters, tag =>
|
|
{
|
|
tagRemoved.Add(tag);
|
|
});
|
|
|
|
Assert.Equal(1, tagRemoved.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoveEveryoneIfNothingInRemoveAllExcept()
|
|
{
|
|
var existingTags = new List<Tag>
|
|
{
|
|
DbFactory.Tag("Action"),
|
|
DbFactory.Tag("Sci-fi"),
|
|
};
|
|
|
|
var peopleFromChapters = new List<Tag>();
|
|
|
|
var tagRemoved = new List<Tag>();
|
|
TagHelper.KeepOnlySameTagBetweenLists(existingTags,
|
|
peopleFromChapters, tag =>
|
|
{
|
|
tagRemoved.Add(tag);
|
|
});
|
|
|
|
Assert.Equal(2, tagRemoved.Count);
|
|
}
|
|
}
|