Skip to content

Commit

Permalink
infrastrucutre test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarth.Gupta committed Jan 9, 2024
1 parent a6bf271 commit ea956b9
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Intercom.Dotnet.Client" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Modules\SimplCommerce.Module.WishList\SimplCommerce.Module.WishList.csproj" />
<ProjectReference Include="..\src\SimplCommerce.Infrastructure\SimplCommerce.Infrastructure.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions SimplCommerce.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.101",
"version": "8.0.100",
"rollForward": "latestMinor",
"allowPrerelease": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CarouselWidgetApiController(IRepository<WidgetInstance> widgetInstanceRep

[HttpGet("{id}")]
public async Task<ActionResult<CarouselWidgetForm>> Get(long id)
{
{
var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
var model = new CarouselWidgetForm
{
Expand All @@ -55,11 +55,11 @@ public async Task<ActionResult<CarouselWidgetForm>> Get(long id)
}

[HttpPost]
public async Task<IActionResult> Post([FromForm]CarouselWidgetForm model)
public async Task<IActionResult> 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);
Expand Down Expand Up @@ -87,7 +87,7 @@ public async Task<IActionResult> Post([FromForm]CarouselWidgetForm model)
}

[HttpPut("{id}")]
public async Task<IActionResult> Put(long id, [FromForm]CarouselWidgetForm model)
public async Task<IActionResult> Put(long id, [FromForm] CarouselWidgetForm model)
{
ModelBindUploadFiles(model);

Expand All @@ -104,7 +104,7 @@ public async Task<IActionResult> Put(long id, [FromForm]CarouselWidgetForm model
}

var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
if(widgetInstance == null)
if (widgetInstance == null)
{
return NotFound();
}
Expand Down
3 changes: 2 additions & 1 deletion src/SimplCommerce.WebHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -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" : {
Expand Down
58 changes: 58 additions & 0 deletions test/SimplCommerce.Infrastructure.Tests/CurrencyHelperTests.cs
Original file line number Diff line number Diff line change
@@ -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);

}



}
}
76 changes: 76 additions & 0 deletions test/SimplCommerce.Infrastructure.Tests/ReflectionHelperTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>);
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<int> { }

public interface IGenericInterface<T> { }

public interface INonGenericInterface { }
}
}
Original file line number Diff line number Diff line change
@@ -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<IRepository<Menu>>();
var menuItemRepositoryMock = new Mock<IRepository<MenuItem>>();

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<JsonResult>(result);
var createdMenu = Assert.IsType<Menu>(okResult.Value);
Assert.Equal("NewMenu", createdMenu.Name);
Assert.True(createdMenu.IsPublished);
}









}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -124,5 +125,5 @@ private UserAddress MakeShippingAddress()
return userAddress;
}
}
*/
}
}*/

0 comments on commit ea956b9

Please sign in to comment.