diff --git a/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleApiControllerTests.cs b/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleApiControllerTests.cs new file mode 100644 index 000000000..f888ea71b --- /dev/null +++ b/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleApiControllerTests.cs @@ -0,0 +1,78 @@ +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>(); + 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(result); + } + + [Fact] + public async Task Post_CreatesNewCartRule_WhenModelStateIsValid() + { + // Arrange + var cartRuleRepositoryMock = new Mock>(); + 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(result); + Assert.Equal(nameof(CartRuleApiController.Get), createdAtActionResult.ActionName); + Assert.NotNull(createdAtActionResult.RouteValues["id"]); + cartRuleRepositoryMock.Verify(repo => repo.Add(It.IsAny()), Times.Once); + cartRuleRepositoryMock.Verify(repo => repo.SaveChangesAsync(), Times.Once); + } + } +} diff --git a/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUsageApiControllerTests.cs b/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUsageApiControllerTests.cs new file mode 100644 index 000000000..f34fceb36 --- /dev/null +++ b/test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUsageApiControllerTests.cs @@ -0,0 +1,121 @@ +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>(); + 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(result); + } + + [Fact] + public void List_ShouldReturnCorrectData() + { + // Arrange + var cartRuleUsageRepositoryMock = new Mock>(); + 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 + var cartRuleUsageData = new List + { + 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(result); + //var cartRuleUsages = Assert.IsType>(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(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 Items { get; set; } + + public int NumberOfPages { get; set; } + + public int TotalRecord { get; set; } + } + } +} +