Uncomment and fix existing PersonHelperTests.cs

This commit is contained in:
Amelia 2025-05-03 19:13:26 +02:00
parent 3a0d33ca13
commit 43b792eeb5
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA

View file

@ -1,5 +1,10 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Entities.Enums;
using API.Helpers;
using API.Helpers.Builders;
using Xunit;
namespace API.Tests.Helpers; namespace API.Tests.Helpers;
@ -8,126 +13,167 @@ public class PersonHelperTests : AbstractDbTest
protected override async Task ResetDb() protected override async Task ResetDb()
{ {
_context.Series.RemoveRange(_context.Series.ToList()); _context.Series.RemoveRange(_context.Series.ToList());
_context.Person.RemoveRange(_context.Person.ToList());
_context.Library.RemoveRange(_context.Library.ToList());
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
//
// // 1. Test adding new people and keeping existing ones // 1. Test adding new people and keeping existing ones
// [Fact] [Fact]
// public async Task UpdateChapterPeopleAsync_AddNewPeople_ExistingPersonRetained() public async Task UpdateChapterPeopleAsync_AddNewPeople_ExistingPersonRetained()
// { {
// var existingPerson = new PersonBuilder("Joe Shmo").Build(); await ResetDb();
// var chapter = new ChapterBuilder("1").Build();
// var library = new LibraryBuilder("My Library")
// // Create an existing person and assign them to the series with a role .Build();
// var series = new SeriesBuilder("Test 1")
// .WithFormat(MangaFormat.Archive) _unitOfWork.LibraryRepository.Add(library);
// .WithMetadata(new SeriesMetadataBuilder() await _unitOfWork.CommitAsync();
// .WithPerson(existingPerson, PersonRole.Editor)
// .Build()) var existingPerson = new PersonBuilder("Joe Shmo").Build();
// .WithVolume(new VolumeBuilder("1").WithChapter(chapter).Build()) var chapter = new ChapterBuilder("1").Build();
// .Build();
// // Create an existing person and assign them to the series with a role
// _unitOfWork.SeriesRepository.Add(series); var series = new SeriesBuilder("Test 1")
// await _unitOfWork.CommitAsync(); .WithLibraryId(library.Id)
// .WithFormat(MangaFormat.Archive)
// // Call UpdateChapterPeopleAsync with one existing and one new person .WithMetadata(new SeriesMetadataBuilder()
// await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo", "New Person" }, PersonRole.Editor, _unitOfWork); .WithPerson(existingPerson, PersonRole.Editor)
// .Build())
// // Assert existing person retained and new person added .WithVolume(new VolumeBuilder("1").WithChapter(chapter).Build())
// var people = await _unitOfWork.PersonRepository.GetAllPeople(); .Build();
// Assert.Contains(people, p => p.Name == "Joe Shmo");
// Assert.Contains(people, p => p.Name == "New Person"); _unitOfWork.SeriesRepository.Add(series);
// await _unitOfWork.CommitAsync();
// var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList();
// Assert.Contains("Joe Shmo", chapterPeople); // Call UpdateChapterPeopleAsync with one existing and one new person
// Assert.Contains("New Person", chapterPeople); await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo", "New Person" }, PersonRole.Editor, _unitOfWork);
// }
// // Assert existing person retained and new person added
// // 2. Test removing a person no longer in the list var people = await _unitOfWork.PersonRepository.GetAllPeople();
// [Fact] Assert.Contains(people, p => p.Name == "Joe Shmo");
// public async Task UpdateChapterPeopleAsync_RemovePeople() Assert.Contains(people, p => p.Name == "New Person");
// {
// var existingPerson1 = new PersonBuilder("Joe Shmo").Build(); var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList();
// var existingPerson2 = new PersonBuilder("Jane Doe").Build(); Assert.Contains("Joe Shmo", chapterPeople);
// var chapter = new ChapterBuilder("1").Build(); Assert.Contains("New Person", chapterPeople);
// }
// var series = new SeriesBuilder("Test 1")
// .WithVolume(new VolumeBuilder("1") // 2. Test removing a person no longer in the list
// .WithChapter(new ChapterBuilder("1") [Fact]
// .WithPerson(existingPerson1, PersonRole.Editor) public async Task UpdateChapterPeopleAsync_RemovePeople()
// .WithPerson(existingPerson2, PersonRole.Editor) {
// .Build()) await ResetDb();
// .Build())
// .Build(); var library = new LibraryBuilder("My Library")
// .Build();
// _unitOfWork.SeriesRepository.Add(series);
// await _unitOfWork.CommitAsync(); _unitOfWork.LibraryRepository.Add(library);
// await _unitOfWork.CommitAsync();
// // Call UpdateChapterPeopleAsync with only one person
// await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork); var existingPerson1 = new PersonBuilder("Joe Shmo").Build();
// var existingPerson2 = new PersonBuilder("Jane Doe").Build();
// var people = await _unitOfWork.PersonRepository.GetAllPeople(); var chapter = new ChapterBuilder("1")
// Assert.DoesNotContain(people, p => p.Name == "Jane Doe"); .WithPerson(existingPerson1, PersonRole.Editor)
// .WithPerson(existingPerson2, PersonRole.Editor)
// var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList(); .Build();
// Assert.Contains("Joe Shmo", chapterPeople);
// Assert.DoesNotContain("Jane Doe", chapterPeople); var series = new SeriesBuilder("Test 1")
// } .WithLibraryId(library.Id)
// .WithVolume(new VolumeBuilder("1")
// // 3. Test no changes when the list of people is the same .WithChapter(chapter)
// [Fact] .Build())
// public async Task UpdateChapterPeopleAsync_NoChanges() .Build();
// {
// var existingPerson = new PersonBuilder("Joe Shmo").Build(); _unitOfWork.SeriesRepository.Add(series);
// var chapter = new ChapterBuilder("1").Build(); await _unitOfWork.CommitAsync();
//
// var series = new SeriesBuilder("Test 1") // Call UpdateChapterPeopleAsync with only one person
// .WithVolume(new VolumeBuilder("1") await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork);
// .WithChapter(new ChapterBuilder("1")
// .WithPerson(existingPerson, PersonRole.Editor) // PersonHelper does not remove the Person from the global DbSet itself
// .Build()) await _unitOfWork.PersonRepository.RemoveAllPeopleNoLongerAssociated();
// .Build())
// .Build(); var people = await _unitOfWork.PersonRepository.GetAllPeople();
// Assert.DoesNotContain(people, p => p.Name == "Jane Doe");
// _unitOfWork.SeriesRepository.Add(series);
// await _unitOfWork.CommitAsync(); var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList();
// Assert.Contains("Joe Shmo", chapterPeople);
// // Call UpdateChapterPeopleAsync with the same list Assert.DoesNotContain("Jane Doe", chapterPeople);
// await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork); }
//
// var people = await _unitOfWork.PersonRepository.GetAllPeople(); // 3. Test no changes when the list of people is the same
// Assert.Contains(people, p => p.Name == "Joe Shmo"); [Fact]
// public async Task UpdateChapterPeopleAsync_NoChanges()
// var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList(); {
// Assert.Contains("Joe Shmo", chapterPeople); await ResetDb();
// Assert.Single(chapter.People); // No duplicate entries
// } var library = new LibraryBuilder("My Library")
// .Build();
// // 4. Test multiple roles for a person
// [Fact] _unitOfWork.LibraryRepository.Add(library);
// public async Task UpdateChapterPeopleAsync_MultipleRoles() await _unitOfWork.CommitAsync();
// {
// var person = new PersonBuilder("Joe Shmo").Build(); var existingPerson = new PersonBuilder("Joe Shmo").Build();
// var chapter = new ChapterBuilder("1").Build(); var chapter = new ChapterBuilder("1").WithPerson(existingPerson, PersonRole.Editor).Build();
//
// var series = new SeriesBuilder("Test 1") var series = new SeriesBuilder("Test 1")
// .WithVolume(new VolumeBuilder("1") .WithLibraryId(library.Id)
// .WithChapter(new ChapterBuilder("1") .WithVolume(new VolumeBuilder("1")
// .WithPerson(person, PersonRole.Writer) // Assign person as Writer .WithChapter(chapter)
// .Build()) .Build())
// .Build()) .Build();
// .Build();
// _unitOfWork.SeriesRepository.Add(series);
// _unitOfWork.SeriesRepository.Add(series); await _unitOfWork.CommitAsync();
// await _unitOfWork.CommitAsync();
// // Call UpdateChapterPeopleAsync with the same list
// // Add same person as Editor await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork);
// await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork);
// var people = await _unitOfWork.PersonRepository.GetAllPeople();
// // Ensure that the same person is assigned with two roles Assert.Contains(people, p => p.Name == "Joe Shmo");
// var chapterPeople = chapter.People.Where(cp => cp.Person.Name == "Joe Shmo").ToList();
// Assert.Equal(2, chapterPeople.Count); // One for each role var chapterPeople = chapter.People.Select(cp => cp.Person.Name).ToList();
// Assert.Contains(chapterPeople, cp => cp.Role == PersonRole.Writer); Assert.Contains("Joe Shmo", chapterPeople);
// Assert.Contains(chapterPeople, cp => cp.Role == PersonRole.Editor); Assert.Single(chapter.People); // No duplicate entries
// } }
// 4. Test multiple roles for a person
[Fact]
public async Task UpdateChapterPeopleAsync_MultipleRoles()
{
await ResetDb();
var library = new LibraryBuilder("My Library")
.Build();
_unitOfWork.LibraryRepository.Add(library);
await _unitOfWork.CommitAsync();
var person = new PersonBuilder("Joe Shmo").Build();
var chapter = new ChapterBuilder("1").WithPerson(person, PersonRole.Writer).Build();
var series = new SeriesBuilder("Test 1")
.WithLibraryId(library.Id)
.WithVolume(new VolumeBuilder("1")
.WithChapter(chapter)
.Build())
.Build();
_unitOfWork.SeriesRepository.Add(series);
await _unitOfWork.CommitAsync();
// Add same person as Editor
await PersonHelper.UpdateChapterPeopleAsync(chapter, new List<string> { "Joe Shmo" }, PersonRole.Editor, _unitOfWork);
// Ensure that the same person is assigned with two roles
var chapterPeople = chapter
.People
.Where(cp =>
cp.Person.Name == "Joe Shmo")
.ToList();
Assert.Equal(2, chapterPeople.Count); // One for each role
Assert.Contains(chapterPeople, cp => cp.Role == PersonRole.Writer);
Assert.Contains(chapterPeople, cp => cp.Role == PersonRole.Editor);
}
} }