From 2785566801de55fdb784256677c358f91fcc2d6e Mon Sep 17 00:00:00 2001 From: Brian Bicknell Date: Sat, 30 Aug 2025 17:27:08 -0400 Subject: [PATCH] Added tag and creator tests. --- .../Creators/CreatorSearchProvider.cs | 4 +- .../Fixtures/CreatorSearchProviderFixture.cs | 26 +++++ .../Fixtures/TagSearchProviderFixture.cs | 32 ++++++ .../Integration/CreatorSearchProviderTests.cs | 103 ++++++++++++++++++ .../Integration/TagSearchProviderTests.cs | 91 ++++++++++++++++ 5 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 JSMR.Tests/Fixtures/CreatorSearchProviderFixture.cs create mode 100644 JSMR.Tests/Fixtures/TagSearchProviderFixture.cs create mode 100644 JSMR.Tests/Integration/CreatorSearchProviderTests.cs create mode 100644 JSMR.Tests/Integration/TagSearchProviderTests.cs diff --git a/JSMR.Infrastructure/Data/Repositories/Creators/CreatorSearchProvider.cs b/JSMR.Infrastructure/Data/Repositories/Creators/CreatorSearchProvider.cs index 33ddd51..4077562 100644 --- a/JSMR.Infrastructure/Data/Repositories/Creators/CreatorSearchProvider.cs +++ b/JSMR.Infrastructure/Data/Repositories/Creators/CreatorSearchProvider.cs @@ -42,8 +42,8 @@ public class CreatorSearchProvider(AppDbContext context) : SearchProvider> selector = field switch { CreatorSortField.VoiceWorkCount => x => x.VoiceWorkCount, - CreatorSortField.Favorite => x => x.Favorite, - CreatorSortField.Blacklisted => x => x.Blacklisted, + CreatorSortField.Favorite => x => !x.Favorite, + CreatorSortField.Blacklisted => x => !x.Blacklisted, _ => x => x.Name }; diff --git a/JSMR.Tests/Fixtures/CreatorSearchProviderFixture.cs b/JSMR.Tests/Fixtures/CreatorSearchProviderFixture.cs new file mode 100644 index 0000000..62985e7 --- /dev/null +++ b/JSMR.Tests/Fixtures/CreatorSearchProviderFixture.cs @@ -0,0 +1,26 @@ +using JSMR.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + +namespace JSMR.Tests.Fixtures; + +public class CreatorSearchProviderFixture : MariaDbFixture +{ + protected override async Task OnInitializedAsync(AppDbContext context) + { + await SeedAsync(context); + } + + private static async Task SeedAsync(AppDbContext context) + { + if (await context.Tags.AnyAsync()) + return; + + context.Creators.AddRange( + new() { CreatorId = 1, Name = "John Smith" }, + new() { CreatorId = 2, Name = "John Doe", Favorite = true }, + new() { CreatorId = 3, Name = "Jane Doe", Blacklisted = true } + ); + + await context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/JSMR.Tests/Fixtures/TagSearchProviderFixture.cs b/JSMR.Tests/Fixtures/TagSearchProviderFixture.cs new file mode 100644 index 0000000..0f62974 --- /dev/null +++ b/JSMR.Tests/Fixtures/TagSearchProviderFixture.cs @@ -0,0 +1,32 @@ +using JSMR.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + +namespace JSMR.Tests.Fixtures; + +public class TagSearchProviderFixture : MariaDbFixture +{ + protected override async Task OnInitializedAsync(AppDbContext context) + { + await SeedAsync(context); + } + + private static async Task SeedAsync(AppDbContext context) + { + if (await context.Tags.AnyAsync()) + return; + + context.Tags.AddRange( + new() { TagId = 1, Name = "OL" }, + new() { TagId = 2, Name = "ほのぼの", Favorite = true }, + new() { TagId = 3, Name = "ツンデレ", Blacklisted = true } + ); + + context.EnglishTags.AddRange( + new() { EnglishTagId = 1, TagId = 1, Name = "Office Lady" }, + new() { EnglishTagId = 2, TagId = 2, Name = "Heartwarming" }, + new() { EnglishTagId = 3, TagId = 3, Name = "Tsundere" } + ); + + await context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/JSMR.Tests/Integration/CreatorSearchProviderTests.cs b/JSMR.Tests/Integration/CreatorSearchProviderTests.cs new file mode 100644 index 0000000..863c673 --- /dev/null +++ b/JSMR.Tests/Integration/CreatorSearchProviderTests.cs @@ -0,0 +1,103 @@ +using JSMR.Application.Common.Search; +using JSMR.Application.Creators.Queries.Search.Contracts; +using JSMR.Infrastructure.Data; +using JSMR.Infrastructure.Data.Repositories.Creators; +using JSMR.Tests.Fixtures; +using Shouldly; + +namespace JSMR.Tests.Integration; + +public class CreatorSearchProviderTests(CreatorSearchProviderFixture fixture) : IClassFixture +{ + [Fact] + public async Task Filter_None_Sort_Name_Ascending() + { + await using AppDbContext context = fixture.CreateDbContext(); + CreatorSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(CreatorSortField.Name, Application.Common.Search.SortDirection.Ascending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(3); + result.TotalItems.ShouldBe(3); + result.Items[0].Name.ShouldBe("Jane Doe"); + result.Items[2].Name.ShouldBe("John Smith"); + } + + [Fact] + public async Task Filter_None_Sort_Name_Descending() + { + await using AppDbContext context = fixture.CreateDbContext(); + CreatorSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(CreatorSortField.Name, Application.Common.Search.SortDirection.Descending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(3); + result.TotalItems.ShouldBe(3); + result.Items[0].Name.ShouldBe("John Smith"); + result.Items[2].Name.ShouldBe("Jane Doe"); + } + + [Fact] + public async Task Filter_None_Sort_Favorite_Descending() + { + await using AppDbContext context = fixture.CreateDbContext(); + CreatorSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(CreatorSortField.Favorite, Application.Common.Search.SortDirection.Ascending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items[0].Name.ShouldBe("John Doe"); + } + + [Fact] + public async Task Filter_None_Sort_Blacklisted_Descending() + { + await using AppDbContext context = fixture.CreateDbContext(); + CreatorSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(CreatorSortField.Blacklisted, Application.Common.Search.SortDirection.Ascending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items[0].Name.ShouldBe("Jane Doe"); + } + + [Fact] + public async Task Filter_By_Name_Creator_Name() + { + await using AppDbContext context = fixture.CreateDbContext(); + CreatorSearchProvider provider = new(context); + + var options = new SearchOptions() + { + Criteria = new() + { + Name = "Jane" + } + }; + + var result = await provider.SearchAsync(options); + + // Assert + result.Items.Length.ShouldBe(1); + result.TotalItems.ShouldBe(1); + result.Items.ShouldContain(creatorView => creatorView.Name == "Jane Doe"); + } +} \ No newline at end of file diff --git a/JSMR.Tests/Integration/TagSearchProviderTests.cs b/JSMR.Tests/Integration/TagSearchProviderTests.cs new file mode 100644 index 0000000..48d857d --- /dev/null +++ b/JSMR.Tests/Integration/TagSearchProviderTests.cs @@ -0,0 +1,91 @@ +using JSMR.Application.Common.Search; +using JSMR.Application.Tags.Queries.Search.Contracts; +using JSMR.Infrastructure.Data; +using JSMR.Infrastructure.Data.Repositories.Tags; +using JSMR.Tests.Fixtures; +using Shouldly; + +namespace JSMR.Tests.Integration; + +public class TagSearchProviderTests(TagSearchProviderFixture fixture) : IClassFixture +{ + [Fact] + public async Task Filter_None_Sort_Name() + { + await using AppDbContext context = fixture.CreateDbContext(); + TagSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(TagSortField.Name, Application.Common.Search.SortDirection.Ascending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(3); + result.TotalItems.ShouldBe(3); + result.Items[0].Name.ShouldBe("OL"); + result.Items[2].Name.ShouldBe("ツンデレ"); + } + + [Fact] + public async Task Filter_None_Sort_EnglishName() + { + await using AppDbContext context = fixture.CreateDbContext(); + TagSearchProvider provider = new(context); + + var options = new SearchOptions() + { + SortOptions = [new(TagSortField.EnglishName, Application.Common.Search.SortDirection.Ascending)] + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(3); + result.TotalItems.ShouldBe(3); + result.Items[0].EnglishName.ShouldBe("Heartwarming"); + result.Items[2].EnglishName.ShouldBe("Tsundere"); + } + + [Fact] + public async Task Filter_By_Name_Tag_Name() + { + await using AppDbContext context = fixture.CreateDbContext(); + TagSearchProvider provider = new(context); + + var options = new SearchOptions() + { + Criteria = new() + { + Name = "ほの" + } + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(1); + result.TotalItems.ShouldBe(1); + result.Items.ShouldContain(tagView => tagView.Name == "ほのぼの"); + } + + [Fact] + public async Task Filter_By_Name_English_Tag_Name() + { + await using AppDbContext context = fixture.CreateDbContext(); + TagSearchProvider provider = new(context); + + var options = new SearchOptions() + { + Criteria = new() + { + Name = "Heart" + } + }; + + var result = await provider.SearchAsync(options); + + result.Items.Length.ShouldBe(1); + result.TotalItems.ShouldBe(1); + result.Items.ShouldContain(tagView => tagView.EnglishName == "Heartwarming"); + } +} \ No newline at end of file