From d50145b38d49d598c453e2a8c514df08a00920fb Mon Sep 17 00:00:00 2001 From: KaranChadha10 Date: Fri, 4 Apr 2025 01:34:36 +0530 Subject: [PATCH 1/2] test: added test cases for helper classes --- .../GlobalUsings.cs | 1 + ...e.Module.WishList.Tests.Controllers.csproj | 32 ++++++++ .../Helpers/CurrencyHelper.cs | 5 ++ .../CurrencyHelperTests.cs | 51 ++++++++++++ .../ReflectionHelperTests.cs | 79 +++++++++++++++++++ .../Controllers/MenuApiControllerTests.cs | 47 +++++++++++ 6 files changed, 215 insertions(+) create mode 100644 SimplCommerce.Module.WishList.Tests.Controllers/GlobalUsings.cs create mode 100644 SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj create mode 100644 test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs create mode 100644 test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs create mode 100644 test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs diff --git a/SimplCommerce.Module.WishList.Tests.Controllers/GlobalUsings.cs b/SimplCommerce.Module.WishList.Tests.Controllers/GlobalUsings.cs new file mode 100644 index 0000000000..9df1d42179 --- /dev/null +++ b/SimplCommerce.Module.WishList.Tests.Controllers/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; diff --git a/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj b/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj new file mode 100644 index 0000000000..3c7ee10f6e --- /dev/null +++ b/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/src/SimplCommerce.Infrastructure/Helpers/CurrencyHelper.cs b/src/SimplCommerce.Infrastructure/Helpers/CurrencyHelper.cs index 9c82c5a648..66ecfbc800 100644 --- a/src/SimplCommerce.Infrastructure/Helpers/CurrencyHelper.cs +++ b/src/SimplCommerce.Infrastructure/Helpers/CurrencyHelper.cs @@ -7,6 +7,11 @@ public static class CurrencyHelper { private static readonly List _zeroDecimalCurrencies = new List { "BIF", "DJF", "JPY", "KRW", "PYG", "VND", "XAF", "XPF", "CLP", "GNF", "KMF", "MGA", "RWF", "VUV", "XOF" }; + /// + /// Determines whether the currency associated with the specified culture uses decimal places. + /// + /// The culture to check for its associated currency format. + /// true if the currency is a known zero-decimal currency else false public static bool IsZeroDecimalCurrencies(CultureInfo cultureInfo) { var regionInfo = new RegionInfo(cultureInfo.LCID); diff --git a/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs new file mode 100644 index 0000000000..68fab8dc7f --- /dev/null +++ b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Globalization; +using Xunit; +using SimplCommerce.Infrastructure.Helpers; + +namespace SimplCommerce.Infrastructure.Tests +{ + /// + /// Contains unit tests for method. + /// + public class CurrencyHelperTests + { + /// + /// Verifies that zero-decimal currencies are correctly identified. + /// + [Theory] + [InlineData("ja-JP", true)] // Japanese Yen (JPY) + [InlineData("ko-KR", true)] // Korean Won (KRW) + [InlineData("en-US", false)] // US Dollar (USD) + [InlineData("fr-FR", false)] // Euro (EUR) + public void IsZeroDecimalCurrencies_ReturnsExpectedResult(string cultureName, bool expectedResult) + { + // Arrange + var cultureInfo = new CultureInfo(cultureName); + + // Act + var result = CurrencyHelper.IsZeroDecimalCurrencies(cultureInfo); + + // Assert + Assert.Equal(expectedResult, result); + } + + /// + /// Verifies that an exception is thrown when an invalid culture is provided. + /// + [Fact] + public void IsZeroDecimalCurrencies_InvalidCulture_ThrowsException() + { + // Arrange + var invalidCulture = CultureInfo.InvariantCulture; + + // Act & Assert + Assert.Throws(() => + CurrencyHelper.IsZeroDecimalCurrencies(invalidCulture)); + } + } +} diff --git a/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs new file mode 100644 index 0000000000..84dbfe7a39 --- /dev/null +++ b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SimplCommerce.Infrastructure.Helpers; +using Xunit; + +namespace SimplCommerce.Infrastructure.Tests +{ + /// + /// Contains unit tests for method. + /// + public class ReflectionHelperTests + { + [Fact] + public void IsAssignableToGenericType_TypeIsAssignable_ReturnsTrue() + { + // Arrange + var targetType = typeof(List); + var genericType = typeof(IEnumerable<>); + + // Act + var result = ReflectionHelper.IsAssignableToGenericType(targetType, genericType); + + // Assert + Assert.True(result); + } + + [Fact] + public void IsAssignableToGenericType_TypeIsNotAssignable_ReturnsFalse() + { + // Arrange + var targetType = typeof(string); + var genericType = typeof(IEnumerable<>); + + // Act + var result = ReflectionHelper.IsAssignableToGenericType(targetType, genericType); + + // Assert + Assert.True(result); + } + + [Fact] + public void IsAssignableToGenericType_TypeInheritsGenericInterface_ReturnsTrue() + { + // Arrange + var targetType = typeof(MyClass); + var genericType = typeof(IGenericInterface<>); + + // Act + var result = ReflectionHelper.IsAssignableToGenericType(targetType, genericType); + + // Assert + Assert.True(result); + } + + [Fact] + public void IsAssignableToGenericType_TypeDoesNotInheritGenericInterface_ReturnsFalse() + { + // Arrange + var targetType = typeof(MyClass); + var genericType = typeof(INonGenericInterface); + + // Act + var result = ReflectionHelper.IsAssignableToGenericType(targetType, genericType); + + // Assert + Assert.False(result); + } + + // Example classes/interfaces for testing + public class MyClass : IGenericInterface { } + + public interface IGenericInterface { } + + public interface INonGenericInterface { } + } +} diff --git a/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs b/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs new file mode 100644 index 0000000000..119f2e4ac2 --- /dev/null +++ b/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Moq; +using SimplCommerce.Infrastructure.Data; +using SimplCommerce.Module.Cms.Areas.Cms.Controllers; +using SimplCommerce.Module.Cms.Areas.Cms.ViewModels; +using SimplCommerce.Module.Cms.Models; +using Xunit; + +namespace SimplCommerce.Module.Cms.Tests.Controllers +{ + /// + /// Contains unit tests for method. + /// + public class MenuApiControllerTests + { + [Fact] + public async Task Post_CreatesMenu() + { + // Arrange + var menuRepositoryMock = new Mock>(); + var menuItemRepositoryMock = new Mock>(); + + var controller = new MenuApiController(menuRepositoryMock.Object, menuItemRepositoryMock.Object); + + var menuForm = new MenuForm + { + Name = "NewMenu", + IsPublished = true + }; + + // Act + var result = await controller.Post(menuForm); + + // Assert + var okResult = Assert.IsType(result); + var createdMenu = Assert.IsType(okResult.Value); + Assert.Equal("NewMenu", createdMenu.Name); + Assert.True(createdMenu.IsPublished); + } + } +} From 1e14784d52c3c7b36dd90256c2850693f664c913 Mon Sep 17 00:00:00 2001 From: KaranChadha10 Date: Fri, 4 Apr 2025 08:10:28 +0530 Subject: [PATCH 2/2] fix: addressed pr comments --- ...implCommerce.Module.WishList.Tests.Controllers.csproj | 1 - .../CurrencyHelperTests.cs | 9 --------- .../ReflectionHelperTests.cs | 3 --- .../Controllers/MenuApiControllerTests.cs | 3 --- 4 files changed, 16 deletions(-) diff --git a/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj b/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj index 3c7ee10f6e..d6b402a0e3 100644 --- a/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj +++ b/SimplCommerce.Module.WishList.Tests.Controllers/SimplCommerce.Module.WishList.Tests.Controllers.csproj @@ -4,7 +4,6 @@ net8.0 enable enable - false true diff --git a/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs index 68fab8dc7f..e21672bceb 100644 --- a/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs +++ b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs @@ -9,14 +9,8 @@ namespace SimplCommerce.Infrastructure.Tests { - /// - /// Contains unit tests for method. - /// public class CurrencyHelperTests { - /// - /// Verifies that zero-decimal currencies are correctly identified. - /// [Theory] [InlineData("ja-JP", true)] // Japanese Yen (JPY) [InlineData("ko-KR", true)] // Korean Won (KRW) @@ -34,9 +28,6 @@ public void IsZeroDecimalCurrencies_ReturnsExpectedResult(string cultureName, bo Assert.Equal(expectedResult, result); } - /// - /// Verifies that an exception is thrown when an invalid culture is provided. - /// [Fact] public void IsZeroDecimalCurrencies_InvalidCulture_ThrowsException() { diff --git a/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs index 84dbfe7a39..c126c1755d 100644 --- a/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs +++ b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs @@ -8,9 +8,6 @@ namespace SimplCommerce.Infrastructure.Tests { - /// - /// Contains unit tests for method. - /// public class ReflectionHelperTests { [Fact] diff --git a/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs b/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs index 119f2e4ac2..62dcf906c0 100644 --- a/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs +++ b/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs @@ -14,9 +14,6 @@ namespace SimplCommerce.Module.Cms.Tests.Controllers { - /// - /// Contains unit tests for method. - /// public class MenuApiControllerTests { [Fact]