Skip to content

Commit

Permalink
Rework tests to use Moq.EFCore, working on ProjectionHelperTests
Browse files Browse the repository at this point in the history
  • Loading branch information
iggy808 committed May 21, 2024
1 parent 32b20bc commit 6212193
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 207 deletions.
22 changes: 14 additions & 8 deletions src/FocusAPI/Methods/User/GetUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
using FocusCore.Queries.User;
using FocusCore.Models;
using MediatR;
using FocusAPI.Helpers;
using FocusCore.Responses;
using FocusCore.Responses.User;
using FocusAPI.Repositories;
using FocusAPI.Data;
using Microsoft.EntityFrameworkCore;

namespace FocusAPI.Methods.User;
public class GetUser
{
public class Handler : IRequestHandler<GetUserQuery, MediatrResultWrapper<GetUserResponse>>
{
IUserRepository _userRepository;
public Handler(IUserRepository userRepository)
IFocusAPIContext _context;
public Handler(IFocusAPIContext context)//IUserRepository userRepository)
{
_userRepository = userRepository;
_context = context;
}

public async Task<MediatrResultWrapper<GetUserResponse>> Handle(
Expand Down Expand Up @@ -61,10 +63,14 @@ public async Task<MediatrResultWrapper<GetUserResponse>> Handle(
{
try
{
return await _userRepository.GetBaseUserWithItemsByAuth0IdAsync(
auth0Id: query.Auth0Id,
cancellationToken: cancellationToken
);
return await _context.Users
.Where(u => u.Auth0Id == query.Auth0Id)
.Include(user => user.Islands)
.Include(user => user.Pets)
.Include(user => user.Decor)
.Include(user => user.Badges)
.Select(user => ProjectionHelper.ProjectToBaseUser(user))
.FirstOrDefaultAsync(cancellationToken);
}
catch (Exception e)
{
Expand Down
15 changes: 0 additions & 15 deletions src/FocusAPI/Repositories/IUserRepository.cs

This file was deleted.

41 changes: 0 additions & 41 deletions src/FocusAPI/Repositories/UserRepository.cs

This file was deleted.

17 changes: 17 additions & 0 deletions test/FocusAPI.Tests/Fakers/BaseModels/BaseBadgeFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Bogus;
using FocusCore.Models;


namespace FocusAPI.Tests.Fakers.BaseModels
{
internal class BaseBadgeFaker : Faker<BaseBadge>
{
public BaseBadgeFaker()
{
RuleFor(badge => badge.Id, f => f.Random.Guid());
RuleFor(badge => badge.Name, f => f.Random.Word());
RuleFor(badge => badge.Description, f => f.Random.Words(4));
RuleFor(badge => badge.Image, f => [0x1]);
}
}
}
17 changes: 17 additions & 0 deletions test/FocusAPI.Tests/Fakers/BaseModels/BaseDecorFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels
{
internal class BaseDecorFaker : Faker<BaseDecor>
{
public BaseDecorFaker()
{
RuleFor(decor => decor.Id, f => f.Random.Guid());
RuleFor(decor => decor.Price, f => Math.Abs(f.Random.Int()));
RuleFor(decor => decor.Name, f => f.Random.Word());
RuleFor(decor => decor.HeightRequest, f => Math.Abs(f.Random.Int()));
RuleFor(decor => decor.Image, f => [0x1]);
}
}
}
15 changes: 15 additions & 0 deletions test/FocusAPI.Tests/Fakers/BaseModels/BaseFriendshipFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels
{
internal class BaseFriendshipFaker : Faker<BaseFriendship>
{
public BaseFriendshipFaker(BaseUser? user = null)
{
RuleFor(friendship => friendship.UserId, f => user == null ? f.Random.Guid() : user.Id);
RuleFor(friendship => friendship.FriendId, f => f.Random.Guid());
RuleFor(friendship => friendship.Status, f => 1);
}
}
}
16 changes: 16 additions & 0 deletions test/FocusAPI.Tests/Fakers/BaseModels/BaseIslandFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels
{
internal class BaseIslandFaker : Faker<BaseIsland>
{
public BaseIslandFaker()
{
RuleFor(island => island.Id, f => f.Random.Guid());
RuleFor(island => island.Price, f => Math.Abs(f.Random.Int()));
RuleFor(island => island.Name, f => f.Random.Word());
RuleFor(island => island.Image, f => [0x1]);
}
}
}
17 changes: 17 additions & 0 deletions test/FocusAPI.Tests/Fakers/BaseModels/BasePetFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class BasePetFaker : Faker<BasePet>
{
public BasePetFaker()
{
RuleFor(pet => pet.Id, f => f.Random.Guid());
RuleFor(pet => pet.Price, f => Math.Abs(f.Random.Int()));
RuleFor(pet => pet.Name, f => f.Random.Word());
RuleFor(pet => pet.Image, f => [0x1]);
RuleFor(pet => pet.HeightRequest, f => Math.Abs(f.Random.Int()));
}
}
}
6 changes: 5 additions & 1 deletion test/FocusAPI.Tests/Fakers/BaseModels/BaseUserBadgeFaker.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Bogus;
using FocusAPI.Tests.Fakers.ImplementedModels;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserBadgeFaker : Faker<BaseUserBadge>
{
internal BaseUserBadgeFaker(Guid? userId = null)
{
BaseBadgeFaker baseBadgeFaker = new();
BaseBadge badge = baseBadgeFaker.Generate();
RuleFor(userBadge => userBadge.UserId, f => userId ??= f.Random.Guid());
RuleFor(userBadge => userBadge.BadgeId, f => f.Random.Guid());
RuleFor(userBadge => userBadge.BadgeId, f => badge.Id);
RuleFor(userBadge => userBadge.Badge, f => badge);
}
}
5 changes: 4 additions & 1 deletion test/FocusAPI.Tests/Fakers/BaseModels/BaseUserDecorFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ internal class BaseUserDecorFaker : Faker<BaseUserDecor>
{
internal BaseUserDecorFaker(Guid? userId = null)
{
BaseDecorFaker baseDecorFaker = new();
BaseDecor decor = baseDecorFaker.Generate();
RuleFor(userDecor => userDecor.UserId, f => userId ??= f.Random.Guid());
RuleFor(userDecor => userDecor.DecorId, f => f.Random.Guid());
RuleFor(userDecor => userDecor.DecorId, f => decor.Id);
RuleFor(userDecor => userDecor.Decor, f => decor);
}
}
35 changes: 27 additions & 8 deletions test/FocusAPI.Tests/Fakers/BaseModels/BaseUserFaker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus;
using FocusAPI.Tests.Fakers.ImplementedModels;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels;
Expand All @@ -9,17 +10,35 @@ internal BaseUserFaker()
Guid userId = Guid.NewGuid();
RuleFor(user => user.Id, f => userId);
RuleFor(user => user.Auth0Id, f => f.Random.Word());
RuleFor(user => user.UserName, f => f.Random.Word());
RuleFor(user => user.Email, f => f.Internet.Email());
RuleFor(user => user.DateCreated, f => f.Date.Recent());
RuleFor(user => user.Balance, f => Math.Abs(f.Random.Int()));
RuleFor(user => user.Pronouns, f => f.Random.Word());
RuleFor(user => user.ProfilePicture, f => [0x1]);

BaseUserPetFaker userPetFaker = new(userId);
RuleFor(user => user.Pets, f => userPetFaker.Generate(2));
BaseUserPetFaker baseUserPetFaker = new(userId);
List<BaseUserPet> baseUserPets = baseUserPetFaker.Generate(2);
RuleFor(user => user.Pets, f => baseUserPets);
RuleFor(user => user.SelectedPet, f => baseUserPets.First().Pet);
RuleFor(user => user.SelectedPetId, f => baseUserPets.First().PetId);

BaseUserIslandFaker userIslandFaker = new(userId);
RuleFor(user => user.Islands, f => userIslandFaker.Generate(2));
BaseUserIslandFaker baseUserIslandFaker = new(userId);
List<BaseUserIsland> baseUserIslands = baseUserIslandFaker.Generate(2);
RuleFor(user => user.Islands, f => baseUserIslands);
RuleFor(user => user.SelectedIsland, f => baseUserIslands.First().Island);
RuleFor(user => user.SelectedIslandId, f => baseUserIslands.First().IslandId);

BaseUserDecorFaker userDecorFaker = new(userId);
RuleFor(user => user.Decor, f => userDecorFaker.Generate(2));
BaseUserDecorFaker baseUserDecorFaker = new(userId);
List<BaseUserDecor> baseUserDecor = baseUserDecorFaker.Generate(2);
RuleFor(user => user.Decor, f => baseUserDecor);
RuleFor(user => user.SelectedDecor, f => baseUserDecor.First().Decor);
RuleFor(user => user.SelectedDecorId, f => baseUserDecor.First().DecorId);

BaseUserBadgeFaker userBadgeFaker = new(userId);
RuleFor(user => user.Badges, f => userBadgeFaker.Generate(2));
BaseUserBadgeFaker baseUserBadgeFaker = new(userId);
List<BaseUserBadge> baseUserBadges = baseUserBadgeFaker.Generate(2);
RuleFor(user => user.Badges, f => baseUserBadges);
RuleFor(user => user.SelectedBadge, f => baseUserBadges.First().Badge);
RuleFor(user => user.SelectedBadgeId, f => baseUserBadges.First().BadgeId);
}
}
5 changes: 4 additions & 1 deletion test/FocusAPI.Tests/Fakers/BaseModels/BaseUserIslandFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ internal class BaseUserIslandFaker : Faker<BaseUserIsland>
{
internal BaseUserIslandFaker(Guid? userId = null)
{
BaseIslandFaker baseIslandFaker = new();
BaseIsland island = baseIslandFaker.Generate();
RuleFor(userIsland => userIsland.UserId, f => userId ??= f.Random.Guid());
RuleFor(userIsland => userIsland.IslandId, f => f.Random.Guid());
RuleFor(userIsland => userIsland.IslandId, f => island.Id);
RuleFor(userIsland => userIsland.Island, f => island);
}
}
6 changes: 5 additions & 1 deletion test/FocusAPI.Tests/Fakers/BaseModels/BaseUserPetFaker.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Bogus;
using FocusAPI.Tests.Fakers.ImplementedModels;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserPetFaker : Faker<BaseUserPet>
{
internal BaseUserPetFaker(Guid? userId = null)
{
BasePetFaker basePetFaker = new();
BasePet pet = basePetFaker.Generate();
RuleFor(userPet => userPet.UserId, f => userId ??= f.Random.Guid());
RuleFor(userPet => userPet.PetId, f => f.Random.Guid());
RuleFor(userPet => userPet.PetId, f => pet.Id);
RuleFor(userPet => userPet.Pet, f => pet);
}
}
15 changes: 15 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/FriendshipFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class FriendshipFaker : Faker<Friendship>
{
public FriendshipFaker(User? user = null)
{
RuleFor(friendship => friendship.UserId, f => user == null ? f.Random.Guid() : user.Id);
RuleFor(friendship => friendship.FriendId, f => f.Random.Guid());
RuleFor(friendship => friendship.Status, f => 1);
}
}
}
6 changes: 5 additions & 1 deletion test/FocusAPI.Tests/Fakers/ImplementedModels/UserFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ internal UserFaker()
UserPetFaker userPetFaker = new(userId);
List<UserPet> userPets = userPetFaker.Generate(2);
RuleFor(user => user.Pets, f => userPets);
RuleFor(user => user.SelectedPet, f => userPets.First().Pet);
RuleFor(user => user.SelectedPetId, f => userPets.First().PetId);

UserIslandFaker userIslandFaker = new(userId);
List<UserIsland> userIslands = userIslandFaker.Generate(2);
RuleFor(user => user.Islands, f => userIslands);
RuleFor(user => user.SelectedIsland, f => userIslands.First().Island);
RuleFor(user => user.SelectedIslandId, f => userIslands.First().IslandId);

UserDecorFaker userDecorFaker = new(userId);
List<UserDecor> userDecor = userDecorFaker.Generate(2);
RuleFor(user => user.Decor, f => userDecor);
RuleFor(user => user.SelectedDecor, f => userDecor.First().Decor);
RuleFor(user => user.SelectedDecorId, f => userDecor.First().DecorId);

UserBadgeFaker userBadgeFaker = new(userId);
List<UserBadge> userBadges = userBadgeFaker.Generate(2);
RuleFor(user => user.Badges, f => userBadges);
RuleFor(user => user.SelectedBadge, f => userBadges.First().Badge);
RuleFor(user => user.SelectedBadgeId, f => userBadges.First().BadgeId);
}
}
1 change: 1 addition & 0 deletions test/FocusAPI.Tests/FocusAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Moq.EntityFrameworkCore" Version="8.0.1.2" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
Expand Down
31 changes: 0 additions & 31 deletions test/FocusAPI.Tests/Helpers/MockSetHelper.cs

This file was deleted.

Loading

0 comments on commit 6212193

Please sign in to comment.