From 5ea07cabbaad404ba650a698d1ee1ecab5f0e2b0 Mon Sep 17 00:00:00 2001 From: Amulya Surya Date: Thu, 15 Jan 2026 20:02:15 +0530 Subject: [PATCH] Add goal icon support and tests for GetGoalsForUser --- CommBank-Server/Models/Goal.cs | 4 +- CommBank.Tests/GoalControllerTests.cs | 89 ++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..96288b8 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -11,6 +11,8 @@ public class Goal public string? Name { get; set; } + public string? Icon { get; set; } + public UInt64 TargetAmount { get; set; } = 0; public DateTime TargetDate { get; set; } @@ -27,4 +29,4 @@ public class Goal [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } -} \ No newline at end of file +} diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..f2c3406 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -3,6 +3,7 @@ using CommBank.Models; using CommBank.Tests.Fake; using Microsoft.AspNetCore.Mvc; +using Xunit; namespace CommBank.Tests; @@ -63,12 +64,98 @@ public async void Get() } [Fact] - public async void GetForUser() + public async void GetForUser_ReturnsGoalsWithIcons() { // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + + // Add icons to test data + goals[0].Icon = "🎯"; + goals[1].Icon = "💰"; + goals[2].Icon = null; // Test null icon + + var user = users[0]; + + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, user); + + GoalController controller = new(goalsService, usersService); + + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + + // Act + var result = await controller.GetForUser(user.Id!); + + // Assert + var okResult = Assert.IsType(result); + var returnedGoals = Assert.IsAssignableFrom>(okResult.Value); + + Assert.NotNull(returnedGoals); + Assert.NotEmpty(returnedGoals); + + foreach (var goal in returnedGoals) + { + Assert.Equal(user.Id, goal.UserId); + + Assert.True(goal.Icon == null || !string.IsNullOrEmpty(goal.Icon)); + } + + + var goalList = returnedGoals.ToList(); + Assert.Contains(goalList, g => g.Icon == "🎯"); + Assert.Contains(goalList, g => g.Icon == "💰"); + Assert.Contains(goalList, g => g.Icon == null); + } + + [Fact] + public async void GetForUser_UserWithNoGoals_ReturnsEmptyList() + { + // Arrange + var goals = new List(); + var users = collections.GetUsers(); + var user = users[1]; + + IGoalsService goalsService = new FakeGoalsService(goals, null); + IUsersService usersService = new FakeUsersService(users, user); + + GoalController controller = new(goalsService, usersService); + + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + // Act + var result = await controller.GetForUser(user.Id!); + + // Assert + var okResult = Assert.IsType(result); + var returnedGoals = Assert.IsAssignableFrom>(okResult.Value); + + Assert.NotNull(returnedGoals); + Assert.Empty(returnedGoals); + } + + [Fact] + public async void GetForUser_InvalidUserId_ReturnsNotFound() + { + // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, null); // User not found + + GoalController controller = new(goalsService, usersService); + + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + + // Act + var result = await controller.GetForUser("invalid-user-id"); + // Assert + Assert.IsType(result); } } \ No newline at end of file