Skip to content

Commit

Permalink
Mock test database iteration 1
Browse files Browse the repository at this point in the history
  • Loading branch information
iggy808 committed May 18, 2024
1 parent 4889f9b commit 6182731
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
17 changes: 16 additions & 1 deletion src/FocusAPI/Data/FocusAPIContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@
using Microsoft.EntityFrameworkCore.Storage;

namespace FocusAPI.Data;
public interface IFocusAPIContext
{
public DbSet<User> Users { get; set; }
public DbSet<Badge> Badges { get; set; }
public DbSet<Pet> Pets { get; set; }
public DbSet<UserBadge> UserBadges { get; set; }
public DbSet<UserPet> UserPets { get; set; }
public DbSet<UserSession> UserSessionHistory { get; set; }
public DbSet<Friendship> Friends { get; set; }
public DbSet<Decor> Decor { get; set; }
public DbSet<UserDecor> UserDecor { get; set; }
public DbSet<MindfulnessTip> MindfulnessTips { get; set; }
public DbSet<Island> Islands { get; set; }
public DbSet<UserIsland> UserIslands { get; set; }
}

public class FocusAPIContext : DbContext
public class FocusAPIContext : DbContext, IFocusAPIContext
{
public DbSet<User> Users { get; set; }
public DbSet<Badge> Badges { get; set; }
Expand Down
5 changes: 3 additions & 2 deletions src/FocusAPI/Methods/User/GetUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class GetUser
{
public class Handler : IRequestHandler<GetUserQuery, MediatrResultWrapper<GetUserResponse>>
{
FocusAPIContext _apiContext;
public Handler(FocusAPIContext apiContext)
IFocusAPIContext _apiContext;
public Handler(IFocusAPIContext apiContext)
{
_apiContext = apiContext;
}
Expand Down Expand Up @@ -63,6 +63,7 @@ public async Task<MediatrResultWrapper<GetUserResponse>> Handle(
{
try
{
var x = _apiContext.Users;
return await _apiContext.Users
.Where(u => u.Auth0Id == query.Auth0Id)
.Include(user => user.Islands)
Expand Down
4 changes: 2 additions & 2 deletions test/FocusAPI.Tests/Helpers/MockSetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace FocusAPI.Tests.Helpers
{
internal static class MockSetHelper
{
internal static void SetupEntities<T>(List<T> sourceList, Mock<FocusAPIContext> context, Expression<Func<FocusAPIContext, DbSet<T>>> setupExpression) where T : class
internal static void SetupEntities<T>(List<T> sourceList, Mock<TestAPIContext> context, Expression<Func<TestAPIContext, DbSet<T>>> setupExpression) where T : class
{
var queryable = sourceList.AsQueryable();

Expand All @@ -23,7 +23,7 @@ internal static void SetupEntities<T>(List<T> sourceList, Mock<FocusAPIContext>
dbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator);
dbSet.Setup(d => d.Add(It.IsAny<T>())).Callback<T>(sourceList.Add);

context.Setup(db => db.Set<T>()).Returns(dbSet.Object);
context.Setup(setupExpression).Returns(dbSet.Object);
}
}
}
27 changes: 27 additions & 0 deletions test/FocusAPI.Tests/Helpers/TestAPIContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FocusAPI.Data;
using FocusAPI.Models;
using Microsoft.EntityFrameworkCore;

namespace FocusAPI.Tests.Helpers;
public class TestAPIContext : DbContext, IFocusAPIContext
{
public DbSet<User> Users { get; set; }
public DbSet<Badge> Badges { get; set; }
public DbSet<Pet> Pets { get; set; }
public DbSet<UserBadge> UserBadges { get; set; }
public DbSet<UserPet> UserPets { get; set; }
public DbSet<UserSession> UserSessionHistory { get; set; }
public DbSet<Friendship> Friends { get; set; }
public DbSet<Decor> Decor { get; set; }
public DbSet<UserDecor> UserDecor { get; set; }
public DbSet<MindfulnessTip> MindfulnessTips { get; set; }
public DbSet<Island> Islands { get; set; }
public DbSet<UserIsland> UserIslands { get; set; }

public TestAPIContext(DbContextOptions<TestAPIContext> options) : base(options) { }
}
9 changes: 4 additions & 5 deletions test/FocusAPI.Tests/UserMethods/GetUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace FocusAPI.Tests.UserMethods;
public class GetUserTests
{
Mock<FocusAPIContext> _context;
Mock<TestAPIContext> _context;
UserFaker _userFaker;
public GetUserTests()
{
Expand All @@ -28,8 +28,8 @@ void SetupTestHelpers()

void SetupSystemDependencies()
{
DbContextOptionsBuilder<FocusAPIContext> optionsBuilder = new();
_context = new Mock<FocusAPIContext>(optionsBuilder.Options);
DbContextOptionsBuilder<TestAPIContext> optionsBuilder = new();
_context = new Mock<TestAPIContext>(optionsBuilder.Options);
}

void SetupMocks(List<User> users)
Expand All @@ -43,14 +43,13 @@ public async Task GetUser_ReturnsNotFound_WhenUserIsNull()
{
// ARRANGE
// Set up test data, test mocks, and system under test
User user = _userFaker.Generate();
SetupMocks([]);
GetUser.Handler handler = new(_context.Object);

// ACT
var result = await handler.Handle(new GetUserQuery
{
Auth0Id = user.Auth0Id
Auth0Id = ""
});

// ASSERT
Expand Down

0 comments on commit 6182731

Please sign in to comment.