-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented FindAsync user method. (#37)
* Refactored UserNotFoundException. * Implemented FoundUsers. * Implemented FindAsync user method. * Version bump.
- Loading branch information
Showing
7 changed files
with
363 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
namespace Logitar.Identity.Domain.Users; | ||
|
||
/// <summary> | ||
/// The results of an user search. | ||
/// </summary> | ||
public record FoundUsers | ||
{ | ||
/// <summary> | ||
/// Gets or sets the user found by unique identifier. | ||
/// </summary> | ||
public UserAggregate? ById { get; set; } | ||
/// <summary> | ||
/// Gets or sets the user found by unique name. | ||
/// </summary> | ||
public UserAggregate? ByUniqueName { get; set; } | ||
/// <summary> | ||
/// Gets or sets the user found by email address. | ||
/// </summary> | ||
public UserAggregate? ByEmail { get; set; } | ||
|
||
/// <summary> | ||
/// Gets all found users. | ||
/// </summary> | ||
public IEnumerable<UserAggregate> All | ||
{ | ||
get | ||
{ | ||
List<UserAggregate> users = new(capacity: 3); | ||
|
||
if (ById != null) | ||
{ | ||
users.Add(ById); | ||
} | ||
if (ByUniqueName != null) | ||
{ | ||
users.Add(ByUniqueName); | ||
} | ||
if (ByEmail != null) | ||
{ | ||
users.Add(ByEmail); | ||
} | ||
|
||
return users.AsReadOnly(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns the first user found, ordered by unique identifier, then by unique name, and then by email address. | ||
/// </summary> | ||
/// <returns>The first user found.</returns> | ||
public UserAggregate First() => All.First(); | ||
/// <summary> | ||
/// Returns the first user found, ordered by unique identifier, then by unique name, and then by email address. | ||
/// </summary> | ||
/// <returns>The first user found, or null if none were found.</returns> | ||
public UserAggregate? FirstOrDefault() => All.FirstOrDefault(); | ||
|
||
/// <summary> | ||
/// Returns the single user found. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">More than one users have been found.</exception> | ||
/// <returns>The single user found.</returns> | ||
public UserAggregate Single() => All.Single(); | ||
/// <summary> | ||
/// Returns the single user found. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">More than one users have been found.</exception> | ||
/// <returns>The single user found, or null if none were found.</returns> | ||
public UserAggregate? SingleOrDefault() => All.SingleOrDefault(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
tests/Logitar.Identity.Domain.UnitTests/Users/FoundUsersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using Bogus; | ||
using Logitar.Identity.Domain.Settings; | ||
using Logitar.Identity.Domain.Shared; | ||
|
||
namespace Logitar.Identity.Domain.Users; | ||
|
||
[Trait(Traits.Category, Categories.Unit)] | ||
public class FoundUsersTests | ||
{ | ||
private readonly Faker _faker = new(); | ||
private readonly UniqueNameSettings _uniqueNameSettings = new(); | ||
|
||
[Fact(DisplayName = "All: it should return all the users found.")] | ||
public void All_it_should_return_all_the_users_found() | ||
{ | ||
FoundUsers users = new(); | ||
Assert.Empty(users.All); | ||
|
||
UserAggregate byId = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.UserName)); | ||
UserAggregate byEmail = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.Email)); | ||
users = new() | ||
{ | ||
ById = byId, | ||
ByEmail = byEmail | ||
}; | ||
|
||
IEnumerable<UserAggregate> all = users.All; | ||
Assert.Equal(2, all.Count()); | ||
Assert.Contains(byId, all); | ||
Assert.Contains(byEmail, all); | ||
} | ||
|
||
[Fact(DisplayName = "First: it should return the first user found.")] | ||
public void First_it_should_return_the_first_user_found() | ||
{ | ||
UserAggregate byUniqueName = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.UserName)); | ||
UserAggregate byEmail = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.Email)); | ||
FoundUsers users = new() | ||
{ | ||
ByUniqueName = byUniqueName, | ||
ByEmail = byEmail | ||
}; | ||
|
||
UserAggregate first = users.First(); | ||
Assert.Equal(byUniqueName, first); | ||
} | ||
|
||
[Fact(DisplayName = "FirstOrDefault: it should return the first user found or null if none found.")] | ||
public void FirstOrDefault_it_should_return_the_first_user_found_or_null_if_none_found() | ||
{ | ||
FoundUsers users = new(); | ||
Assert.Null(users.FirstOrDefault()); | ||
|
||
UserAggregate byUniqueName = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.UserName)); | ||
UserAggregate byEmail = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.Email)); | ||
users = new() | ||
{ | ||
ByUniqueName = byUniqueName, | ||
ByEmail = byEmail | ||
}; | ||
|
||
UserAggregate? first = users.FirstOrDefault(); | ||
Assert.NotNull(first); | ||
Assert.Equal(byUniqueName, first); | ||
} | ||
|
||
[Fact(DisplayName = "Single: it should return the only user found.")] | ||
public void Single_it_should_return_the_only_user_found() | ||
{ | ||
UserAggregate user = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.UserName)); | ||
FoundUsers users = new() | ||
{ | ||
ById = user | ||
}; | ||
|
||
UserAggregate single = users.Single(); | ||
Assert.Equal(user, single); | ||
} | ||
|
||
[Fact(DisplayName = "SingleOrDefault: it should return the only user found or null if none found.")] | ||
public void SingleOrDefault_it_should_return_the_only_user_found_or_null_if_none_found() | ||
{ | ||
FoundUsers users = new(); | ||
Assert.Null(users.SingleOrDefault()); | ||
|
||
UserAggregate byId = new(new UniqueNameUnit(_uniqueNameSettings, _faker.Person.UserName)); | ||
users = new() | ||
{ | ||
ById = byId | ||
}; | ||
|
||
UserAggregate? single = users.SingleOrDefault(); | ||
Assert.NotNull(single); | ||
Assert.Equal(byId, single); | ||
} | ||
} |
Oops, something went wrong.