Skip to content

Commit

Permalink
Add fakers for implemented schema, add projection helper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iggy808 committed May 20, 2024
1 parent 98b2f76 commit 32b20bc
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/FocusAPI/Repositories/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface IUserRepository
Expression<Func<User, bool>>[]? wherePredicates = null,
CancellationToken cancellationToken = default
);

public Task SaveChangesAsync();
}
3 changes: 2 additions & 1 deletion src/FocusAPI/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace FocusAPI.Repositories;
public class UserRepository : IUserRepository
{
private readonly FocusAPIContext _context;

public UserRepository(FocusAPIContext context)
{
_context = context;
Expand Down Expand Up @@ -37,4 +36,6 @@ public UserRepository(FocusAPIContext context)

return await query.Select(user => ProjectionHelper.ProjectToBaseUser(user)).FirstOrDefaultAsync(cancellationToken);
}

public async Task SaveChangesAsync() => await _context.SaveChangesAsync();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers;
namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserBadgeFaker : Faker<BaseUserBadge>
{
internal BaseUserBadgeFaker(Guid? userId = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers;
namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserDecorFaker : Faker<BaseUserDecor>
{
internal BaseUserDecorFaker(Guid? userId = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers;
namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserFaker : Faker<BaseUser>
{
internal BaseUserFaker()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FocusAPI.Models;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers;
namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserIslandFaker : Faker<BaseUserIsland>
{
internal BaseUserIslandFaker(Guid? userId = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bogus;
using FocusCore.Models;

namespace FocusAPI.Tests.Fakers;
namespace FocusAPI.Tests.Fakers.BaseModels;
internal class BaseUserPetFaker : Faker<BaseUserPet>
{
internal BaseUserPetFaker(Guid? userId = null)
Expand Down
22 changes: 22 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/BadgeFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bogus;
using FocusAPI.Models;


namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class BadgeFaker : Faker<Badge>
{
public BadgeFaker()
{
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]);
}
}
}
22 changes: 22 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/DecorFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class DecorFaker : Faker<Decor>
{
public DecorFaker()
{
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]);
}
}
}
21 changes: 21 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/IslandFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class IslandFaker : Faker<Island>
{
public IslandFaker()
{
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/ImplementedModels/PetFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels
{
internal class PetFaker : Faker<Pet>
{
public PetFaker()
{
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()));
}
}
}
16 changes: 16 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/UserBadgeFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels;
internal class UserBadgeFaker : Faker<UserBadge>
{
internal UserBadgeFaker(Guid? userId = null)
{
BadgeFaker badgeFaker = new();
Badge badge = badgeFaker.Generate();
RuleFor(userBadge => userBadge.UserId, f => userId ??= f.Random.Guid());
RuleFor(userBadge => userBadge.BadgeId, f => badge.Id);
RuleFor(userBadge => userBadge.DateAcquired, f => f.Date.Recent());
RuleFor(userBadge => userBadge.Badge, f => badge);
}
}
15 changes: 15 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/UserDecorFaker.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 UserDecorFaker : Faker<UserDecor>
{
internal UserDecorFaker(Guid? userId = null)
{
DecorFaker decorFaker = new();
Decor decor = decorFaker.Generate();
RuleFor(userDecor => userDecor.UserId, f => userId ??= f.Random.Guid());
RuleFor(userDecor => userDecor.DecorId, f => decor.Id);
RuleFor(userDecor => userDecor.Decor, f => decor);
}
}
39 changes: 39 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/UserFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Bogus;
using FocusAPI.Models;

namespace FocusAPI.Tests.Fakers.ImplementedModels;
internal class UserFaker : Faker<User>
{
internal UserFaker()
{
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]);

UserPetFaker userPetFaker = new(userId);
List<UserPet> userPets = userPetFaker.Generate(2);
RuleFor(user => user.Pets, f => userPets);
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.SelectedIslandId, f => userIslands.First().IslandId);

UserDecorFaker userDecorFaker = new(userId);
List<UserDecor> userDecor = userDecorFaker.Generate(2);
RuleFor(user => user.Decor, f => userDecor);
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.SelectedBadgeId, f => userBadges.First().BadgeId);
}
}
15 changes: 15 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/UserIslandFaker.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 UserIslandFaker : Faker<UserIsland>
{
internal UserIslandFaker(Guid? userId = null)
{
IslandFaker islandFaker = new();
Island island = islandFaker.Generate();
RuleFor(userIsland => userIsland.UserId, f => userId ??= f.Random.Guid());
RuleFor(userIsland => userIsland.IslandId, f => island.Id);
RuleFor(userIsland => userIsland.Island, f => island);
}
}
15 changes: 15 additions & 0 deletions test/FocusAPI.Tests/Fakers/ImplementedModels/UserPetFaker.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 UserPetFaker : Faker<UserPet>
{
internal UserPetFaker(Guid? userId = null)
{
PetFaker petFaker = new();
Pet pet = petFaker.Generate();
RuleFor(userPet => userPet.UserId, f => userId ??= f.Random.Guid());
RuleFor(userPet => userPet.PetId, f => pet.Id);
RuleFor(userPet => userPet.Pet, f => pet);
}
}
112 changes: 112 additions & 0 deletions test/FocusAPI.Tests/ProjectionHelperMethods/ProjectionHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using FocusAPI.Helpers;
using FocusAPI.Models;
using FocusAPI.Tests.Fakers.BaseModels;
using FocusAPI.Tests.Fakers.ImplementedModels;
using FocusCore.Models;
using Shouldly;

