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

supportaa6543c016fc
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/RecentlyViewedProductsServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/RecentlyViewedProductsServiceTests.cs
new file mode 100644
index 000000000..596e00290
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/RecentlyViewedProductsServiceTests.cs
@@ -0,0 +1,64 @@
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Data;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Catalog.Services.Products.Tests
+{
+ [TestClass()]
+ public class RecentlyViewedProductsServiceTests
+ {
+ private IRepository<RecentlyViewedProduct> _repository;
+ private MemoryCacheBase _cacheBase;
+ private Mock<IMediator> _mediatorMock;
+ private Mock<IProductService> _productServiceMock;
+ RecentlyViewedProductsService _service;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<RecentlyViewedProduct>();
+ _mediatorMock = new Mock<IMediator>();
+ _productServiceMock = new Mock<IProductService>();
+ _productServiceMock.Setup(a => a.GetProductsByIds(It.IsAny<string[]>(), false)).Returns(() => Task.FromResult((IList<Product>)new List<Product>() { new Product() { Id = "1", Published = true }, new Product() { Id = "2", Published = true }, new Product() { Id = "3", Published = true } }));
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _service = new RecentlyViewedProductsService(_productServiceMock.Object, _cacheBase, new CatalogSettings() { RecentlyViewedProductsEnabled = true, RecentlyViewedProductsNumber = 10 }, _repository);
+ }
+
+
+ [TestMethod()]
+ public async Task GetRecentlyViewedProductsTest()
+ {
+ //Arrange
+ await _service.AddProductToRecentlyViewedList("1", "1");
+ await _service.AddProductToRecentlyViewedList("1", "2");
+ await _service.AddProductToRecentlyViewedList("1", "3");
+ await _service.AddProductToRecentlyViewedList("2", "1");
+
+ //Act
+ var result = await _service.GetRecentlyViewedProducts("1", 10);
+
+ //Assert
+ Assert.AreEqual(3, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task AddProductToRecentlyViewedListTest()
+ {
+ //Act
+ await _service.AddProductToRecentlyViewedList("1", "1");
+ await _service.AddProductToRecentlyViewedList("1", "2");
+ await _service.AddProductToRecentlyViewedList("1", "3");
+ await _service.AddProductToRecentlyViewedList("2", "1");
+
+
+ //Assert
+ Assert.AreEqual(4, _repository.Table.Count());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/SpecificationAttributeServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/SpecificationAttributeServiceTests.cs
new file mode 100644
index 000000000..c49003cb4
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/SpecificationAttributeServiceTests.cs
@@ -0,0 +1,246 @@
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Data;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Catalog.Services.Products.Tests
+{
+ [TestClass()]
+ public class SpecificationAttributeServiceTests
+ {
+
+ SpecificationAttributeService service;
+ private IRepository<SpecificationAttribute> _repository;
+ private IRepository<Product> _repositoryProduct;
+ private MemoryCacheBase _cacheBase;
+ private Mock<IMediator> _mediatorMock;
+ private Mock<IProductService> _productServiceMock;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<SpecificationAttribute>();
+ _repositoryProduct = new MongoDBRepositoryTest<Product>();
+ _mediatorMock = new Mock<IMediator>();
+ _productServiceMock = new Mock<IProductService>();
+ _productServiceMock.Setup(a => a.GetProductsByIds(It.IsAny<string[]>(), false)).Returns(() => Task.FromResult((IList<Product>)new List<Product>() { new Product() { Id = "1", Published = true }, new Product() { Id = "2", Published = true }, new Product() { Id = "3", Published = true } }));
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ service = new SpecificationAttributeService(_cacheBase, _repository, _repositoryProduct, _mediatorMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task GetSpecificationAttributeByIdTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test"
+ };
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ var result = await service.GetSpecificationAttributeById(specificationAttribute.Id);
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual("test", result.Name);
+ }
+
+ [TestMethod()]
+ public async Task GetSpecificationAttributeBySeNameTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ var result = await service.GetSpecificationAttributeBySeName("test");
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual("test", result.Name);
+ }
+
+ [TestMethod()]
+ public async Task GetSpecificationAttributesTest()
+ {
+ //Arrange
+ await service.InsertSpecificationAttribute(new SpecificationAttribute());
+ await service.InsertSpecificationAttribute(new SpecificationAttribute());
+ await service.InsertSpecificationAttribute(new SpecificationAttribute());
+
+ //Act
+ var result = await service.GetSpecificationAttributes();
+
+ //Assert
+ Assert.IsNotNull(result);
+ Assert.AreEqual(3, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task InsertSpecificationAttributeTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+
+ //Act
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Assert
+ Assert.AreEqual(1, _repository.Table.Count());
+ }
+
+ [TestMethod()]
+ public async Task UpdateSpecificationAttributeTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ specificationAttribute.Name = "test2";
+ await service.UpdateSpecificationAttribute(specificationAttribute);
+
+ //Assert
+ Assert.AreEqual("test2", _repository.Table.FirstOrDefault(x=>x.Id == specificationAttribute.Id).Name);
+ }
+
+ [TestMethod()]
+ public async Task DeleteSpecificationAttributeTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ await service.DeleteSpecificationAttribute(specificationAttribute);
+
+ //Assert
+ Assert.IsNull(_repository.Table.FirstOrDefault(x => x.Id == specificationAttribute.Id));
+ }
+
+ [TestMethod()]
+ public async Task GetSpecificationAttributeByOptionIdTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+ var attr = new SpecificationAttributeOption();
+ specificationAttribute.SpecificationAttributeOptions.Add(attr);
+
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ var result = await service.GetSpecificationAttributeByOptionId(attr.Id);
+
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task DeleteSpecificationAttributeOptionTest()
+ {
+ //Arrange
+ var specificationAttribute = new SpecificationAttribute() {
+ Name = "test",
+ SeName = "test"
+ };
+ var attr = new SpecificationAttributeOption();
+ specificationAttribute.SpecificationAttributeOptions.Add(attr);
+
+ await service.InsertSpecificationAttribute(specificationAttribute);
+
+ //Act
+ await service.DeleteSpecificationAttributeOption(attr);
+
+ //Assert
+ Assert.AreEqual(0, _repository.Table.FirstOrDefault(x=>x.Id == specificationAttribute.Id).SpecificationAttributeOptions.Count);
+ }
+
+ [TestMethod()]
+ public async Task InsertProductSpecificationAttributeTest()
+ {
+ //Arrange
+ var product = new Product();
+
+ await _repositoryProduct.InsertAsync(product);
+
+ //Act
+ var attr = new ProductSpecificationAttribute();
+ await service.InsertProductSpecificationAttribute(attr, product.Id);
+
+ //Assert
+ Assert.AreEqual(1, _repositoryProduct.Table.FirstOrDefault(x => x.Id == product.Id).ProductSpecificationAttributes.Count);
+ }
+
+ [TestMethod()]
+ public async Task UpdateProductSpecificationAttributeTest()
+ {
+ //Arrange
+ var product = new Product();
+ await _repositoryProduct.InsertAsync(product);
+ var attr = new ProductSpecificationAttribute();
+ await service.InsertProductSpecificationAttribute(attr, product.Id);
+
+ //Act
+ attr.CustomName = "test";
+ await service.UpdateProductSpecificationAttribute(attr, product.Id);
+
+ //Assert
+ Assert.AreEqual("test", _repositoryProduct.Table.FirstOrDefault(x => x.Id == product.Id).ProductSpecificationAttributes.FirstOrDefault(x=>x.Id == attr.Id).CustomName);
+ }
+
+ [TestMethod()]
+ public async Task DeleteProductSpecificationAttributeTest()
+ {
+ //Arrange
+ var product = new Product();
+ await _repositoryProduct.InsertAsync(product);
+
+ var attr = new ProductSpecificationAttribute();
+ await service.InsertProductSpecificationAttribute(attr, product.Id);
+
+ //Act
+ await service.DeleteProductSpecificationAttribute(attr, product.Id);
+
+ //Assert
+ Assert.AreEqual(0, _repositoryProduct.Table.FirstOrDefault(x => x.Id == product.Id).ProductSpecificationAttributes.Count);
+
+ }
+
+ [TestMethod()]
+ public async Task GetProductSpecificationAttributeCountTest()
+ {
+ //Arrange
+ var product = new Product();
+ await _repositoryProduct.InsertAsync(product);
+
+ var attr = new ProductSpecificationAttribute();
+ await service.InsertProductSpecificationAttribute(attr, product.Id);
+
+ //Act
+ var result = service.GetProductSpecificationAttributeCount();
+
+ //Assert
+ Assert.AreEqual(1,result);
+ }
+ }
+}
\ No newline at end of file