From 74b97fd43b61f7e2e7209bc0c907519773d35cdd Mon Sep 17 00:00:00 2001 From: Lucky Bhoir Date: Thu, 26 Mar 2026 15:04:01 +0530 Subject: [PATCH] Added Icon field to Goal model and xUnit tests --- CommBank-Server/CommBank.csproj | 2 +- CommBank-Server/Models/Goal.cs | 2 + CommBank-Server/Secrets.json | 2 +- CommBank-Server/appsettings.json | 9 +- CommBank.Tests/GoalControllerTests.cs | 113 ++++++++++++-------------- 5 files changed, 62 insertions(+), 66 deletions(-) diff --git a/CommBank-Server/CommBank.csproj b/CommBank-Server/CommBank.csproj index 983cc88..3e5aaa8 100644 --- a/CommBank-Server/CommBank.csproj +++ b/CommBank-Server/CommBank.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable CommBank_Server diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..81f0192 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; } diff --git a/CommBank-Server/Secrets.json b/CommBank-Server/Secrets.json index 0e5bf94..85f0112 100644 --- a/CommBank-Server/Secrets.json +++ b/CommBank-Server/Secrets.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "CommBank": "{CONNECTION_STRING}" + "CommBank": "mongodb+srv://luckybhoir2230_db_user:Lucky9345@cluster0.sgrd1ox.mongodb.net/CommBank?retryWrites=true&w=majority&appName=Cluster0" } } \ No newline at end of file diff --git a/CommBank-Server/appsettings.json b/CommBank-Server/appsettings.json index af0538f..0acaa10 100644 --- a/CommBank-Server/appsettings.json +++ b/CommBank-Server/appsettings.json @@ -5,6 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} - + "AllowedHosts": "*", + "ConnectionStrings": { + "CommBank": "mongodb+srv://luckybhoir2230_db_user:Lucky9345@cluster0.sgrd1ox.mongodb.net/CommBank?retryWrites=true&w=majority&appName=Cluster0" + }, + "DatabaseName": "CommBank" +} \ No newline at end of file diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..00633c7 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -1,74 +1,65 @@ -using CommBank.Controllers; -using CommBank.Services; -using CommBank.Models; -using CommBank.Tests.Fake; +using Xunit; +using Moq; using Microsoft.AspNetCore.Mvc; +using CommBank.Controllers; +using CommBank.Models; +using CommBank.Services; +using System.Collections.Generic; +using System.Threading.Tasks; -namespace CommBank.Tests; - -public class GoalControllerTests +namespace CommBank.Tests { - private readonly FakeCollections collections; - - public GoalControllerTests() + public class GoalControllerTests { - collections = new(); - } - - [Fact] - public async void GetAll() - { - // Arrange - var goals = collections.GetGoals(); - var users = collections.GetUsers(); - IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); - IUsersService usersService = new FakeUsersService(users, users[0]); - GoalController controller = new(goalsService, usersService); + private readonly Mock _mockGoalsService; + private readonly GoalController _controller; - // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(); - - // Assert - var index = 0; - foreach (Goal goal in result) + public GoalControllerTests() { - Assert.IsAssignableFrom(goal); - Assert.Equal(goals[index].Id, goal.Id); - Assert.Equal(goals[index].Name, goal.Name); - index++; + // We "Mock" the service so we don't need a real database to run tests + _mockGoalsService = new Mock(); + _controller = new GoalController(_mockGoalsService.Object); } - } - [Fact] - public async void Get() - { - // Arrange - var goals = collections.GetGoals(); - var users = collections.GetUsers(); - IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); - IUsersService usersService = new FakeUsersService(users, users[0]); - GoalController controller = new(goalsService, usersService); + [Fact] + public async Task GetGoalsForUser_ReturnsOkResult_WithListOfGoals() + { + // 1. ARRANGE + var userId = "test-user-123"; + var fakeGoals = new List + { + new Goal { Id = "1", Name = "Holiday", Icon = "✈️", UserId = userId }, + new Goal { Id = "2", Name = "Savings", Icon = "💰", UserId = userId } + }; - // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(goals[0].Id!); + _mockGoalsService.Setup(service => service.GetGoalsForUser(userId)) + .ReturnsAsync(fakeGoals); - // Assert - Assert.IsAssignableFrom(result.Value); - Assert.Equal(goals[0], result.Value); - Assert.NotEqual(goals[1], result.Value); - } + // 2. ACT + var result = await _controller.GetGoalsForUser(userId); - [Fact] - public async void GetForUser() - { - // Arrange - - // Act - - // Assert + // 3. ASSERT + var okResult = Assert.IsType(result); + var returnedGoals = Assert.IsType>(okResult.Value); + Assert.Equal(2, returnedGoals.Count); + Assert.Equal("✈️", returnedGoals[0].Icon); // Verifying our new Icon field! + } + + [Fact] + public async Task GetGoalsForUser_ReturnsEmptyList_WhenNoGoalsExist() + { + // ARRANGE + var userId = "empty-user"; + _mockGoalsService.Setup(service => service.GetGoalsForUser(userId)) + .ReturnsAsync(new List()); + + // ACT + var result = await _controller.GetGoalsForUser(userId); + + // ASSERT + var okResult = Assert.IsType(result); + var returnedGoals = Assert.IsType>(okResult.Value); + Assert.Empty(returnedGoals); + } } } \ No newline at end of file