-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for pricing module controllers (#1078)
Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
1 parent
dae5acd
commit 6816fa1
Showing
2 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleApiControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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); | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUsageApiControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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; } | ||
} | ||
} | ||
} | ||
|