From 33e9cb12c902a6ca91925da1620401e74c9f76fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Tue, 19 Sep 2023 09:21:20 +0200 Subject: [PATCH] fix searching by name --- .../Features/UserSearch/UserSearchService.cs | 8 +- .../UserSearch/UserSearchServiceTests.cs | 90 ++++++++++++++++--- 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/web/Server/Features/UserSearch/UserSearchService.cs b/src/web/Server/Features/UserSearch/UserSearchService.cs index 56cd9ea2..804d3fd3 100644 --- a/src/web/Server/Features/UserSearch/UserSearchService.cs +++ b/src/web/Server/Features/UserSearch/UserSearchService.cs @@ -8,8 +8,8 @@ namespace Jordnaer.Server.Features.UserSearch; public interface IUserSearchService { - Task GetUsersAsync(UserSearchFilter filter, CancellationToken cancellationToken); - Task> GetUsersByNameAsync(string searchString, string omitById, CancellationToken cancellationToken); + Task GetUsersAsync(UserSearchFilter filter, CancellationToken cancellationToken = default); + Task> GetUsersByNameAsync(string searchString, string omitById, CancellationToken cancellationToken = default); } public class UserSearchService : IUserSearchService @@ -184,10 +184,8 @@ private static IQueryable ApplyNameFilter(string? filter, IQueryabl { if (!string.IsNullOrWhiteSpace(filter)) { - string trimmedNameFilter = new(filter.Where(c => !char.IsWhiteSpace(c)).ToArray()); - users = users.Where(user => !string.IsNullOrEmpty(user.SearchableName) && - EF.Functions.Like(user.SearchableName, $"%{trimmedNameFilter}%")); + EF.Functions.Like(user.SearchableName, $"%{filter}%")); } return users; diff --git a/tests/web/Jordnaer.Server.Tests/UserSearch/UserSearchServiceTests.cs b/tests/web/Jordnaer.Server.Tests/UserSearch/UserSearchServiceTests.cs index f34c8bfe..7df1d0c8 100644 --- a/tests/web/Jordnaer.Server.Tests/UserSearch/UserSearchServiceTests.cs +++ b/tests/web/Jordnaer.Server.Tests/UserSearch/UserSearchServiceTests.cs @@ -40,7 +40,7 @@ public async Task Return_UserSearchResult_Given_Valid_Filter() var filter = new UserSearchFilter(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.Should().BeOfType(); @@ -60,7 +60,7 @@ public async Task Return_UserSearchResult_With_LookingFor_Filter() await _context.SaveChangesAsync(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1); @@ -68,23 +68,89 @@ public async Task Return_UserSearchResult_With_LookingFor_Filter() } [Fact] - public async Task Return_UserSearchResult_With_Name_Filter() + public async Task Return_UserSearchResult_With_FirstName_Filter() { // Arrange - var filter = new UserSearchFilter { Name = _faker.Name.FirstName() }; + string? firstName = _faker.Name.FirstName(); + var filter = new UserSearchFilter { Name = firstName }; var users = CreateTestUsers(5); // Ensure at least one user has the specified name in their SearchableName - users[0].FirstName = filter.Name; + users[0].FirstName = firstName; _context.UserProfiles.AddRange(users); await _context.SaveChangesAsync(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1); - result.Users.Should().ContainSingle(user => user.FirstName != null && - user.FirstName.Contains(filter.Name)); + result.Users.Should().ContainSingle(user => user.FirstName == firstName); + } + + [Fact] + public async Task Return_UserSearchResult_With_LastName_Filter() + { + //TODO + // Arrange + string? lastName = _faker.Name.LastName(); + var filter = new UserSearchFilter { Name = lastName }; + var users = CreateTestUsers(5); + // Ensure at least one user has the specified name in their SearchableName + users[0].LastName = lastName; + _context.UserProfiles.AddRange(users); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetUsersAsync(filter); + + // Assert + result.TotalCount.Should().Be(1); + result.Users.Should().ContainSingle(user => user.LastName == lastName); + } + + [Fact] + public async Task Return_UserSearchResult_With_ProfileName_Filter() + { + //TODO + // Arrange + string? userName = _faker.Internet.UserName(); + var filter = new UserSearchFilter { Name = userName }; + var users = CreateTestUsers(5); + // Ensure at least one user has the specified name in their SearchableName + users[0].UserName = userName; + _context.UserProfiles.AddRange(users); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetUsersAsync(filter); + + // Assert + result.TotalCount.Should().Be(1); + result.Users.Should().ContainSingle(user => user.UserName == userName); + } + + [Fact] + public async Task Return_UserSearchResult_With_CombinedName_Filter() + { + // Arrange + string? firstName = _faker.Name.FirstName(); + string? lastName = _faker.Name.LastName(); + + var filter = new UserSearchFilter { Name = $"{firstName} {lastName}" }; + var users = CreateTestUsers(5); + // Ensure at least one user has the specified name in their SearchableName + users[0].FirstName = firstName; + users[0].LastName = lastName; + _context.UserProfiles.AddRange(users); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetUsersAsync(filter); + + // Assert + result.TotalCount.Should().Be(1); + result.Users.Should().ContainSingle(user => user.FirstName == firstName && + user.LastName == lastName); } [Fact] @@ -104,7 +170,7 @@ public async Task Return_UserSearchResult_With_ChildGender_Filter() await _context.SaveChangesAsync(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1); @@ -127,7 +193,7 @@ public async Task Return_UserSearchResult_With_MinimumChildAge_Filter() await _context.SaveChangesAsync(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1); @@ -153,7 +219,7 @@ public async Task Return_UserSearchResult_With_MaximumChildAge_Filter() await _context.SaveChangesAsync(); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1); @@ -189,7 +255,7 @@ public async Task Return_UserSearchResult_With_Location_Filter() new RefitSettings())); // Act - var result = await _sut.GetUsersAsync(filter, CancellationToken.None); + var result = await _sut.GetUsersAsync(filter); // Assert result.TotalCount.Should().Be(1);