From ea956b983ef1ba136e61f252e376443472e1c013 Mon Sep 17 00:00:00 2001 From: "Samarth.Gupta" Date: Tue, 9 Jan 2024 07:17:14 +0000 Subject: [PATCH] infrastrucutre test cases --- .../GlobalUsings.cs | 1 + ...e.Module.WishList.Tests.Controllers.csproj | 32 ++++++++ .../WishListControllerTests.cs | 1 + SimplCommerce.sln | 15 ++++ global.json | 2 +- .../CarouselWidgetApiController.cs | 10 +-- src/SimplCommerce.WebHost/appsettings.json | 3 +- .../CurrencyHelperTests.cs | 58 ++++++++++++++ .../ReflectionHelperTests.cs | 76 +++++++++++++++++++ .../Controllers/MenuApiControllerTests.cs | 53 +++++++++++++ ...efaultShippingAddressViewComponentTests.cs | 11 +-- 11 files changed, 250 insertions(+), 12 deletions(-) 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 SimplCommerce.Module.WishList.Tests.Controllers/WishListControllerTests.cs 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..8c927eb747 --- /dev/null +++ b/SimplCommerce.Module.WishList.Tests.Controllers/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file 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/SimplCommerce.Module.WishList.Tests.Controllers/WishListControllerTests.cs b/SimplCommerce.Module.WishList.Tests.Controllers/WishListControllerTests.cs new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/SimplCommerce.Module.WishList.Tests.Controllers/WishListControllerTests.cs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/SimplCommerce.sln b/SimplCommerce.sln index 2c53621439..a78c0044ad 100644 --- a/SimplCommerce.sln +++ b/SimplCommerce.sln @@ -136,6 +136,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Paymen EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Checkouts", "src\Modules\SimplCommerce.Module.Checkouts\SimplCommerce.Module.Checkouts.csproj", "{4473538D-2BFA-4C53-B642-0D0DC4F16863}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.WishList.Tests.Controllers", "SimplCommerce.Module.WishList.Tests.Controllers\SimplCommerce.Module.WishList.Tests.Controllers.csproj", "{FB134E1E-A785-4EC7-97E8-A4F260A633DC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -710,6 +712,18 @@ Global {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x64.Build.0 = Release|Any CPU {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x86.ActiveCfg = Release|Any CPU {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x86.Build.0 = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|x64.ActiveCfg = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|x64.Build.0 = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|x86.ActiveCfg = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Debug|x86.Build.0 = Debug|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|Any CPU.Build.0 = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|x64.ActiveCfg = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|x64.Build.0 = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|x86.ActiveCfg = Release|Any CPU + {FB134E1E-A785-4EC7-97E8-A4F260A633DC}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -765,6 +779,7 @@ Global {14586564-62CC-4117-AC1B-858ED53C2D6C} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} {E30CF10F-FABF-4917-8BEB-CB81E4CE2C92} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} {4473538D-2BFA-4C53-B642-0D0DC4F16863} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} + {FB134E1E-A785-4EC7-97E8-A4F260A633DC} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10} diff --git a/global.json b/global.json index 77a73a039e..27fbe488b0 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.101", + "version": "8.0.100", "rollForward": "latestMinor", "allowPrerelease": false } diff --git a/src/Modules/SimplCommerce.Module.Cms/Areas/Cms/Controllers/CarouselWidgetApiController.cs b/src/Modules/SimplCommerce.Module.Cms/Areas/Cms/Controllers/CarouselWidgetApiController.cs index 57fdc645be..6a4a778504 100644 --- a/src/Modules/SimplCommerce.Module.Cms/Areas/Cms/Controllers/CarouselWidgetApiController.cs +++ b/src/Modules/SimplCommerce.Module.Cms/Areas/Cms/Controllers/CarouselWidgetApiController.cs @@ -33,7 +33,7 @@ public CarouselWidgetApiController(IRepository widgetInstanceRep [HttpGet("{id}")] public async Task> Get(long id) - { + { var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id); var model = new CarouselWidgetForm { @@ -55,11 +55,11 @@ public async Task> Get(long id) } [HttpPost] - public async Task Post([FromForm]CarouselWidgetForm model) + public async Task Post([FromForm] CarouselWidgetForm model) { ModelBindUploadFiles(model); - if(model.Items.Any(x => x.UploadImage == null)) + if (model.Items.Any(x => x.UploadImage == null)) { ModelState.AddModelError("Images", "Images is required"); return BadRequest(ModelState); @@ -87,7 +87,7 @@ public async Task Post([FromForm]CarouselWidgetForm model) } [HttpPut("{id}")] - public async Task Put(long id, [FromForm]CarouselWidgetForm model) + public async Task Put(long id, [FromForm] CarouselWidgetForm model) { ModelBindUploadFiles(model); @@ -104,7 +104,7 @@ public async Task Put(long id, [FromForm]CarouselWidgetForm model } var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id); - if(widgetInstance == null) + if (widgetInstance == null) { return NotFound(); } diff --git a/src/SimplCommerce.WebHost/appsettings.json b/src/SimplCommerce.WebHost/appsettings.json index 150c72a1de..1b03a0e1eb 100644 --- a/src/SimplCommerce.WebHost/appsettings.json +++ b/src/SimplCommerce.WebHost/appsettings.json @@ -1,6 +1,7 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=.;Database=SimplCommerce;Trusted_Connection=True;TrustServerCertificate=true;MultipleActiveResultSets=true" + "DefaultConnection": "server=DotNetFSD\\SQLEXPRESS; database=SimplCommerce; user id=sa; password=pass@123;trustservercertificate=true", + }, "Authentication" : { "Facebook" : { diff --git a/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs new file mode 100644 index 0000000000..ee24bb23a8 --- /dev/null +++ b/test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs @@ -0,0 +1,58 @@ +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 +{ + public class CurrencyHelperTests + { + [Fact] + public void IsZeroDecimalCurrencies_WithZeroDecimalCurrency_ReturnsTrue() + { + var cultureInfo = new CultureInfo("ja-JP"); + + var result = CurrencyHelper.IsZeroDecimalCurrencies(cultureInfo); + + Assert.True(result); + } + + [Fact] + public void IsZeroDecimalCurrencies_WithNonZeroDecimalCurrency_ReturnsFalse() + { + var cultureInfo = new CultureInfo("en-US"); + + var result = CurrencyHelper.IsZeroDecimalCurrencies(cultureInfo); + + Assert.False(result); + } + + [Fact] + public void IsZeroDecimalCurrencies_WithUnknownCurrency_ReturnsFalse() + { + var cultureInfo = new CultureInfo("fr-FR"); + + var result = CurrencyHelper.IsZeroDecimalCurrencies(cultureInfo); + + Assert.False(result); + } + [Fact] + public void IsZeroDecimalCurrencies_WithUnknown2Currency_ReturnsFalse() + { + var cultureInfo = new CultureInfo("de-DE_phoneb"); + + var result = CurrencyHelper.IsZeroDecimalCurrencies(cultureInfo); + + Assert.False(result); + + } + + + + } +} diff --git a/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs new file mode 100644 index 0000000000..c126c1755d --- /dev/null +++ b/test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs @@ -0,0 +1,76 @@ +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 +{ + 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..e0813b5d0b --- /dev/null +++ b/test/SimplCommerce.Module.Cms.Tests/Controllers/MenuApiControllerTests.cs @@ -0,0 +1,53 @@ +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 +{ + 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); + } + + + + + + + + + + } +} diff --git a/test/SimplCommerce.Module.Core.Tests/Components/DefaultShippingAddressViewComponentTests.cs b/test/SimplCommerce.Module.Core.Tests/Components/DefaultShippingAddressViewComponentTests.cs index cfd3380c67..8de4dae736 100644 --- a/test/SimplCommerce.Module.Core.Tests/Components/DefaultShippingAddressViewComponentTests.cs +++ b/test/SimplCommerce.Module.Core.Tests/Components/DefaultShippingAddressViewComponentTests.cs @@ -4,15 +4,16 @@ using Microsoft.AspNetCore.Mvc.ViewComponents; using Microsoft.EntityFrameworkCore; using Moq; +using SimplCommerce.Module.Core.Areas.Core.Components; +using SimplCommerce.Module.Core.Areas.Core.ViewModels.Manage; using SimplCommerce.Module.Core.Data; using SimplCommerce.Module.Core.Extensions; using SimplCommerce.Module.Core.Models; using Xunit; -namespace SimplCommerce.Module.Core.Tests.Components +/*namespace SimplCommerce.Module.Core.Tests.Components { - /* - Maybe we should not maintain these kind of unit test, it doesn't bring much value but waste of time + public class DefaultShippingAddressViewComponentTests { @@ -124,5 +125,5 @@ private UserAddress MakeShippingAddress() return userAddress; } } - */ -} + +}*/