Skip to content

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.

Add tests

support282d81c8d224
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountProviderTest.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountProviderTest.cs
index 068fe2733..f6e6c14b7 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountProviderTest.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountProviderTest.cs
@@ -33,7 +33,7 @@ namespace Grand.Business.Catalog.Tests.Services.Discounts
public class DiscountRuleValidTest : IDiscountRule
{
- public string SystemName => throw new NotImplementedException();
+ public string SystemName => "RuleSystemName";
public string FriendlyName => throw new NotImplementedException();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
index c63a3d341..68e318347 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
@@ -147,23 +147,22 @@ namespace Grand.Business.Catalog.Services.Discounts.Tests
Assert.IsNull(_repository.Table.FirstOrDefault(x => x.Name == "test"));
}
- //TODO
- //[TestMethod()]
- //public void LoadDiscountProviderBySystemNameTest_NotNull()
- //{
- // //Act
- // var provider = _dicountService.LoadDiscountProviderBySystemName("SampleDiscountProvider");
- // //Assert
- // Assert.IsNotNull(provider);
- //}
- //[TestMethod()]
- //public void LoadDiscountProviderBySystemNameTest_Null()
- //{
- // //Act
- // var provider = _dicountService.LoadDiscountProviderBySystemName("SampleDiscountProvider2");
- // //Assert
- // Assert.IsNull(provider);
- //}
+ [TestMethod()]
+ public void LoadDiscountProviderBySystemNameTest_NotNull()
+ {
+ //Act
+ var provider = _dicountService.LoadDiscountProviderByRuleSystemName("RuleSystemName");
+ //Assert
+ Assert.IsNotNull(provider);
+ }
+ [TestMethod()]
+ public void LoadDiscountProviderBySystemNameTest_Null()
+ {
+ //Act
+ var provider = _dicountService.LoadDiscountProviderByRuleSystemName("RuleSystemName2");
+ //Assert
+ Assert.IsNull(provider);
+ }
[TestMethod()]
public void LoadAllDiscountProvidersTest()
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Prices/PriceServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Prices/PriceServiceTests.cs
index 8283de550..bf873638d 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Prices/PriceServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Prices/PriceServiceTests.cs
@@ -23,6 +23,7 @@ using MediatR;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Grand.Business.Common.Services.Directory;
+using Grand.Domain.Common;
namespace Grand.Business.Catalog.Tests.Service.Prices
{
@@ -305,5 +306,21 @@ namespace Grand.Business.Catalog.Tests.Service.Prices
var subtotal = (await _pricingService.GetSubTotal(shoppingCartItem, product001)).subTotal;
Assert.AreEqual(110.22, subtotal);
}
+ [TestMethod()]
+ public async Task Can_get_product_cost()
+ {
+ var product001 = new Product {
+ Id = "242422",
+ Name = "product name 01",
+ Price = 55.11,
+ EnteredPrice = false,
+ Published = true,
+ ProductCost = 20
+ };
+
+ var productCost = await _pricingService.GetProductCost(product001, new List<CustomAttribute>());
+
+ Assert.AreEqual(20, productCost);
+ }
}
}
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs
new file mode 100644
index 000000000..5e4d23b55
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs
@@ -0,0 +1,274 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Grand.Infrastructure;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using Grand.SharedKernel.Extensions;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Catalog.Services.Products.Tests
+{
+ [TestClass()]
+ public class AuctionServiceTests
+ {
+ private IRepository<Bid> _repository;
+ private IRepository<Product> _productrepository;
+ private Mock<IWorkContext> _workContextMock;
+ private Mock<IMediator> _mediatorMock;
+ private AuctionService _auctionService;
+ private MemoryCacheBase _cacheBase;
+
+ [TestInitialize()]
+ public void InitializeTests()
+ {
+ CommonPath.BaseDirectory = "";
+
+ _repository = new MongoDBRepositoryTest<Bid>();
+ _productrepository = new MongoDBRepositoryTest<Product>();
+ _workContextMock = new Mock<IWorkContext>();
+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });
+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _auctionService = new AuctionService(_repository, _productrepository, _cacheBase, _mediatorMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task GetBidTest()
+ {
+ //Arrange
+ var bid = new Bid() {
+ Amount = 10
+ };
+ await _auctionService.InsertBid(bid);
+
+ //Act
+ var result = await _auctionService.GetBid(bid.Id);
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual(10, result.Amount);
+ }
+
+ [TestMethod()]
+ public async Task GetLatestBidTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ var bid2 = new Bid() {
+ Amount = 12,
+ ProductId = "1",
+ Date = DateTime.UtcNow
+ };
+ await _auctionService.InsertBid(bid2);
+
+ //Act
+ var result = await _auctionService.GetLatestBid("1");
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual(12, result.Amount);
+ }
+
+ [TestMethod()]
+ public async Task GetBidsByProductIdTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ var bid2 = new Bid() {
+ Amount = 12,
+ ProductId = "1",
+ Date = DateTime.UtcNow
+ };
+ await _auctionService.InsertBid(bid2);
+
+ //Act
+ var result = await _auctionService.GetBidsByProductId("1");
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual(2, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task GetBidsByCustomerIdTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ var bid2 = new Bid() {
+ Amount = 12,
+ ProductId = "1",
+ CustomerId = "2",
+ Date = DateTime.UtcNow
+ };
+ await _auctionService.InsertBid(bid2);
+
+ //Act
+ var result = await _auctionService.GetBidsByCustomerId("1");
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual(1, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task InsertBidTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ Assert.AreEqual(1, _repository.Table.Count());
+ }
+
+ [TestMethod()]
+ public async Task UpdateBidTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ bid1.Amount = 12;
+ //Act
+ await _auctionService.UpdateBid(bid1);
+ //Assert
+ Assert.AreEqual(12, _repository.Table.FirstOrDefault().Amount);
+ }
+
+ [TestMethod()]
+ public async Task DeleteBidTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ //Act
+ await _auctionService.DeleteBid(bid1);
+ //Assert
+ Assert.AreEqual(0, _repository.Table.Count());
+ }
+
+ [TestMethod()]
+ public async Task UpdateHighestBidTest()
+ {
+ //Arrange
+ var product = new Product();
+ _productrepository.Insert(product);
+
+ //Act
+ await _auctionService.UpdateHighestBid(product, 15, "sample");
+
+ //Assert
+ Assert.AreEqual(15, _productrepository.Table.FirstOrDefault(x => x.Id == product.Id).HighestBid);
+ }
+
+ [TestMethod()]
+ public async Task GetAuctionsToEndTest()
+ {
+ //Arrange
+ var product1 = new Product() {
+ ProductTypeId = ProductType.Auction,
+ AvailableEndDateTimeUtc = DateTime.UtcNow.AddDays(1),
+ };
+ _productrepository.Insert(product1);
+ var product2 = new Product() {
+ ProductTypeId = ProductType.Auction,
+ AvailableEndDateTimeUtc = DateTime.UtcNow.AddDays(-1),
+ };
+ _productrepository.Insert(product2);
+
+ //Act
+ var result = await _auctionService.GetAuctionsToEnd();
+
+ //Assert
+ Assert.AreEqual(1, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task UpdateAuctionEndedTest()
+ {
+ //Arrange
+ var product1 = new Product() {
+ ProductTypeId = ProductType.Auction,
+ AvailableEndDateTimeUtc = DateTime.UtcNow.AddDays(1),
+ };
+ _productrepository.Insert(product1);
+
+ //Act
+ await _auctionService.UpdateAuctionEnded(product1, true, true);
+
+ //Assert
+ Assert.AreEqual(true, _productrepository.Table.FirstOrDefault(x => x.Id == product1.Id).AuctionEnded);
+ }
+
+ [TestMethod()]
+ public async Task NewBidTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ //Act
+ await _auctionService.NewBid(new Customer() { Id = "1" }, new Product() { Id = "1" }, new Domain.Stores.Store(), new Domain.Localization.Language(), "", 12);
+ //Assert
+ Assert.AreEqual(12, _repository.Table.Where(x => x.ProductId == "1").Max(x => x.Amount));
+ }
+
+ [TestMethod()]
+ public async Task CancelBidByOrderTest()
+ {
+ //Arrange
+ var bid1 = new Bid() {
+ Amount = 10,
+ ProductId = "1",
+ CustomerId = "1",
+ OrderId = "1",
+ Date = DateTime.UtcNow.AddDays(-1)
+ };
+ await _auctionService.InsertBid(bid1);
+ //Act
+ await _auctionService.CancelBidByOrder("1");
+ //Assert
+ Assert.AreEqual(0, _repository.Table.Count());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs
index dbb473979..8e834497d 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs
@@ -26,7 +26,7 @@ namespace Grand.Business.Catalog.Tests.Service.Products
}
[TestMethod()]
- public void Can_calculate_total_quantity_when_we_do_not_use_multiple_warehouses()
+ public void GetTotalStockQuantity_multiple_warehouses()
{
//if UseMultipleWarehouses is set to false, it will ignore StockQuantity of attached Warhouses
var product = new Product {
@@ -43,7 +43,7 @@ namespace Grand.Business.Catalog.Tests.Service.Products
}
[TestMethod()]
- public void Can_calculate_total_quantity_when_we_do_use_multiple_warehouses_with_reserved()
+ public void GetTotalStockQuantity_multiple_warehouses_with_reserved()
{
var product = new Product {
ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
@@ -58,16 +58,17 @@ namespace Grand.Business.Catalog.Tests.Service.Products
product.ProductWarehouseInventory.Add(
new ProductWarehouseInventory { WarehouseId = "3", StockQuantity = 4, ReservedQuantity = 444 });
+ var stock = _stockQuantityService.GetTotalStockQuantity(product, useReservedQuantity: true, total: true);
//argument is true, so it will consider borh StockQuantity and ReservedQuantity
//equation:
//available quantity = StockQuantity - ReservedQuantity
//-1320 = 12 - 1332
//looks like our Warehouse is lacking of stuff in number -1320
- Assert.AreEqual(-1320, _stockQuantityService.GetTotalStockQuantity(product, useReservedQuantity: true, total: true));
+ Assert.AreEqual(-1320, stock);
}
[TestMethod()]
- public void Can_calculate_total_quantity_when_we_do_use_multiple_warehouses_without_reserved()
+ public void GetTotalStockQuantity_multiple_warehouses_without_reserved()
{
//if UseMultipleWarehouses is set to true, it will show StockQuantity of attached Warhouses
var product = new Product {
@@ -82,13 +83,13 @@ namespace Grand.Business.Catalog.Tests.Service.Products
new ProductWarehouseInventory { WarehouseId = "2", StockQuantity = 4, ReservedQuantity = 222 });
product.ProductWarehouseInventory.Add(
new ProductWarehouseInventory { WarehouseId = "3", StockQuantity = 4, ReservedQuantity = 111 });
-
+ var stock = _stockQuantityService.GetTotalStockQuantity(product, useReservedQuantity: false, total: true);
//it will ignore ReservedQuantity.. it is important to sell stuff, not having stuff
- Assert.AreEqual(12, _stockQuantityService.GetTotalStockQuantity(product, useReservedQuantity: false, total: true));
+ Assert.AreEqual(12, stock);
}
[TestMethod()]
- public void Can_calculate_total_quantity_when_we_do_use_multiple_warehouses_with_warehouse_specified()
+ public void GetTotalStockQuantity_multiple_warehouses_with_warehouse_specified()
{
//show only specified Warehouse (by WarehouseID)
var product = new Product {
@@ -104,9 +105,10 @@ namespace Grand.Business.Catalog.Tests.Service.Products
product.ProductWarehouseInventory.Add(
new ProductWarehouseInventory { WarehouseId = "987", StockQuantity = 0, ReservedQuantity = 0 });
+ var stock = _stockQuantityService.GetTotalStockQuantity(product, true, "654");
//only warehouse with ID = 654,
//-1 = 1500-1501
- Assert.AreEqual(-1, _stockQuantityService.GetTotalStockQuantity(product, true, "654"));
+ Assert.AreEqual(-1, stock);
}