Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unit test cases #1076

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions SimplCommerce.sln
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ 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.ProductComparison.Tests", "test\SimplCommerce.Module.ProductComparison.Tests\SimplCommerce.Module.ProductComparison.Tests.csproj", "{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.ShippingFree.Tests", "test\SimplCommerce.Module.ShippingFree.Tests\SimplCommerce.Module.ShippingFree.Tests.csproj", "{FE71DD92-4A2C-4143-B714-91CF5D95645F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -710,6 +714,30 @@ 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
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|x64.ActiveCfg = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|x64.Build.0 = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|x86.ActiveCfg = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Debug|x86.Build.0 = Debug|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|Any CPU.Build.0 = Release|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|x64.ActiveCfg = Release|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|x64.Build.0 = Release|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|x86.ActiveCfg = Release|Any CPU
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC}.Release|x86.Build.0 = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|x64.ActiveCfg = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|x64.Build.0 = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|x86.ActiveCfg = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Debug|x86.Build.0 = Debug|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|Any CPU.Build.0 = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|x64.ActiveCfg = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|x64.Build.0 = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|x86.ActiveCfg = Release|Any CPU
{FE71DD92-4A2C-4143-B714-91CF5D95645F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -765,6 +793,8 @@ 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}
{7C8B8D9F-4101-49B8-A633-C71FBEDCD3BC} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
{FE71DD92-4A2C-4143-B714-91CF5D95645F} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Linq;
using Moq;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Core.Models;
using SimplCommerce.Module.Core.Services;
using Xunit;

namespace SimplCommerce.Module.Core.Tests.Services
{
public class WidgetInstanceServiceTests
{
#region PositiveTestCases
[Fact]
public void GetPublished_ShouldReturnPublishedWidgetInstances()
{
// Arrange
var widgetInstances = new[]
{
new WidgetInstance { PublishStart = DateTimeOffset.Now.AddDays(-1), PublishEnd = DateTimeOffset.Now.AddDays(1) },
new WidgetInstance { PublishStart = DateTimeOffset.Now.AddDays(-2), PublishEnd = DateTimeOffset.Now.AddDays(2) }
};

var mockRepository = new Mock<IRepository<WidgetInstance>>();
mockRepository.Setup(x => x.Query()).Returns(widgetInstances.AsQueryable());

var widgetInstanceService = new WidgetInstanceService(mockRepository.Object);

// Act
var result = widgetInstanceService.GetPublished();

// Assert
Assert.Equal(widgetInstances.Length, result.Count());
}
#endregion

#region NegativeTestCases
[Fact]
public void GetPublished_ShouldNotReturnUnpublishedWidgetInstances()
{
// Arrange
var widgetInstances = new[]
{
new WidgetInstance { PublishStart = DateTimeOffset.Now.AddDays(1), PublishEnd = DateTimeOffset.Now.AddDays(2) },
new WidgetInstance { PublishStart = null, PublishEnd = DateTimeOffset.Now.AddDays(-1) },
};

var mockRepository = new Mock<IRepository<WidgetInstance>>();
mockRepository.Setup(x => x.Query()).Returns(widgetInstances.AsQueryable());

var widgetInstanceService = new WidgetInstanceService(mockRepository.Object);

// Act
var result = widgetInstanceService.GetPublished();

// Assert
Assert.Empty(result);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Moq;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Infrastructure.Web.SmartTable;
using SimplCommerce.Module.Catalog.Models;
using SimplCommerce.Module.Pricing.Areas.Pricing.Controllers;
using SimplCommerce.Module.Pricing.Areas.Pricing.ViewModels;
using SimplCommerce.Module.Pricing.Models;
using Xunit;
namespace SimplCommerce.Module.Pricing.Tests.Controllers
{
public class CartRuleApiControllerTests
{
[Fact]
public void List_ReturnsJsonResult()
{
// Arrange
var cartRuleRepositoryMock = new Mock<IRepository<CartRule>>();
var controller = new CartRuleApiController(cartRuleRepositoryMock.Object);
var smartTableParam = new SmartTableParam()
{
Pagination = new Pagination() { Start = 1, TotalItemCount = 1, Number = 1, NumberOfPages = 1 },
Search = new Search() { },
Sort = new Sort() { }
};
// Act
var result = controller.List(smartTableParam);
// Assert
Assert.IsType<JsonResult>(result);
}


[Fact]
public async Task Post_CreatesNewCartRule_WhenModelStateIsValid()
{
// Arrange
var cartRuleRepositoryMock = new Mock<IRepository<CartRule>>();
var controller = new CartRuleApiController(cartRuleRepositoryMock.Object);
var model = new CartRuleForm
{
Id = 1,
Name = "Test Cart Rule",
IsActive = true,
StartOn = DateTime.Now,
EndOn = DateTime.Now.AddDays(7),
IsCouponRequired = true,
RuleToApply = "Coupon",
DiscountAmount = 10,
DiscountStep = 2,
MaxDiscountAmount = 50,
UsageLimitPerCoupon = 100,
UsageLimitPerCustomer = 1
};
// Act
var result = await controller.Post(model);
// Assert
var createdAtActionResult = Assert.IsType<CreatedAtActionResult>(result);
Assert.Equal(nameof(CartRuleApiController.Get), createdAtActionResult.ActionName);
Assert.NotNull(createdAtActionResult.RouteValues["id"]);
cartRuleRepositoryMock.Verify(repo => repo.Add(It.IsAny<CartRule>()), Times.Once);
cartRuleRepositoryMock.Verify(repo => repo.SaveChangesAsync(), Times.Once);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Infrastructure.Web.SmartTable;
using SimplCommerce.Module.Core.Models;
using SimplCommerce.Module.Pricing.Areas.Pricing.Controllers;
using SimplCommerce.Module.Pricing.Models;
using Xunit;

namespace SimplCommerce.Module.Pricing.Tests.Controllers
{
public class CartRuleUsageApiControllerTests
{

[Fact]
public void List_ReturnsJsonResult()
{
// Arrange
var cartRuleUsageRepositoryMock = new Mock<IRepository<CartRuleUsage>>();
var controller = new CartRuleUsageApiController(cartRuleUsageRepositoryMock.Object);
var smartTableParam = new SmartTableParam() { Pagination = new Pagination() { }, Search = new Search() { }, Sort = new Sort() };

// Act
var result = controller.List(smartTableParam);

// Assert
Assert.IsType<JsonResult>(result);
}

[Fact]
public void List_ShouldReturnCorrectData()
{
// Arrange
var cartRuleUsageRepositoryMock = new Mock<IRepository<CartRuleUsage>>();
var controller = new CartRuleUsageApiController(cartRuleUsageRepositoryMock.Object);

var smartTableParam = new SmartTableParam()
{
Pagination = new Pagination() { },
Search = new Search
{

PredicateObject = new JObject()
},
Sort = new Sort() { }
};
smartTableParam.Search.PredicateObject.Add("RuleName", "TestRule");
smartTableParam.Search.PredicateObject.Add("CouponCode", "TestCoupon");
smartTableParam.Search.PredicateObject.Add("FullName", "TestUser");
smartTableParam.Search.PredicateObject.Add("CreatedOn", new JObject
{
{ "after" , DateTimeOffset.Now.AddDays(-7)},
{ "before" , DateTimeOffset.Now}
});

// Mocking the IQueryable<CartRuleUsage>
var cartRuleUsageData = new List<CartRuleUsage>
{
new CartRuleUsage
{
CartRuleId = 101,
CartRule = new CartRule { Name = "TestRule" },
UserId = 201,
User = new User { FullName = "TestUser" },
Coupon = new Coupon { Code = "TestCoupon" },
OrderId = 301,
CreatedOn = DateTimeOffset.Now.AddDays(-5)
},
// Add more data as needed for testing different scenarios
}.AsQueryable();

cartRuleUsageRepositoryMock.Setup(r => r.Query()).Returns(cartRuleUsageData);

// Act
var result = controller.List(smartTableParam);

// Assert
var jsonResult = Assert.IsType<JsonResult>(result);
//var cartRuleUsages = Assert.IsType<SmartTableResult<searchobject>>(jsonResult.Value);

// Add more assertions based on your expectations for the result
Assert.NotNull(jsonResult.Value);
var serializeobj = JsonConvert.SerializeObject(jsonResult.Value);
var deserializeobj = JsonConvert.DeserializeObject<searchObject>(serializeobj);
Assert.Equal("TestRule", deserializeobj.Items[0].CartRuleName);
Assert.Equal("TestUser", deserializeobj.Items[0].FullName);
Assert.Equal("TestCoupon", deserializeobj.Items[0].CouponCode);
Assert.Equal(101, deserializeobj.Items[0].CartRuleId);
Assert.Equal(201, deserializeobj.Items[0].UserId);
Assert.Equal(301, deserializeobj.Items[0].OrderId);

}
public class Itemsobject
{
public long Id { get; set; }
public long CartRuleId { get; set; }
public string CartRuleName { get; set; }
public long UserId { get; set; }
public string FullName { get; set; }
public string CouponCode { get; set; }
public long OrderId { get; set; }
public DateTimeOffset CreatedOn { get; set; }


}
public class searchObject
{
public List<Itemsobject> Items { get; set; }
public int NumberOfPages { get; set; }
public int TotalRecord { get; set; }

}


}
}

Loading
Loading