namespace FocusAPI.Tests.ProjectionHelperMethods;
public class ProjectionHelperTests
{
UserFaker _userFaker;
BaseUserFaker _baseUserFaker;
public ProjectionHelperTests()
{
SetupTestHelpers();
}

void SetupTestHelpers()
{
_userFaker = new UserFaker();
_baseUserFaker = new BaseUserFaker();
}

[Fact]
public void ProjectToBaseUser_CorrectlyProjectsAllFields()
{
// ARRANGE
User user = _userFaker.Generate();

// ACT
BaseUser projectedUser = ProjectionHelper.ProjectToBaseUser(user);

// ASSERT
projectedUser.Id.ShouldBe(user.Id);
projectedUser.Auth0Id.ShouldBe(user.Auth0Id);
projectedUser.UserName.ShouldBe(user.UserName);
projectedUser.Email.ShouldBe(user.Email);
projectedUser.DateCreated.ShouldBe(user.DateCreated);
projectedUser.Balance.ShouldBe(user.Balance);
projectedUser.Pronouns.ShouldBe(user.Pronouns);
projectedUser.ProfilePicture.ShouldBe(user.ProfilePicture);
projectedUser.SelectedPetId.ShouldBe(user.SelectedPetId);
projectedUser.SelectedIslandId.ShouldBe(user.SelectedIslandId);
projectedUser.SelectedDecorId.ShouldBe(user.SelectedDecorId);
projectedUser.SelectedBadgeId.ShouldBe(user.SelectedBadgeId);

projectedUser.Pets.ShouldNotBeNull();
projectedUser.Pets.Count.ShouldBe(user.Pets.Count);
for (int i = 0; i < projectedUser.Pets.Count; i++)
{
UserPet userPet = user.Pets.ElementAt(i);
BaseUserPet projectedUserPet = projectedUser.Pets.ElementAt(i);
projectedUserPet.UserId.ShouldBe(userPet.UserId);
projectedUserPet.PetId.ShouldBe(userPet.PetId);
/* Check these assertions w/ team - do we want to do a complete mapping of all items when projecting?
projectedUserPet.Pet.Id.ShouldBe(userPet.Pet.Id);
projectedUserPet.Pet.Price.ShouldBe(userPet.Pet.Price);
projectedUserPet.Pet.Name.ShouldBe(userPet.Pet.Name);
projectedUserPet.Pet.Image.ShouldBe(userPet.Pet.Image);
projectedUserPet.Pet.HeightRequest.ShouldBe(userPet.Pet.HeightRequest);
*/
}

projectedUser.Islands.ShouldNotBeNull();
projectedUser.Islands.Count.ShouldBe(user.Islands.Count);
for (int i = 0; i < projectedUser.Islands.Count; i++)
{
UserIsland userIsland = user.Islands.ElementAt(i);
BaseUserIsland projectedUserIsland = projectedUser.Islands.ElementAt(i);
projectedUserIsland.UserId.ShouldBe(userIsland.UserId);
projectedUserIsland.IslandId.ShouldBe(userIsland.IslandId);
/*
projectedUserIsland.Island.Id.ShouldBe(userIsland.Island.Id);
projectedUserIsland.Island.Price.ShouldBe(userIsland.Island.Price);
projectedUserIsland.Island.Name.ShouldBe(userIsland.Island.Name);
projectedUserIsland.Island.Image.ShouldBe(userIsland.Island.Image);
*/
}

projectedUser.Decor.ShouldNotBeNull();
projectedUser.Decor.Count.ShouldBe(user.Decor.Count);
for (int i = 0; i < projectedUser.Decor.Count; i++)
{
UserDecor userDecor = user.Decor.ElementAt(i);
BaseUserDecor projectedUserDecor = projectedUser.Decor.ElementAt(i);
projectedUserDecor.UserId.ShouldBe(userDecor.UserId);
projectedUserDecor.DecorId.ShouldBe(userDecor.DecorId);
/*
projectedUserDecor.Decor.Id.ShouldBe(userDecor.Decor.Id);
projectedUserDecor.Decor.Price.ShouldBe(userDecor.Decor.Price);
projectedUserDecor.Decor.Name.ShouldBe(userDecor.Decor.Name);
projectedUserDecor.Decor.Image.ShouldBe(userDecor.Decor.Image);
projectedUserDecor.Decor.HeightRequest.ShouldBe(userDecor.Decor.HeightRequest);
*/
}

projectedUser.Badges.ShouldNotBeNull();
projectedUser.Badges.Count.ShouldBe(user.Badges.Count);
for (int i = 0; i < projectedUser.Badges.Count; i++)
{
UserBadge userBadge = user.Badges.ElementAt(i);
BaseUserBadge projectedUserBadge = projectedUser.Badges.ElementAt(i);
projectedUserBadge.UserId.ShouldBe(userBadge.UserId);
projectedUserBadge.BadgeId.ShouldBe(userBadge.BadgeId);
/*
projectedUserBadge.Badge.Id.ShouldBe(userBadge.Badge.Id);
projectedUserBadge.Badge.Name.ShouldBe(userBadge.Badge.Name);
projectedUserBadge.Badge.Description.ShouldBe(userBadge.Badge.Description);
*/
}
}
}
2 changes: 1 addition & 1 deletion test/FocusAPI.Tests/UserMethods/GetUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using FocusAPI.Methods.User;
using FocusAPI.Models;
using FocusAPI.Repositories;
using FocusAPI.Tests.Fakers;
using FocusCore.Queries.User;
using Moq;
using Shouldly;
using FocusCore.Models;
using FocusAPI.Tests.Fakers.BaseModels;

namespace FocusAPI.Tests.UserMethods;
public class GetUserTests
Expand Down

0 comments on commit 32b20bc

Please sign in to comment.