Skip to content

Commit

Permalink
fix searching by name
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Sep 19, 2023
1 parent 4182d64 commit 33e9cb1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
8 changes: 3 additions & 5 deletions src/web/Server/Features/UserSearch/UserSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Jordnaer.Server.Features.UserSearch;

public interface IUserSearchService
{
Task<UserSearchResult> GetUsersAsync(UserSearchFilter filter, CancellationToken cancellationToken);
Task<List<UserSlim>> GetUsersByNameAsync(string searchString, string omitById, CancellationToken cancellationToken);
Task<UserSearchResult> GetUsersAsync(UserSearchFilter filter, CancellationToken cancellationToken = default);
Task<List<UserSlim>> GetUsersByNameAsync(string searchString, string omitById, CancellationToken cancellationToken = default);
}

public class UserSearchService : IUserSearchService
Expand Down Expand Up @@ -184,10 +184,8 @@ private static IQueryable<UserProfile> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserSearchResult>();
Expand All @@ -60,31 +60,97 @@ 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);
result.Users.Should().ContainSingle(u => u.LookingFor.Contains(filter.LookingFor.First()));
}

[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]
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 33e9cb1

Please sign in to comment.