Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add tests
support48188c3d4119
diff --git a/src/Tests/Grand.Business.Checkout.Tests/Grand.Business.Checkout.Tests.csproj b/src/Tests/Grand.Business.Checkout.Tests/Grand.Business.Checkout.Tests.csprojindex be4f8c6cb..0d511981c 100644--- a/src/Tests/Grand.Business.Checkout.Tests/Grand.Business.Checkout.Tests.csproj+++ b/src/Tests/Grand.Business.Checkout.Tests/Grand.Business.Checkout.Tests.csproj@@ -18,6 +18,8 @@<ItemGroup><ProjectReference Include="..\..\Business\Grand.Business.Checkout\Grand.Business.Checkout.csproj" /><ProjectReference Include="..\..\Business\Grand.Business.Core\Grand.Business.Core.csproj" />+ <ProjectReference Include="..\Grand.Data.Tests\Grand.Data.Tests.csproj" />+ <ProjectReference Include="..\Grand.Infrastructure.Tests\Grand.Infrastructure.Tests.csproj" /></ItemGroup></Project>diff --git a/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/LoyaltyPointsServiceTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/LoyaltyPointsServiceTests.csnew file mode 100644index 000000000..1db415dd0--- /dev/null+++ b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/LoyaltyPointsServiceTests.cs@@ -0,0 +1,65 @@+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Data;+using Grand.Domain.Orders;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;+++namespace Grand.Business.Checkout.Services.Orders.Tests+{+ [TestClass()]+ public class LoyaltyPointsServiceTests+ {+ private LoyaltyPointsService _loyaltyPointsService;+ private IRepository<LoyaltyPointsHistory> _repository;+ private Mock<IMediator> _mediatorMock;++ [TestInitialize()]+ public void Init()+ {+ _repository = new MongoDBRepositoryTest<LoyaltyPointsHistory>();+ _mediatorMock = new Mock<IMediator>();++ _loyaltyPointsService = new LoyaltyPointsService(_repository, new LoyaltyPointsSettings() { }, _mediatorMock.Object);+ }++ [TestMethod()]+ public async Task GetLoyaltyPointsBalanceTest()+ {+ //Assert+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 10, CreatedOnUtc = DateTime.UtcNow.AddDays(-3) });+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 20, CreatedOnUtc = DateTime.UtcNow.AddDays(-2) });+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 30, CreatedOnUtc = DateTime.UtcNow.AddDays(-1) });+ //Act+ var result = await _loyaltyPointsService.GetLoyaltyPointsBalance("1", "");+ //Assert+ Assert.AreEqual(30, result);++ }++ [TestMethod()]+ public async Task AddLoyaltyPointsHistoryTest()+ {+ //Act+ var result = await _loyaltyPointsService.AddLoyaltyPointsHistory("1", 10, "");+ //Assert+ Assert.IsNotNull(result);+ Assert.AreEqual(10, result.Points);+ Assert.IsTrue(_repository.Table.Any());+ }++ [TestMethod()]+ public async Task GetLoyaltyPointsHistoryTest()+ {+ //Assert+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 10, CreatedOnUtc = DateTime.UtcNow.AddDays(-3) });+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 20, CreatedOnUtc = DateTime.UtcNow.AddDays(-2) });+ await _repository.InsertAsync(new LoyaltyPointsHistory() { CustomerId = "1", StoreId = "", Points = 10, PointsBalance = 30, CreatedOnUtc = DateTime.UtcNow.AddDays(-1) });+ //Act+ var result = await _loyaltyPointsService.GetLoyaltyPointsHistory("1");+ //Assert+ Assert.AreEqual(3, result.Count);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/MerchandiseReturnServiceTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/MerchandiseReturnServiceTests.csnew file mode 100644index 000000000..7b43e1c6c--- /dev/null+++ b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/MerchandiseReturnServiceTests.cs@@ -0,0 +1,319 @@+using Grand.Business.Core.Queries.Checkout.Orders;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Data;+using Grand.Domain.Orders;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Tests.Caching;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Checkout.Services.Orders.Tests+{+ [TestClass()]+ public class MerchandiseReturnServiceTests+ {+ private MerchandiseReturnService _merchandiseReturnService;++ private IRepository<MerchandiseReturn> _repository;+ private IRepository<MerchandiseReturnAction> _merchandiseReturnActionRepository;+ private IRepository<MerchandiseReturnReason> _merchandiseReturnReasonRepository;+ private IRepository<MerchandiseReturnNote> _merchandiseReturnNoteRepository;++ private Mock<IMediator> _mediatorMock;+ private MemoryCacheBase _cacheBase;++ [TestInitialize()]+ public void Init()+ {+ _repository = new MongoDBRepositoryTest<MerchandiseReturn>();+ _merchandiseReturnActionRepository = new MongoDBRepositoryTest<MerchandiseReturnAction>();+ _merchandiseReturnReasonRepository = new MongoDBRepositoryTest<MerchandiseReturnReason>();+ _merchandiseReturnNoteRepository = new MongoDBRepositoryTest<MerchandiseReturnNote>();++ _mediatorMock = new Mock<IMediator>();++ var query = from p in _repository.Table+ select p;+ _mediatorMock.Setup(x => x.Send(It.IsAny<GetMerchandiseReturnQuery>(), default))+ .Returns(Task.FromResult(query));++ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);++ _merchandiseReturnService = new MerchandiseReturnService(_repository, _merchandiseReturnActionRepository, _merchandiseReturnReasonRepository, _merchandiseReturnNoteRepository, _cacheBase, _mediatorMock.Object);+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnByIdTest()+ {+ //Arange+ var merchandiseReturn = new MerchandiseReturn();+ await _repository.InsertAsync(merchandiseReturn);++ //Act+ var result = _merchandiseReturnService.GetMerchandiseReturnById(merchandiseReturn.Id);++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnByIdTest_ReturnNumber()+ {+ //Arange+ var merchandiseReturn = new MerchandiseReturn() { ReturnNumber = 1 };+ await _repository.InsertAsync(merchandiseReturn);++ //Act+ var result = _merchandiseReturnService.GetMerchandiseReturnById(merchandiseReturn.ReturnNumber);++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task SearchMerchandiseReturnsTest()+ {+ //Arange+ var merchandiseReturn = new MerchandiseReturn();+ await _repository.InsertAsync(merchandiseReturn);++ //Act+ var result = await _merchandiseReturnService.SearchMerchandiseReturns();++ //Assert+ Assert.IsTrue(result.Any());+ }++ [TestMethod()]+ public async Task GetAllMerchandiseReturnActionsTest()+ {+ //Arrange+ await _merchandiseReturnActionRepository.InsertAsync(new MerchandiseReturnAction());+ await _merchandiseReturnActionRepository.InsertAsync(new MerchandiseReturnAction());++ //Act+ var result = await _merchandiseReturnService.GetAllMerchandiseReturnActions();++ //Assert+ Assert.IsTrue(result.Any());+ Assert.AreEqual(2, result.Count());+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnActionByIdTest()+ {+ //Arrange+ await _merchandiseReturnActionRepository.InsertAsync(new MerchandiseReturnAction() { Id = "1" });+ await _merchandiseReturnActionRepository.InsertAsync(new MerchandiseReturnAction());++ //Act+ var result = await _merchandiseReturnService.GetMerchandiseReturnActionById("1");++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task InsertMerchandiseReturnTest()+ {+ //Act+ await _merchandiseReturnService.InsertMerchandiseReturn(new MerchandiseReturn());++ //Assert+ Assert.IsTrue(_repository.Table.Any());+ }++ [TestMethod()]+ public async Task UpdateMerchandiseReturnTest()+ {+ //Arange+ var merchandiseReturn = new MerchandiseReturn();+ await _repository.InsertAsync(merchandiseReturn);++ //Act+ merchandiseReturn.CustomerComments = "test";+ await _merchandiseReturnService.UpdateMerchandiseReturn(merchandiseReturn);++ //Assert+ Assert.IsTrue(_repository.Table.FirstOrDefault(x=>x.Id == merchandiseReturn.Id).CustomerComments == "test");+ }++ [TestMethod()]+ public async Task DeleteMerchandiseReturnTest()+ {+ //Arange+ var merchandiseReturn = new MerchandiseReturn();+ await _repository.InsertAsync(merchandiseReturn);++ //Act+ await _merchandiseReturnService.DeleteMerchandiseReturn(merchandiseReturn);++ //Assert+ Assert.IsFalse(_repository.Table.Any());+ }++ [TestMethod()]+ public async Task InsertMerchandiseReturnActionTest()+ {+ //Act+ await _merchandiseReturnService.InsertMerchandiseReturnAction(new MerchandiseReturnAction());++ //Assert+ Assert.IsTrue(_merchandiseReturnActionRepository.Table.Any());+ }++ [TestMethod()]+ public async Task UpdateMerchandiseReturnActionTest()+ {+ //Arange+ var merchandiseReturnAction = new MerchandiseReturnAction();+ await _merchandiseReturnActionRepository.InsertAsync(merchandiseReturnAction);++ //Act+ merchandiseReturnAction.Name = "test";+ await _merchandiseReturnService.UpdateMerchandiseReturnAction(merchandiseReturnAction);++ //Assert+ Assert.IsTrue(_merchandiseReturnActionRepository.Table.FirstOrDefault(x => x.Id == merchandiseReturnAction.Id).Name == "test");+ }++ [TestMethod()]+ public async Task DeleteMerchandiseReturnActionTest()+ {+ //Arange+ var merchandiseReturnAction = new MerchandiseReturnAction();+ await _merchandiseReturnActionRepository.InsertAsync(merchandiseReturnAction);++ //Act+ await _merchandiseReturnService.DeleteMerchandiseReturnAction(merchandiseReturnAction);++ //Assert+ Assert.IsFalse(_merchandiseReturnActionRepository.Table.Any());+ }++ [TestMethod()]+ public async Task DeleteMerchandiseReturnReasonTest()+ {+ //Arange+ var merchandiseReturnReason = new MerchandiseReturnReason();+ await _merchandiseReturnReasonRepository.InsertAsync(merchandiseReturnReason);++ //Act+ await _merchandiseReturnService.DeleteMerchandiseReturnReason(merchandiseReturnReason);++ //Assert+ Assert.IsFalse(_merchandiseReturnReasonRepository.Table.Any());+ }++ [TestMethod()]+ public async Task GetAllMerchandiseReturnReasonsTest()+ {+ //Arrange+ await _merchandiseReturnReasonRepository.InsertAsync(new MerchandiseReturnReason() { Id = "1" });+ await _merchandiseReturnReasonRepository.InsertAsync(new MerchandiseReturnReason());++ //Act+ var result = await _merchandiseReturnService.GetAllMerchandiseReturnReasons();++ //Assert+ Assert.AreEqual(2, result.Count);+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnReasonByIdTest()+ {+ //Arrange+ await _merchandiseReturnReasonRepository.InsertAsync(new MerchandiseReturnReason() { Id = "1" });+ await _merchandiseReturnReasonRepository.InsertAsync(new MerchandiseReturnReason());++ //Act+ var result = await _merchandiseReturnService.GetMerchandiseReturnReasonById("1");++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task InsertMerchandiseReturnReasonTest()+ {+ //Arange+ var merchandiseReturnReason = new MerchandiseReturnReason();++ //Act+ await _merchandiseReturnService.InsertMerchandiseReturnReason(merchandiseReturnReason);++ //Assert+ Assert.IsTrue(_merchandiseReturnReasonRepository.Table.Any());+ }++ [TestMethod()]+ public async Task UpdateMerchandiseReturnReasonTest()+ {+ //Arange+ var merchandiseReturnReason = new MerchandiseReturnReason();+ await _merchandiseReturnReasonRepository.InsertAsync(merchandiseReturnReason);+ //Act+ merchandiseReturnReason.Name = "test";+ await _merchandiseReturnService.UpdateMerchandiseReturnReason(merchandiseReturnReason);++ //Assert+ Assert.IsTrue(_merchandiseReturnReasonRepository.Table.FirstOrDefault(x => x.Id == merchandiseReturnReason.Id).Name == "test");+ }++ [TestMethod()]+ public async Task DeleteMerchandiseReturnNoteTest()+ {+ //Arange+ var merchandiseReturnNote = new MerchandiseReturnNote();+ await _merchandiseReturnNoteRepository.InsertAsync(merchandiseReturnNote);++ //Act+ await _merchandiseReturnService.DeleteMerchandiseReturnNote(merchandiseReturnNote);++ //Assert+ Assert.IsFalse(_merchandiseReturnNoteRepository.Table.Any());+ }++ [TestMethod()]+ public async Task InsertMerchandiseReturnNoteTest()+ {+ //Arange+ var merchandiseReturnNote = new MerchandiseReturnNote();++ //Act+ await _merchandiseReturnService.InsertMerchandiseReturnNote(merchandiseReturnNote);++ //Assert+ Assert.IsTrue(_merchandiseReturnNoteRepository.Table.Any());+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnNotesTest()+ {+ //Arange+ var merchandiseReturnNote = new MerchandiseReturnNote() { MerchandiseReturnId = "1" };+ await _merchandiseReturnNoteRepository.InsertAsync(merchandiseReturnNote);++ //Act+ var result = await _merchandiseReturnService.GetMerchandiseReturnNotes("1");++ //Assert+ Assert.AreEqual(1, result.Count);+ }++ [TestMethod()]+ public async Task GetMerchandiseReturnNoteTest()+ {+ //Arange+ var merchandiseReturnNote = new MerchandiseReturnNote() { Id = "1" };+ await _merchandiseReturnNoteRepository.InsertAsync(merchandiseReturnNote);++ //Act+ var result = await _merchandiseReturnService.GetMerchandiseReturnNote("1");++ //Assert+ Assert.IsNotNull(result);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderCalculationServiceTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderCalculationServiceTests.csnew file mode 100644index 000000000..5efb82de8--- /dev/null+++ b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderCalculationServiceTests.cs@@ -0,0 +1,237 @@+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Grand.Business.Core.Extensions;+using Grand.Business.Core.Interfaces.Catalog.Discounts;+using Grand.Business.Core.Interfaces.Catalog.Prices;+using Grand.Business.Core.Interfaces.Catalog.Products;+using Grand.Business.Core.Interfaces.Catalog.Tax;+using Grand.Business.Core.Utilities.Catalog;+using Grand.Business.Core.Interfaces.Checkout.CheckoutAttributes;+using Grand.Business.Core.Interfaces.Checkout.GiftVouchers;+using Grand.Business.Core.Interfaces.Checkout.Orders;+using Grand.Business.Core.Interfaces.Checkout.Payments;+using Grand.Business.Core.Interfaces.Checkout.Shipping;+using Grand.Business.Core.Utilities.Checkout;+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Domain.Catalog;+using Grand.Domain.Common;+using Grand.Domain.Customers;+using Grand.Domain.Directory;+using Grand.Domain.Discounts;+using Grand.Domain.Orders;+using Grand.Domain.Shipping;+using Grand.Domain.Stores;+using Grand.Domain.Tax;+using Grand.Infrastructure;+using Grand.SharedKernel;+using Moq;++namespace Grand.Business.Checkout.Services.Orders.Tests+{+ [TestClass()]+ public class OrderCalculationServiceTests+ {+ private OrderCalculationService _service;++ private Mock<IWorkContext> _workContextMock;+ private Mock<IPricingService> _pricingServiceMock;+ private Mock<ITaxService> _taxServiceMock;+ private Mock<IShippingService> _shippingServiceMock;+ private Mock<IPaymentService> _paymentServiceMock;+ private Mock<ICheckoutAttributeParser> _checkoutAttributeParserMock;+ private Mock<IDiscountService> _discountServiceMock;+ private Mock<IGiftVoucherService> _giftVoucherServiceMock;+ private Mock<ILoyaltyPointsService> _loyaltyPointsServiceMock;+ private Mock<IProductService> _productServiceMock;+ private Mock<ICurrencyService> _currencyServiceMock;+ private Mock<IGroupService> _groupServiceMock;+ private TaxSettings _taxSettings;+ private LoyaltyPointsSettings _loyaltyPointsSettings;+ private ShippingSettings _shippingSettings;+ private ShoppingCartSettings _shoppingCartSettings;+ private CatalogSettings _catalogSettings;++ [TestInitialize()]+ public void Init()+ {+ _workContextMock = new Mock<IWorkContext>();+ _pricingServiceMock = new Mock<IPricingService>();+ _taxServiceMock = new Mock<ITaxService>();+ _shippingServiceMock = new Mock<IShippingService>();+ _paymentServiceMock = new Mock<IPaymentService>();+ _checkoutAttributeParserMock = new Mock<ICheckoutAttributeParser>();+ _discountServiceMock = new Mock<IDiscountService>();+ _giftVoucherServiceMock = new Mock<IGiftVoucherService>();+ _loyaltyPointsServiceMock = new Mock<ILoyaltyPointsService>();+ _productServiceMock = new Mock<IProductService>();+ _currencyServiceMock = new Mock<ICurrencyService>();+ _groupServiceMock = new Mock<IGroupService>();+ _taxSettings = new TaxSettings();+ _loyaltyPointsSettings = new LoyaltyPointsSettings();+ _shippingSettings = new ShippingSettings();+ _shoppingCartSettings = new ShoppingCartSettings();+ _catalogSettings = new CatalogSettings();++ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "", Name = "test store" });+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);+ _workContextMock.Setup(c => c.WorkingCurrency).Returns(() => new Currency());+ _workContextMock.Setup(c => c.TaxDisplayType).Returns(() => TaxDisplayType.ExcludingTax);++ _service = new OrderCalculationService(_workContextMock.Object, _pricingServiceMock.Object, _taxServiceMock.Object, _shippingServiceMock.Object,+ _paymentServiceMock.Object, _checkoutAttributeParserMock.Object, _discountServiceMock.Object, _giftVoucherServiceMock.Object,+ _loyaltyPointsServiceMock.Object, _productServiceMock.Object, _currencyServiceMock.Object, _groupServiceMock.Object,+ _taxSettings, _loyaltyPointsSettings, _shippingSettings, _shoppingCartSettings, _catalogSettings);+ }++ [TestMethod()]+ public async Task GetShoppingCartSubTotalTest()+ {+ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _checkoutAttributeParserMock.Setup(a => a.ParseCheckoutAttributeValue(It.IsAny<List<CustomAttribute>>())).Returns(() => Task.FromResult((IList<(CheckoutAttribute ca, CheckoutAttributeValue cav)>)new List<(CheckoutAttribute ca, CheckoutAttributeValue cav)>()));+ _discountServiceMock.Setup(a => a.GetAllDiscounts(DiscountType.AssignedToOrderSubTotal, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())).Returns(() => Task.FromResult((IList<Discount>)new List<Discount>() { }));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1 } };+ var result = await _service.GetShoppingCartSubTotal(shoppingCartItem, false);++ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task GetShoppingCartAdditionalShippingChargeTest()+ {+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10 } };+ //Act+ var result = await _service.GetShoppingCartAdditionalShippingCharge(shoppingCartItem);+ //Assert+ Assert.AreEqual(10, result);+ }++ [TestMethod()]+ public async Task IsFreeShippingTest()+ {+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10, IsFreeShipping = true } };+ //Act+ var result = await _service.GetShoppingCartAdditionalShippingCharge(shoppingCartItem);+ //Assert+ Assert.AreEqual(0, result);+ }++ [TestMethod()]+ public async Task AdjustShippingRateTest()+ {+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10 } };+ //Act+ var result = await _service.AdjustShippingRate(10, shoppingCartItem);++ //Assert+ Assert.AreEqual(20, result.shippingRate);+ }++ [TestMethod()]+ public async Task GetShoppingCartShippingTotalTest()+ {+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));+ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _checkoutAttributeParserMock.Setup(a => a.ParseCheckoutAttributeValue(It.IsAny<List<CustomAttribute>>())).Returns(() => Task.FromResult((IList<(CheckoutAttribute ca, CheckoutAttributeValue cav)>)new List<(CheckoutAttribute ca, CheckoutAttributeValue cav)>()));+ _discountServiceMock.Setup(a => a.GetAllDiscounts(DiscountType.AssignedToOrderSubTotal, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())).Returns(() => Task.FromResult((IList<Discount>)new List<Discount>() { }));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10, IsFreeShipping = true } };+ //Act+ var result = await _service.GetShoppingCartShippingTotal(shoppingCartItem);++ //Assert+ Assert.AreEqual(0, result.shoppingCartShippingTotal);+ }++ [TestMethod()]+ public async Task GetTaxTotalTest()+ {+ //TODO+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));+ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _checkoutAttributeParserMock.Setup(a => a.ParseCheckoutAttributeValue(It.IsAny<List<CustomAttribute>>())).Returns(() => Task.FromResult((IList<(CheckoutAttribute ca, CheckoutAttributeValue cav)>)new List<(CheckoutAttribute ca, CheckoutAttributeValue cav)>()));+ _discountServiceMock.Setup(a => a.GetAllDiscounts(DiscountType.AssignedToOrderSubTotal, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())).Returns(() => Task.FromResult((IList<Discount>)new List<Discount>() { }));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10, IsFreeShipping = true } };+ //Act+ var result = await _service.GetTaxTotal(shoppingCartItem, false);+ //Assert+ Assert.AreEqual(0, result.taxtotal);+ }++ [TestMethod()]+ public async Task GetShoppingCartTotalTest()+ {+ //TODO+ //Arrange+ var cgs = new List<CustomerGroup>() { new CustomerGroup() };+ _groupServiceMock.Setup(c => c.GetAllByIds(It.IsAny<string[]>()))+ .Returns(Task.FromResult<IList<CustomerGroup>>(cgs));+ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _checkoutAttributeParserMock.Setup(a => a.ParseCheckoutAttributeValue(It.IsAny<List<CustomAttribute>>())).Returns(() => Task.FromResult((IList<(CheckoutAttribute ca, CheckoutAttributeValue cav)>)new List<(CheckoutAttribute ca, CheckoutAttributeValue cav)>()));+ _discountServiceMock.Setup(a => a.GetAllDiscounts(DiscountType.AssignedToOrderSubTotal, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())).Returns(() => Task.FromResult((IList<Discount>)new List<Discount>() { }));++ var shoppingCartItem = new List<ShoppingCartItem>() { new ShoppingCartItem() { Quantity = 1, IsShipEnabled = true, AdditionalShippingChargeProduct = 10, IsFreeShipping = true } };+ //Act+ var result = await _service.GetShoppingCartTotal(shoppingCartItem, false, false);+ //Assert+ Assert.AreEqual(0, result.shoppingCartTotal);++ }++ [TestMethod()]+ public async Task ConvertLoyaltyPointsToAmountTest()+ {+ //Arrange+ _loyaltyPointsSettings.ExchangeRate = 1;+ //Act+ var result = await _service.ConvertLoyaltyPointsToAmount(10);+ //Assert+ Assert.AreEqual(10, result);+ }++ [TestMethod()]+ public void ConvertAmountToLoyaltyPointsTest()+ {+ //Arrange+ _loyaltyPointsSettings.ExchangeRate = 1;+ //Act+ var result = _service.ConvertAmountToLoyaltyPoints(10);+ //Assert+ Assert.AreEqual(10, result);+ }++ [TestMethod()]+ public void CheckMinimumLoyaltyPointsToUseRequirementTest()+ {+ //Assert+ _loyaltyPointsSettings.MinimumLoyaltyPointsToUse = 20;+ //Act+ var result = _service.CheckMinimumLoyaltyPointsToUseRequirement(10);+ //Assert+ Assert.IsFalse(result);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderTagServiceTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderTagServiceTests.csnew file mode 100644index 000000000..59a7ad599--- /dev/null+++ b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/OrderTagServiceTests.cs@@ -0,0 +1,173 @@+using Grand.Business.Core.Queries.Checkout.Orders;+using Grand.Data.Tests.MongoDb;+using Grand.Domain.Data;+using Grand.Domain.Orders;+using Grand.Infrastructure.Caching;+using Grand.Infrastructure.Tests.Caching;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Checkout.Services.Orders.Tests+{+ [TestClass()]+ public class OrderTagServiceTests+ {+ private OrderTagService _service;+ private IRepository<OrderTag> _orderTagRepository;+ private IRepository<Order> _orderRepository;+ private MemoryCacheBase _cacheBase;+ private Mock<IMediator> _mediatorMock;++ [TestInitialize()]+ public void Init()+ {+ _orderTagRepository = new MongoDBRepositoryTest<OrderTag>();+ _orderRepository = new MongoDBRepositoryTest<Order>();++ _mediatorMock = new Mock<IMediator>();++ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);++ _service = new OrderTagService(_orderTagRepository, _orderRepository, _cacheBase, _mediatorMock.Object);+ }++ [TestMethod()]+ public async Task GetAllOrderTagsTest()+ {+ //Arrange+ await _orderTagRepository.InsertAsync(new OrderTag());+ await _orderTagRepository.InsertAsync(new OrderTag());+ await _orderTagRepository.InsertAsync(new OrderTag());++ //Act+ var result = await _service.GetAllOrderTags();++ //Assert+ Assert.AreEqual(3, result.Count);+ }++ [TestMethod()]+ public async Task GetOrderTagByIdTest()+ {+ //Arrange+ await _orderTagRepository.InsertAsync(new OrderTag() { Id = "1"});+ await _orderTagRepository.InsertAsync(new OrderTag());+ await _orderTagRepository.InsertAsync(new OrderTag());++ //Act+ var result = await _service.GetOrderTagById("1");++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task GetOrderTagByNameTest()+ {+ //Arrange+ await _orderTagRepository.InsertAsync(new OrderTag() { Id = "1", Name = "test" });+ await _orderTagRepository.InsertAsync(new OrderTag());+ await _orderTagRepository.InsertAsync(new OrderTag());++ //Act+ var result = await _service.GetOrderTagByName("test");++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task InsertOrderTagTest()+ {+ //Act+ await _service.InsertOrderTag(new OrderTag());++ //Assert+ Assert.IsTrue(_orderTagRepository.Table.Any());+ }++ [TestMethod()]+ public async Task UpdateOrderTagTest()+ {+ //Arrange+ var orderTag = new OrderTag();+ await _orderTagRepository.InsertAsync(orderTag);++ //Act+ orderTag.Name = "test";+ await _service.UpdateOrderTag(orderTag);++ //Assert+ Assert.IsTrue(_orderTagRepository.Table.FirstOrDefault(x=>x.Id == orderTag.Id).Name == "test");+ }++ [TestMethod()]+ public async Task DeleteOrderTagTest()+ {+ //Arrange+ var orderTag = new OrderTag();+ await _orderTagRepository.InsertAsync(orderTag);++ //Act+ await _service.DeleteOrderTag(orderTag);++ //Assert+ Assert.IsFalse(_orderTagRepository.Table.FirstOrDefault(x => x.Id == orderTag.Id)?.Name == "test");+ }++ [TestMethod()]+ public async Task AttachOrderTagTest()+ {+ //Arrange+ var orderTag = new OrderTag();+ await _orderTagRepository.InsertAsync(orderTag);+ var order = new Order();+ await _orderRepository.InsertAsync(order);++ //Act+ await _service.AttachOrderTag(orderTag.Id, order.Id);++ //Assert+ Assert.IsTrue(_orderRepository.Table.FirstOrDefault(x => x.Id == order.Id).OrderTags.Any(z => z == orderTag.Id));+ Assert.IsTrue(_orderTagRepository.Table.FirstOrDefault(x => x.Id == orderTag.Id).Count == 1);+ }++ [TestMethod()]+ public async Task DetachOrderTagTest()+ {+ //Arrange+ var orderTag = new OrderTag();+ await _orderTagRepository.InsertAsync(orderTag);+ var order = new Order();+ await _orderRepository.InsertAsync(order);++ await _service.AttachOrderTag(orderTag.Id, order.Id);++ //Act+ await _service.DetachOrderTag(orderTag.Id, order.Id);++ //Assert+ Assert.IsFalse(_orderRepository.Table.FirstOrDefault(x => x.Id == order.Id).OrderTags.Any(z => z == orderTag.Id));+ Assert.IsTrue(_orderTagRepository.Table.FirstOrDefault(x => x.Id == orderTag.Id).Count == 0);+ }++ [TestMethod()]+ public async Task GetOrderCountTest()+ {+ //Arrange+ var orderTag = new OrderTag();+ await _orderTagRepository.InsertAsync(orderTag);+ var order = new Order();+ await _orderRepository.InsertAsync(order);++ await _service.AttachOrderTag(orderTag.Id, order.Id);++ //Act+ var result = await _service.GetOrderCount(orderTag.Id, string.Empty);++ //Assert+ Assert.AreEqual(1, result);+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/ShoppingCartServiceTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/ShoppingCartServiceTests.csnew file mode 100644index 000000000..7467047f9--- /dev/null+++ b/src/Tests/Grand.Business.Checkout.Tests/Services/Orders/ShoppingCartServiceTests.cs@@ -0,0 +1,194 @@+using Grand.Business.Core.Interfaces.Catalog.Products;+using Grand.Business.Core.Interfaces.Checkout.Orders;+using Grand.Business.Core.Interfaces.Common.Directory;+using Grand.Business.Core.Interfaces.Customers;+using Grand.Business.Core.Utilities.Checkout;+using Grand.Domain.Catalog;+using Grand.Domain.Common;+using Grand.Domain.Customers;+using Grand.Domain.Directory;+using Grand.Domain.Orders;+using Grand.Domain.Tax;+using Grand.Infrastructure;+using MediatR;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Business.Checkout.Services.Orders.Tests+{+ [TestClass()]+ public class ShoppingCartServiceTests+ {+ private ShoppingCartService _shoppingCartService;+ private Mock<IWorkContext> _workContextMock;+ private Mock<IProductService> _productServiceMock;+ private Mock<ICustomerService> _customerServiceMock;+ private Mock<IMediator> _mediatorMock;+ private Mock<IUserFieldService> _userFieldServiceMock;+ private Mock<IShoppingCartValidator> _shoppingCartValidatorMock;+ private ShoppingCartSettings _shoppingCartSettings;++ [TestInitialize()]+ public void Init()+ {+ _workContextMock = new Mock<IWorkContext>();+ _productServiceMock = new Mock<IProductService>();+ _shoppingCartSettings = new ShoppingCartSettings();+ _customerServiceMock = new Mock<ICustomerService>();+ _mediatorMock = new Mock<IMediator>();+ _userFieldServiceMock = new Mock<IUserFieldService>();+ _shoppingCartValidatorMock = new Mock<IShoppingCartValidator>();++ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "", Name = "test store" });+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);+ _workContextMock.Setup(c => c.WorkingCurrency).Returns(() => new Currency());+ _workContextMock.Setup(c => c.TaxDisplayType).Returns(() => TaxDisplayType.ExcludingTax);++ _shoppingCartService = new ShoppingCartService(_workContextMock.Object, _productServiceMock.Object,+ _customerServiceMock.Object, _mediatorMock.Object, _userFieldServiceMock.Object, _shoppingCartValidatorMock.Object, _shoppingCartSettings);+ }++ [TestMethod()]+ public async Task GetShoppingCartTest()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ customer.ShoppingCartItems.Add(new ShoppingCartItem() { ShoppingCartTypeId = ShoppingCartType.ShoppingCart, StoreId = "" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));++ //Act+ var result = await _shoppingCartService.GetShoppingCart();+ //Assert+ Assert.IsTrue(result.Any());+ }++ [TestMethod()]+ public async Task FindShoppingCartItemTest()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ customer.ShoppingCartItems.Add(new ShoppingCartItem() { ShoppingCartTypeId = ShoppingCartType.ShoppingCart, StoreId = "", ProductId = "1" });+ customer.ShoppingCartItems.Add(new ShoppingCartItem() { ShoppingCartTypeId = ShoppingCartType.ShoppingCart, StoreId = "", ProductId = "2" });+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));++ //Act+ var result = await _shoppingCartService.FindShoppingCartItem(customer.ShoppingCartItems.ToList(), ShoppingCartType.ShoppingCart, "1");++ //Assert+ Assert.IsNotNull(result);+ }++ [TestMethod()]+ public async Task AddToCartTest_Success()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _shoppingCartValidatorMock.Setup(x => x.CheckCommonWarnings(It.IsAny<Customer>(), It.IsAny<IList<ShoppingCartItem>>(), It.IsAny<Product>(), ShoppingCartType.ShoppingCart, null, null, It.IsAny<int>(), It.IsAny<string>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));+ _shoppingCartValidatorMock.Setup(x => x.GetShoppingCartItemWarnings(It.IsAny<Customer>(), It.IsAny<ShoppingCartItem>(), It.IsAny<Product>(), It.IsAny<ShoppingCartValidatorOptions>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));++ //Act+ var result = await _shoppingCartService.AddToCart(customer, "2", ShoppingCartType.ShoppingCart, "");++ //Assert+ Assert.IsNotNull(result.shoppingCartItem);+ }+ [TestMethod()]+ public async Task AddToCartTest_Warning()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _shoppingCartValidatorMock.Setup(x => x.CheckCommonWarnings(It.IsAny<Customer>(), It.IsAny<IList<ShoppingCartItem>>(), It.IsAny<Product>(), ShoppingCartType.ShoppingCart, null, null, It.IsAny<int>(), It.IsAny<string>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));+ _shoppingCartValidatorMock.Setup(x => x.GetShoppingCartItemWarnings(It.IsAny<Customer>(), It.IsAny<ShoppingCartItem>(), It.IsAny<Product>(), It.IsAny<ShoppingCartValidatorOptions>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { "Error" }));++ //Act+ var result = await _shoppingCartService.AddToCart(customer, "2", ShoppingCartType.ShoppingCart, "");++ //Assert+ Assert.AreEqual(1, result.warnings.Count);+ }+ [TestMethod()]+ public async Task UpdateShoppingCartItemTest()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _shoppingCartValidatorMock.Setup(x => x.CheckCommonWarnings(It.IsAny<Customer>(), It.IsAny<IList<ShoppingCartItem>>(), It.IsAny<Product>(), ShoppingCartType.ShoppingCart, null, null, It.IsAny<int>(), It.IsAny<string>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));+ _shoppingCartValidatorMock.Setup(x => x.GetShoppingCartItemWarnings(It.IsAny<Customer>(), It.IsAny<ShoppingCartItem>(), It.IsAny<Product>(), It.IsAny<ShoppingCartValidatorOptions>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));++ var item = await _shoppingCartService.AddToCart(customer, "2", ShoppingCartType.ShoppingCart, "");+ //Act+ var result = await _shoppingCartService.UpdateShoppingCartItem(customer, item.shoppingCartItem.Id, "", new List<CustomAttribute>(), null);+ //Assert+ _customerServiceMock.Verify(x => x.UpdateShoppingCartItem(It.IsAny<string>(), It.IsAny<ShoppingCartItem>()), Times.Once);+ }++ [TestMethod()]+ public async Task DeleteShoppingCartItemTest()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _shoppingCartValidatorMock.Setup(x => x.CheckCommonWarnings(It.IsAny<Customer>(), It.IsAny<IList<ShoppingCartItem>>(), It.IsAny<Product>(), ShoppingCartType.ShoppingCart, null, null, It.IsAny<int>(), It.IsAny<string>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));+ _shoppingCartValidatorMock.Setup(x => x.GetShoppingCartItemWarnings(It.IsAny<Customer>(), It.IsAny<ShoppingCartItem>(), It.IsAny<Product>(), It.IsAny<ShoppingCartValidatorOptions>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));++ var item = await _shoppingCartService.AddToCart(customer, "2", ShoppingCartType.ShoppingCart, "");+ //Act+ await _shoppingCartService.DeleteShoppingCartItem(customer, item.shoppingCartItem);+ //Assert+ _customerServiceMock.Verify(x => x.DeleteShoppingCartItem(It.IsAny<string>(), It.IsAny<ShoppingCartItem>()), Times.Once);+ }++ [TestMethod()]+ public async Task MigrateShoppingCartTest()+ {+ //Arrange+ var customer = new Customer();+ customer.Groups.Add("1");+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => customer);++ _productServiceMock.Setup(a => a.GetProductById(It.IsAny<string>(), false)).Returns(() => Task.FromResult(new Product() { Id = "1", Published = true, Price = 10 }));+ _shoppingCartValidatorMock.Setup(x => x.CheckCommonWarnings(It.IsAny<Customer>(), It.IsAny<IList<ShoppingCartItem>>(), It.IsAny<Product>(), ShoppingCartType.ShoppingCart, null, null, It.IsAny<int>(), It.IsAny<string>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));+ _shoppingCartValidatorMock.Setup(x => x.GetShoppingCartItemWarnings(It.IsAny<Customer>(), It.IsAny<ShoppingCartItem>(), It.IsAny<Product>(), It.IsAny<ShoppingCartValidatorOptions>()))+ .Returns(() => Task.FromResult((IList<string>)new List<string>() { }));++ var item = await _shoppingCartService.AddToCart(customer, "2", ShoppingCartType.ShoppingCart, "");+ var customer2 = new Customer();+ //Act+ await _shoppingCartService.MigrateShoppingCart(customer, customer2, false);+ //Assert+ Assert.IsTrue(customer2.ShoppingCartItems.Any());+ Assert.IsFalse(customer.ShoppingCartItems.Any());+ }+ }+}\ No newline at end of file