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 tests for pricing module controllers #1078

Merged
merged 17 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
4008797
added tests for SimplCommerce.Module.Pricing - CartRuleApiController
Arunkumar0610 Jan 10, 2024
d2c758c
added tests for SimplCommerce.Module.Pricing - CartRuleUsageApiContro…
Arunkumar0610 Jan 10, 2024
3a65029
Update CartRuleApiControllerTests.cs
Arunkumar0610 Jan 11, 2024
e8d35b9
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleAp…
hishamco Jan 11, 2024
67b8a7d
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleAp…
hishamco Jan 11, 2024
1f7ddc5
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleAp…
hishamco Jan 11, 2024
7f3dede
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleAp…
hishamco Jan 11, 2024
9d0c52f
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
787516c
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
61bf2de
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
48af4a2
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
2f72390
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
9c14f9c
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
3226e00
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
66b3888
Update test/SimplCommerce.Module.Pricing.Tests/SimplCommerce.Module.P…
hishamco Jan 11, 2024
3a238f2
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleAp…
hishamco Jan 11, 2024
e8a8f44
Update test/SimplCommerce.Module.Pricing.Tests/Controllers/CartRuleUs…
hishamco Jan 11, 2024
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
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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = controller.List(smartTableParam);
// Assert
Arunkumar0610 marked this conversation as resolved.
Show resolved Hide resolved
Assert.IsType<JsonResult>(result);
}

Arunkumar0610 marked this conversation as resolved.
Show resolved Hide resolved

[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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.Post(model);
// Assert
Arunkumar0610 marked this conversation as resolved.
Show resolved Hide resolved
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);
}

Arunkumar0610 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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]
hishamco marked this conversation as resolved.
Show resolved Hide resolved
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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
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; }


}
hishamco marked this conversation as resolved.
Show resolved Hide resolved
public class searchObject
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
public List<Itemsobject> Items { get; set; }
hishamco marked this conversation as resolved.
Show resolved Hide resolved
public int NumberOfPages { get; set; }
hishamco marked this conversation as resolved.
Show resolved Hide resolved
public int TotalRecord { get; set; }

hishamco marked this conversation as resolved.
Show resolved Hide resolved
}


}
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
<ProjectReference Include="..\..\src\SimplCommerce.Infrastructure\SimplCommerce.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

hishamco marked this conversation as resolved.
Show resolved Hide resolved
</Project>
Loading