Kavita/API.Tests/Helpers/TagHelperTests.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

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);
}
}