From 6182731a4496a0dc1aede43c93c48a1704f451f4 Mon Sep 17 00:00:00 2001 From: egray Date: Sat, 18 May 2024 13:15:12 -0500 Subject: [PATCH] Mock test database iteration 1 --- src/FocusAPI/Data/FocusAPIContext.cs | 17 +++++++++++- src/FocusAPI/Methods/User/GetUser.cs | 5 ++-- test/FocusAPI.Tests/Helpers/MockSetHelper.cs | 4 +-- test/FocusAPI.Tests/Helpers/TestAPIContext.cs | 27 +++++++++++++++++++ .../UserMethods/GetUserTests.cs | 9 +++---- 5 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 test/FocusAPI.Tests/Helpers/TestAPIContext.cs diff --git a/src/FocusAPI/Data/FocusAPIContext.cs b/src/FocusAPI/Data/FocusAPIContext.cs index b7437670..6156e494 100644 --- a/src/FocusAPI/Data/FocusAPIContext.cs +++ b/src/FocusAPI/Data/FocusAPIContext.cs @@ -4,8 +4,23 @@ using Microsoft.EntityFrameworkCore.Storage; namespace FocusAPI.Data; +public interface IFocusAPIContext +{ + public DbSet Users { get; set; } + public DbSet Badges { get; set; } + public DbSet Pets { get; set; } + public DbSet UserBadges { get; set; } + public DbSet UserPets { get; set; } + public DbSet UserSessionHistory { get; set; } + public DbSet Friends { get; set; } + public DbSet Decor { get; set; } + public DbSet UserDecor { get; set; } + public DbSet MindfulnessTips { get; set; } + public DbSet Islands { get; set; } + public DbSet UserIslands { get; set; } +} -public class FocusAPIContext : DbContext +public class FocusAPIContext : DbContext, IFocusAPIContext { public DbSet Users { get; set; } public DbSet Badges { get; set; } diff --git a/src/FocusAPI/Methods/User/GetUser.cs b/src/FocusAPI/Methods/User/GetUser.cs index f7ca7fc1..09f29b17 100644 --- a/src/FocusAPI/Methods/User/GetUser.cs +++ b/src/FocusAPI/Methods/User/GetUser.cs @@ -13,8 +13,8 @@ public class GetUser { public class Handler : IRequestHandler> { - FocusAPIContext _apiContext; - public Handler(FocusAPIContext apiContext) + IFocusAPIContext _apiContext; + public Handler(IFocusAPIContext apiContext) { _apiContext = apiContext; } @@ -63,6 +63,7 @@ public async Task> Handle( { try { + var x = _apiContext.Users; return await _apiContext.Users .Where(u => u.Auth0Id == query.Auth0Id) .Include(user => user.Islands) diff --git a/test/FocusAPI.Tests/Helpers/MockSetHelper.cs b/test/FocusAPI.Tests/Helpers/MockSetHelper.cs index 31eb7e72..74fee938 100644 --- a/test/FocusAPI.Tests/Helpers/MockSetHelper.cs +++ b/test/FocusAPI.Tests/Helpers/MockSetHelper.cs @@ -12,7 +12,7 @@ namespace FocusAPI.Tests.Helpers { internal static class MockSetHelper { - internal static void SetupEntities(List sourceList, Mock context, Expression>> setupExpression) where T : class + internal static void SetupEntities(List sourceList, Mock context, Expression>> setupExpression) where T : class { var queryable = sourceList.AsQueryable(); @@ -23,7 +23,7 @@ internal static void SetupEntities(List sourceList, Mock dbSet.As>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator); dbSet.Setup(d => d.Add(It.IsAny())).Callback(sourceList.Add); - context.Setup(db => db.Set()).Returns(dbSet.Object); + context.Setup(setupExpression).Returns(dbSet.Object); } } } diff --git a/test/FocusAPI.Tests/Helpers/TestAPIContext.cs b/test/FocusAPI.Tests/Helpers/TestAPIContext.cs new file mode 100644 index 00000000..d4785601 --- /dev/null +++ b/test/FocusAPI.Tests/Helpers/TestAPIContext.cs @@ -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 Users { get; set; } + public DbSet Badges { get; set; } + public DbSet Pets { get; set; } + public DbSet UserBadges { get; set; } + public DbSet UserPets { get; set; } + public DbSet UserSessionHistory { get; set; } + public DbSet Friends { get; set; } + public DbSet Decor { get; set; } + public DbSet UserDecor { get; set; } + public DbSet MindfulnessTips { get; set; } + public DbSet Islands { get; set; } + public DbSet UserIslands { get; set; } + + public TestAPIContext(DbContextOptions options) : base(options) { } +} \ No newline at end of file diff --git a/test/FocusAPI.Tests/UserMethods/GetUserTests.cs b/test/FocusAPI.Tests/UserMethods/GetUserTests.cs index fb08b10a..747462ed 100644 --- a/test/FocusAPI.Tests/UserMethods/GetUserTests.cs +++ b/test/FocusAPI.Tests/UserMethods/GetUserTests.cs @@ -13,7 +13,7 @@ namespace FocusAPI.Tests.UserMethods; public class GetUserTests { - Mock _context; + Mock _context; UserFaker _userFaker; public GetUserTests() { @@ -28,8 +28,8 @@ void SetupTestHelpers() void SetupSystemDependencies() { - DbContextOptionsBuilder optionsBuilder = new(); - _context = new Mock(optionsBuilder.Options); + DbContextOptionsBuilder optionsBuilder = new(); + _context = new Mock(optionsBuilder.Options); } void SetupMocks(List users) @@ -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