|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.Json.Nodes; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Moq; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using SimplCommerce.Infrastructure.Data; |
| 10 | +using SimplCommerce.Infrastructure.Web.SmartTable; |
| 11 | +using SimplCommerce.Module.Core.Models; |
| 12 | +using SimplCommerce.Module.Pricing.Areas.Pricing.Controllers; |
| 13 | +using SimplCommerce.Module.Pricing.Models; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace SimplCommerce.Module.Pricing.Tests.Controllers |
| 17 | +{ |
| 18 | + public class CartRuleUsageApiControllerTests |
| 19 | + { |
| 20 | + [Fact] |
| 21 | + public void List_ReturnsJsonResult() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + var cartRuleUsageRepositoryMock = new Mock<IRepository<CartRuleUsage>>(); |
| 25 | + var controller = new CartRuleUsageApiController(cartRuleUsageRepositoryMock.Object); |
| 26 | + var smartTableParam = new SmartTableParam() { Pagination = new Pagination() { }, Search = new Search() { }, Sort = new Sort() }; |
| 27 | + |
| 28 | + // Act |
| 29 | + var result = controller.List(smartTableParam); |
| 30 | + |
| 31 | + // Assert |
| 32 | + Assert.IsType<JsonResult>(result); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void List_ShouldReturnCorrectData() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var cartRuleUsageRepositoryMock = new Mock<IRepository<CartRuleUsage>>(); |
| 40 | + var controller = new CartRuleUsageApiController(cartRuleUsageRepositoryMock.Object); |
| 41 | + |
| 42 | + var smartTableParam = new SmartTableParam() |
| 43 | + { |
| 44 | + Pagination = new Pagination() { }, |
| 45 | + Search = new Search |
| 46 | + { |
| 47 | + |
| 48 | + PredicateObject = new JObject() |
| 49 | + }, |
| 50 | + Sort = new Sort() { } |
| 51 | + }; |
| 52 | + smartTableParam.Search.PredicateObject.Add("RuleName", "TestRule"); |
| 53 | + smartTableParam.Search.PredicateObject.Add("CouponCode", "TestCoupon"); |
| 54 | + smartTableParam.Search.PredicateObject.Add("FullName", "TestUser"); |
| 55 | + smartTableParam.Search.PredicateObject.Add("CreatedOn", new JObject |
| 56 | + { |
| 57 | + { "after" , DateTimeOffset.Now.AddDays(-7)}, |
| 58 | + { "before" , DateTimeOffset.Now} |
| 59 | + }); |
| 60 | + |
| 61 | + // Mocking the IQueryable<CartRuleUsage> |
| 62 | + var cartRuleUsageData = new List<CartRuleUsage> |
| 63 | + { |
| 64 | + new CartRuleUsage |
| 65 | + { |
| 66 | + CartRuleId = 101, |
| 67 | + CartRule = new CartRule { Name = "TestRule" }, |
| 68 | + UserId = 201, |
| 69 | + User = new User { FullName = "TestUser" }, |
| 70 | + Coupon = new Coupon { Code = "TestCoupon" }, |
| 71 | + OrderId = 301, |
| 72 | + CreatedOn = DateTimeOffset.Now.AddDays(-5) |
| 73 | + }, |
| 74 | + // Add more data as needed for testing different scenarios |
| 75 | + }.AsQueryable(); |
| 76 | + |
| 77 | + cartRuleUsageRepositoryMock.Setup(r => r.Query()).Returns(cartRuleUsageData); |
| 78 | + |
| 79 | + // Act |
| 80 | + var result = controller.List(smartTableParam); |
| 81 | + |
| 82 | + // Assert |
| 83 | + var jsonResult = Assert.IsType<JsonResult>(result); |
| 84 | + //var cartRuleUsages = Assert.IsType<SmartTableResult<searchobject>>(jsonResult.Value); |
| 85 | + |
| 86 | + // Add more assertions based on your expectations for the result |
| 87 | + Assert.NotNull(jsonResult.Value); |
| 88 | + var serializeobj = JsonConvert.SerializeObject(jsonResult.Value); |
| 89 | + var deserializeobj = JsonConvert.DeserializeObject<searchObject>(serializeobj); |
| 90 | + Assert.Equal("TestRule", deserializeobj.Items[0].CartRuleName); |
| 91 | + Assert.Equal("TestUser", deserializeobj.Items[0].FullName); |
| 92 | + Assert.Equal("TestCoupon", deserializeobj.Items[0].CouponCode); |
| 93 | + Assert.Equal(101, deserializeobj.Items[0].CartRuleId); |
| 94 | + Assert.Equal(201, deserializeobj.Items[0].UserId); |
| 95 | + Assert.Equal(301, deserializeobj.Items[0].OrderId); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public class Itemsobject |
| 100 | + { |
| 101 | + public long Id { get; set; } |
| 102 | + public long CartRuleId { get; set; } |
| 103 | + public string CartRuleName { get; set; } |
| 104 | + public long UserId { get; set; } |
| 105 | + public string FullName { get; set; } |
| 106 | + public string CouponCode { get; set; } |
| 107 | + public long OrderId { get; set; } |
| 108 | + public DateTimeOffset CreatedOn { get; set; } |
| 109 | + } |
| 110 | + |
| 111 | + public class searchObject |
| 112 | + { |
| 113 | + public List<Itemsobject> Items { get; set; } |
| 114 | + |
| 115 | + public int NumberOfPages { get; set; } |
| 116 | + |
| 117 | + public int TotalRecord { get; set; } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments