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

supportfe07c60ea35a
diff --git a/src/Tests/Grand.Business.Common.Tests/Grand.Business.Common.Tests.csproj b/src/Tests/Grand.Business.Common.Tests/Grand.Business.Common.Tests.csproj
index 73744d3f4..245b05ef0 100644
--- a/src/Tests/Grand.Business.Common.Tests/Grand.Business.Common.Tests.csproj
+++ b/src/Tests/Grand.Business.Common.Tests/Grand.Business.Common.Tests.csproj
@@ -4,15 +4,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
- <ItemGroup>
- <Compile Remove="Services\Pdf\**" />
- <Compile Remove="Services\Seo\**" />
- <EmbeddedResource Remove="Services\Pdf\**" />
- <EmbeddedResource Remove="Services\Seo\**" />
- <None Remove="Services\Pdf\**" />
- <None Remove="Services\Seo\**" />
- </ItemGroup>
-
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.2" />
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Logging/CustomerActivityServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Logging/CustomerActivityServiceTests.cs
new file mode 100644
index 000000000..dea2a50b3
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Logging/CustomerActivityServiceTests.cs
@@ -0,0 +1,253 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Grand.Domain.Localization;
+using Grand.Domain.Logging;
+using Grand.Infrastructure;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Common.Services.Logging.Tests
+{
+ [TestClass()]
+ public class CustomerActivityServiceTests
+ {
+ private IRepository<ActivityLog> _repository;
+ private IRepository<ActivityLogType> _repositoryActivityLogType;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+
+ private CustomerActivityService _customerActivityService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<ActivityLog>();
+ _repositoryActivityLogType = new MongoDBRepositoryTest<ActivityLogType>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _customerActivityService = new CustomerActivityService(_cacheBase, _repository, _repositoryActivityLogType);
+ }
+
+ private async Task InsertActivityType()
+ {
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "test", SystemKeyword = "test" });
+ }
+
+ [TestMethod()]
+ public async Task InsertActivityTypeTest()
+ {
+ //Act
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Enabled = true, Name = "test", SystemKeyword = "test" });
+ //Assert
+ Assert.IsTrue(_repositoryActivityLogType.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task UpdateActivityTypeTest()
+ {
+ //Arrange
+ var activityLogType = new ActivityLogType();
+ await _customerActivityService.InsertActivityType(activityLogType);
+
+ //Act
+ activityLogType.Enabled = true;
+ await _customerActivityService.UpdateActivityType(activityLogType);
+ //Assert
+ Assert.IsTrue(_repositoryActivityLogType.Table.FirstOrDefault(x=>x.Id == activityLogType.Id).Enabled);
+ }
+
+ [TestMethod()]
+ public async Task DeleteActivityTypeTest()
+ {
+ //Arrange
+ var activityLogType = new ActivityLogType();
+ await _customerActivityService.InsertActivityType(activityLogType);
+
+ //Act
+ await _customerActivityService.DeleteActivityType(activityLogType);
+ //Assert
+ Assert.IsNull(_repositoryActivityLogType.Table.FirstOrDefault(x => x.Id == activityLogType.Id));
+ }
+
+ [TestMethod()]
+ public async Task GetAllActivityTypesTest()
+ {
+ //Arrange
+ var activityLogType = new ActivityLogType();
+ await _customerActivityService.InsertActivityType(activityLogType);
+
+ //Act
+ var result = await _customerActivityService.GetAllActivityTypes();
+ //Assert
+ Assert.AreEqual(1, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task GetActivityTypeByIdTest()
+ {
+ //Arrange
+ var activityLogType = new ActivityLogType();
+ await _customerActivityService.InsertActivityType(activityLogType);
+
+ //Act
+ var result = await _customerActivityService.GetActivityTypeById(activityLogType.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task InsertActivityTest()
+ {
+ await InsertActivityType();
+ //Act
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1"});
+ //Assert
+ //TODO
+ Assert.IsTrue(true);
+ }
+
+ [TestMethod()]
+ public async Task DeleteActivityTest()
+ {
+ await InsertActivityType();
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+ var activityLogs = await _customerActivityService.GetAllActivities();
+ //Act
+ await _customerActivityService.DeleteActivity(activityLogs.FirstOrDefault());
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task GetAllActivitiesTest()
+ {
+ //Arrange
+ await InsertActivityType();
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+ //Act
+ var activityLogs = await _customerActivityService.GetAllActivities();
+ //Assert
+ //TODO
+ Assert.IsTrue(activityLogs.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetStatsActivitiesTest()
+ {
+ //Arrange
+ await InsertActivityType();
+ await _customerActivityService.InsertActivity("test", "1", new Customer(), "", "");
+ //Act
+ var activityLogs = await _customerActivityService.GetStatsActivities();
+ //Assert
+ Assert.AreEqual(1, activityLogs.Count);
+ }
+
+ [TestMethod()]
+ public async Task GetCategoryActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "AddNewCategory", SystemKeyword = "AddNewCategory" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+
+ //Act
+ var activities = await _customerActivityService.GetCategoryActivities(categoryId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetKnowledgebaseCategoryActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() {Id = "1", Enabled = true, Name = "CreateKnowledgebaseCategory", SystemKeyword = "CreateKnowledgebaseCategory" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+ //Act
+ var activities = await _customerActivityService.GetKnowledgebaseCategoryActivities(categoryId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+
+ }
+
+ [TestMethod()]
+ public async Task GetKnowledgebaseArticleActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "CreateKnowledgebaseArticle", SystemKeyword = "CreateKnowledgebaseArticle" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+
+ //Act
+ var activities = await _customerActivityService.GetKnowledgebaseArticleActivities(categoryId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetBrandActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "AddNewBrand", SystemKeyword = "AddNewBrand" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+
+ //Act
+ var activities = await _customerActivityService.GetBrandActivities(brandId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetCollectionActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "AddNewCollection", SystemKeyword = "AddNewCollection" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+ //Act
+ var activities = await _customerActivityService.GetCollectionActivities(collectionId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetProductActivitiesTest()
+ {
+ //Arrange
+ await _customerActivityService.InsertActivityType(new ActivityLogType() { Id = "1", Enabled = true, Name = "AddNewProduct", SystemKeyword = "AddNewProduct" });
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+
+ //Act
+ var activities = await _customerActivityService.GetProductActivities(productId:"1");
+ //Assert
+ Assert.IsTrue(activities.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetActivityByIdTest()
+ {
+ //Arrange
+ await InsertActivityType();
+ await _repository.InsertAsync(new ActivityLog() { ActivityLogTypeId = "1", EntityKeyId = "1" });
+ //Act
+ var activityLogs = await _customerActivityService.GetAllActivities();
+ var result = await _customerActivityService.GetActivityById(activityLogs[0].Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task ClearAllActivitiesTest()
+ {
+ //Arrange
+ await InsertActivityType();
+ await _customerActivityService.InsertActivity("test", "1", new Customer(), "", "");
+ //Act
+ await _customerActivityService.ClearAllActivities();
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Logging/DefaultLoggerTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Logging/DefaultLoggerTests.cs
new file mode 100644
index 000000000..42de501db
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Logging/DefaultLoggerTests.cs
@@ -0,0 +1,94 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Data;
+using Grand.Domain.Logging;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Business.Common.Services.Logging.Tests
+{
+ [TestClass()]
+ public class DefaultLoggerTests
+ {
+ private IRepository<Log> _repository;
+ private DefaultLogger _logger;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<Log>();
+ _logger = new DefaultLogger(_repository);
+ }
+
+ [TestMethod()]
+ public async Task GetAllLogsTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new Log());
+ await _repository.InsertAsync(new Log());
+ await _repository.InsertAsync(new Log());
+ //Act
+ var result = await _logger.GetAllLogs();
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task GetLogByIdTest()
+ {
+ //Arrange
+ var log = new Log();
+ await _repository.InsertAsync(log);
+ //Act
+ var result = await _logger.GetLogById(log.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetLogByIdsTest()
+ {
+ //Arrange
+ var log = new Log();
+ await _repository.InsertAsync(log);
+ //Act
+ var result = await _logger.GetLogByIds(new[] { log.Id });
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task InsertLogTest()
+ {
+ //Act
+ await _logger.InsertLog(LogLevel.Debug, "message");
+ //Assert
+ Thread.Sleep(100);
+ Assert.IsTrue(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task DeleteLogTest()
+ {
+ //Arrange
+ var log = new Log();
+ await _repository.InsertAsync(log);
+ //Act
+ await _logger.DeleteLog(log);
+ var result = await _logger.GetLogById(log.Id);
+ //Assert
+ Assert.IsNull(result);
+ }
+
+ [TestMethod()]
+ public async Task ClearLogTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new Log());
+ await _repository.InsertAsync(new Log());
+ await _repository.InsertAsync(new Log());
+ //Act
+ await _logger.ClearLog();
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugServiceTests.cs
new file mode 100644
index 000000000..69f73e354
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugServiceTests.cs
@@ -0,0 +1,137 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Data;
+using Grand.Domain.Seo;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Common.Services.Seo.Tests
+{
+ [TestClass()]
+ public class SlugServiceTests
+ {
+ private IRepository<EntityUrl> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+
+ private SlugService _slugService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<EntityUrl>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _slugService = new SlugService(_cacheBase, _repository, new Infrastructure.Configuration.PerformanceConfig());
+ }
+
+ [TestMethod()]
+ public async Task GetEntityUrlByIdTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ await _repository.InsertAsync(entity);
+ //Act
+ var result = _slugService.GetEntityUrlById(entity.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task InsertEntityUrlTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ //Act
+ await _slugService.InsertEntityUrl(entity);
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task UpdateEntityUrlTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ await _slugService.InsertEntityUrl(entity);
+ //Act
+ entity.Slug = "slug2";
+ await _slugService.UpdateEntityUrl(entity);
+ //Assert
+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == entity.Id).Slug == entity.Slug);
+ }
+
+ [TestMethod()]
+ public async Task DeleteEntityUrlTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ await _slugService.InsertEntityUrl(entity);
+ //Act
+ await _slugService.DeleteEntityUrl(entity);
+ //Assert
+ Assert.IsNull(_repository.Table.FirstOrDefault(x => x.Id == entity.Id));
+ }
+
+ [TestMethod()]
+ public async Task GetBySlugTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ await _slugService.InsertEntityUrl(entity);
+ //Act
+ var result = await _slugService.GetBySlug(entity.Slug);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetBySlugCachedTest()
+ {
+ //Arrange
+ var entity = new EntityUrl() { Slug = "slug" };
+ await _slugService.InsertEntityUrl(entity);
+ //Act
+ var result = await _slugService.GetBySlugCached(entity.Slug);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetAllEntityUrlTest()
+ {
+ //Arrange
+ await _slugService.InsertEntityUrl(new EntityUrl());
+ await _slugService.InsertEntityUrl(new EntityUrl());
+ await _slugService.InsertEntityUrl(new EntityUrl());
+ //Act
+ var result = await _slugService.GetAllEntityUrl();
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task GetActiveSlugTest()
+ {
+ //Arrange
+ await _slugService.InsertEntityUrl(new EntityUrl() { IsActive = true });
+ //Act
+ var result = await _slugService.GetAllEntityUrl();
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task SaveSlugTest()
+ {
+ //Act
+ await _slugService.SaveSlug<Category>(new Category(), "slug", "1");
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.System.Tests/Services/BackgroundService/QueuedMessagesSendScheduleTaskTests.cs b/src/Tests/Grand.Business.System.Tests/Services/BackgroundService/QueuedMessagesSendScheduleTaskTests.cs
index c68d89087..2036cac36 100644
--- a/src/Tests/Grand.Business.System.Tests/Services/BackgroundService/QueuedMessagesSendScheduleTaskTests.cs
+++ b/src/Tests/Grand.Business.System.Tests/Services/BackgroundService/QueuedMessagesSendScheduleTaskTests.cs
@@ -57,7 +57,7 @@ namespace Grand.Business.System.Tests.Services.BackgroundService
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()
, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(),
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IEnumerable<string>>())).ThrowsAsync(new Exception());
- _loggerMock.Setup(c => c.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
+
await _task.Execute();
_loggerMock.Verify(c => c.InsertLog(Domain.Logging.LogLevel.Error, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Customer>(), null, null, null), Times.Once);
_queuedEmailServiceMock.Verify(c => c.UpdateQueuedEmail(It.IsAny<QueuedEmail>()), Times.Once);