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

supporte3f52f1a0513
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs
new file mode 100644
index 000000000..e81a8725a
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs
@@ -0,0 +1,157 @@
+using Grand.Business.Common.Services.Security;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Security;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Grand.Domain.Shipping;
+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 InventoryManageServiceTests
+ {
+ private IRepository<Product> _repository;
+ private IRepository<InventoryJournal> _repositoryInventoryJournal;
+ private Mock<IWorkContext> _workContextMock;
+ private Mock<IMediator> _mediatorMock;
+ private CatalogSettings _settings;
+ private InventoryManageService _inventoryManageService;
+ private MemoryCacheBase _cacheBase;
+ private StockQuantityService _stockQuantityService;
+ private Mock<ITranslationService> _translationService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ CommonPath.BaseDirectory = "";
+
+ CommonPath.BaseDirectory = "";
+
+ _repository = new MongoDBRepositoryTest<Product>();
+ _repositoryInventoryJournal = new MongoDBRepositoryTest<InventoryJournal>();
+ _workContextMock = new Mock<IWorkContext>();
+ _translationService = new Mock<ITranslationService>();
+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store() { Id = "" });
+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());
+ _mediatorMock = new Mock<IMediator>();
+ _settings = new CatalogSettings();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _stockQuantityService = new StockQuantityService(_translationService.Object);
+ _inventoryManageService = new InventoryManageService(_repository, _repositoryInventoryJournal, _stockQuantityService, _cacheBase, _mediatorMock.Object, _settings);
+ }
+
+
+
+ [TestMethod()]
+ public async Task AdjustReservedTest_ManageStock_Reserve()
+ {
+ //Arrange
+ var product = new Product() {
+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
+ ReservedQuantity = 5,
+ };
+ await _repository.InsertAsync(product);
+ //Act
+ await _inventoryManageService.AdjustReserved(product, -10);
+ product = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
+ //Assert
+ Assert.AreEqual(15, product.ReservedQuantity);
+ }
+
+ [TestMethod()]
+ public async Task AdjustReservedTest_ManageStock_UnblockReserved()
+ {
+ //Arrange
+ var product = new Product() {
+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
+ ReservedQuantity = 10,
+ };
+ await _repository.InsertAsync(product);
+ //Act
+ await _inventoryManageService.AdjustReserved(product, 8);
+ product = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
+ //Assert
+ Assert.AreEqual(2, product.ReservedQuantity);
+ }
+
+ [TestMethod()]
+ public async Task BookReservedInventoryTest_ManageStock()
+ {
+ //Arrange
+ var product = new Product() {
+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
+ StockQuantity = 10,
+ ReservedQuantity = 10,
+ };
+ await _repository.InsertAsync(product);
+
+ var shipment = new Shipment();
+ var shipmentItem = new Domain.Shipping.ShipmentItem() { Quantity = 10 };
+
+ //Act
+ await _inventoryManageService.BookReservedInventory(product, shipment, shipmentItem);
+ product = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
+ //Assert
+ Assert.AreEqual(0, product.ReservedQuantity);
+ Assert.AreEqual(0, product.StockQuantity);
+ }
+
+ [TestMethod()]
+ public async Task ReverseBookedInventoryTest()
+ {
+ //Arrange
+ var product = new Product() {
+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
+ StockQuantity = 10,
+ ReservedQuantity = 10,
+ };
+ await _repository.InsertAsync(product);
+
+ var shipment = new Shipment();
+ var shipmentItem = new Domain.Shipping.ShipmentItem() { Quantity = 10 };
+ await _inventoryManageService.BookReservedInventory(product, shipment, shipmentItem);
+
+ //Act
+ await _inventoryManageService.ReverseBookedInventory(shipment, shipmentItem);
+
+ product = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
+
+ //Assert
+ Assert.AreEqual(10, product.ReservedQuantity);
+ Assert.AreEqual(10, product.StockQuantity);
+ }
+
+ [TestMethod()]
+ public async Task UpdateStockProductTest()
+ {
+ //Arrange
+ var product = new Product() {
+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,
+ StockQuantity = 0,
+ ReservedQuantity = 0,
+ };
+ await _repository.InsertAsync(product);
+
+ //Act
+ product.StockQuantity = 10;
+ product.ReservedQuantity = 10;
+ await _inventoryManageService.UpdateStockProduct(product);
+
+ product = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
+
+ //Assert
+ Assert.AreEqual(10, product.ReservedQuantity);
+ Assert.AreEqual(10, product.StockQuantity);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs
new file mode 100644
index 000000000..68aa9cd84
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs
@@ -0,0 +1,272 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Grand.Business.Catalog.Services.Products;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Grand.Domain.Data;
+using Grand.Domain.Catalog;
+using MediatR;
+using Moq;
+using Grand.Data.Tests.MongoDb;
+using Grand.SharedKernel.Extensions;
+using Grand.Domain.Common;
+using Grand.Business.Core.Commands.Catalog;
+
+namespace Grand.Business.Catalog.Services.Products.Tests
+{
+ [TestClass()]
+ public class OutOfStockSubscriptionServiceTests
+ {
+ private IRepository<OutOfStockSubscription> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private OutOfStockSubscriptionService _outOfStockSubscriptionService;
+
+ [TestInitialize()]
+ public void InitializeTests()
+ {
+ CommonPath.BaseDirectory = "";
+
+ _repository = new MongoDBRepositoryTest<OutOfStockSubscription>();
+ _mediatorMock = new Mock<IMediator>();
+ _mediatorMock.Setup(x => x.Send(It.IsAny<SendNotificationsToSubscribersCommand>(), default))
+ .Returns(Task.FromResult((IList<OutOfStockSubscription>)new List<OutOfStockSubscription>()));
+
+ _outOfStockSubscriptionService = new OutOfStockSubscriptionService(_repository, _mediatorMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task GetAllSubscriptionsByCustomerIdTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ };
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ var result = await _outOfStockSubscriptionService.GetAllSubscriptionsByCustomerId("1");
+
+ //Assert
+ Assert.AreEqual(2, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task FindSubscriptionTest_IsNotNull()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "2",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ ProductId = "3",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ var result = await _outOfStockSubscriptionService.FindSubscription("1", "1", new List<CustomAttribute>(), "", "");
+
+ //Assert
+ Assert.IsNotNull(result);
+ }
+ [TestMethod()]
+ public async Task FindSubscriptionTest_IsNull()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "2",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ ProductId = "3",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ var result = await _outOfStockSubscriptionService.FindSubscription("1", "1", new List<CustomAttribute>(), "1", "");
+
+ //Assert
+ Assert.IsNull(result);
+ }
+ [TestMethod()]
+ public async Task GetSubscriptionByIdTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "2",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ ProductId = "3",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ var result = await _outOfStockSubscriptionService.GetSubscriptionById(outOfStockSubscription1.Id);
+
+ //Assert
+ Assert.IsNotNull(result);
+
+ }
+
+ [TestMethod()]
+ public async Task InsertSubscriptionTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ //Act
+ await _outOfStockSubscriptionService.InsertSubscription(outOfStockSubscription1);
+
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ Assert.IsTrue(_repository.Table.Count() == 1);
+ }
+
+ [TestMethod()]
+ public async Task UpdateSubscriptionTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _outOfStockSubscriptionService.InsertSubscription(outOfStockSubscription1);
+ outOfStockSubscription1.CustomerId = "2";
+ //Act
+ await _outOfStockSubscriptionService.UpdateSubscription(outOfStockSubscription1);
+ //Assert
+ Assert.IsTrue(_repository.Table.FirstOrDefault().CustomerId == "2");
+ }
+
+ [TestMethod()]
+ public async Task DeleteSubscriptionTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _outOfStockSubscriptionService.InsertSubscription(outOfStockSubscription1);
+ //Act
+ await _outOfStockSubscriptionService.DeleteSubscription(outOfStockSubscription1);
+
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ Assert.IsTrue(_repository.Table.Count() == 0);
+ }
+
+ [TestMethod()]
+ public async Task SendNotificationsToSubscribersTest()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "2",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ await _outOfStockSubscriptionService.SendNotificationsToSubscribers(new Product() { Id = "1" }, "");
+
+ //Assert
+ _mediatorMock.Verify(c => c.Send(It.IsAny<SendNotificationsToSubscribersCommand>(), default), Times.AtLeastOnce);
+ }
+
+ [TestMethod()]
+ public async Task SendNotificationsToSubscribersTest1()
+ {
+ //Arange
+ var outOfStockSubscription1 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ outOfStockSubscription1.Attributes.Add(new CustomAttribute() { Key = "MyKey", Value = "1" });
+
+ var outOfStockSubscription2 = new OutOfStockSubscription() {
+ CustomerId = "1",
+ ProductId = "2",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ var outOfStockSubscription3 = new OutOfStockSubscription() {
+ CustomerId = "2",
+ ProductId = "1",
+ StoreId = "",
+ WarehouseId = "",
+ };
+ await _repository.InsertManyAsync(new[] { outOfStockSubscription1, outOfStockSubscription2, outOfStockSubscription3 });
+
+ //Act
+ await _outOfStockSubscriptionService.SendNotificationsToSubscribers(new Product() { Id = "1" }, new CustomAttribute[] { new CustomAttribute { Key = "MyKey", Value = "1" } }, "");
+
+ //Assert
+ _mediatorMock.Verify(c => c.Send(It.IsAny<SendNotificationsToSubscribersCommand>(), default), Times.AtLeastOnce);
+ }
+ }
+}
\ No newline at end of file