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

supportf3f789bf0306
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
index 0b52b1f37..29d27194d 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
@@ -129,6 +129,7 @@ namespace Grand.Business.Catalog.Services.Products.Tests
[TestMethod()]
public async Task GetProductsByIdsTest()
{
+ await insertSampleProducts();
//Act
var result = await _productService.GetProductsByIds(new[] { "1" });
diff --git a/src/Tests/Grand.Business.Common.Tests/Events/GroupDeletedEventHandlerTests.cs b/src/Tests/Grand.Business.Common.Tests/Events/GroupDeletedEventHandlerTests.cs
new file mode 100644
index 000000000..4c1e7433f
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Events/GroupDeletedEventHandlerTests.cs
@@ -0,0 +1,45 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Business.Common.Events.Tests
+{
+ [TestClass()]
+ public class GroupDeletedEventHandlerTests
+ {
+
+ private IRepository<Customer> _repository;
+ private GroupDeletedEventHandler _handler;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<Customer>();
+ _handler = new GroupDeletedEventHandler(_repository);
+ }
+
+ [TestMethod()]
+ public async Task HandleTest()
+ {
+ //Arrange
+ var customer1 = new Customer();
+ customer1.Groups.Add("1");
+ customer1.Groups.Add("2");
+ customer1.Groups.Add("3");
+ await _repository.InsertAsync(customer1);
+ var customer2 = new Customer();
+ customer2.Groups.Add("1");
+ customer2.Groups.Add("2");
+ customer2.Groups.Add("3");
+ await _repository.InsertAsync(customer2);
+ //Act
+ var notification = new Infrastructure.Events.EntityDeleted<CustomerGroup>(new CustomerGroup() { Id = "1" });
+ await _handler.Handle(notification, CancellationToken.None);
+
+ //Assert
+ var count = _repository.Table.Where(x => x.Groups.Contains("1")).Count();
+ Assert.AreEqual(0, count);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Events/LanguageDeletedEventHandlerTests.cs b/src/Tests/Grand.Business.Common.Tests/Events/LanguageDeletedEventHandlerTests.cs
new file mode 100644
index 000000000..30c1d4782
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Events/LanguageDeletedEventHandlerTests.cs
@@ -0,0 +1,50 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Grand.Business.Common.Events;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Moq;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Configuration;
+using Grand.Domain.Localization;
+
+namespace Grand.Business.Common.Events.Tests
+{
+ [TestClass()]
+ public class LanguageDeletedEventHandlerTests
+ {
+ private Mock<ILanguageService> _languageServiceMock;
+ private Mock<ISettingService> _settingServiceMock;
+
+ private LanguageSettings _languageSettings;
+
+ private LanguageDeletedEventHandler _handler;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _languageServiceMock = new Mock<ILanguageService>();
+ _settingServiceMock = new Mock<ISettingService>();
+ _languageSettings = new LanguageSettings() { DefaultAdminLanguageId = "1" };
+ _handler = new LanguageDeletedEventHandler(_languageServiceMock.Object, _settingServiceMock.Object,
+ _languageSettings);
+ }
+
+ [TestMethod()]
+ public async Task HandleTest()
+ {
+ //Assert
+ _languageServiceMock.Setup(x => x.GetAllLanguages(It.IsAny<bool>(), It.IsAny<string>())).Returns(Task.FromResult(
+ new List<Language> { new Language { Name = "English" }, new Language { Name = "Polish" } } as IList<Language>));
+
+ var notification = new Infrastructure.Events.EntityDeleted<Language>(new Language() { Id = "1" });
+ //Act
+ await _handler.Handle(notification, CancellationToken.None);
+
+ //Assert
+ _settingServiceMock.Verify(x => x.SaveSetting<LanguageSettings>(It.IsAny<LanguageSettings>(), ""), Times.Once());
+ }
+ }
+}
\ No newline at end of file
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 928b3f126..73744d3f4 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
@@ -28,6 +28,8 @@
<ProjectReference Include="..\..\Business\Grand.Business.Catalog\Grand.Business.Catalog.csproj" />
<ProjectReference Include="..\..\Business\Grand.Business.Common\Grand.Business.Common.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.Common.Tests/Services/Directory/CountryServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs
new file mode 100644
index 000000000..80a520c8e
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Directory/CountryServiceTests.cs
@@ -0,0 +1,214 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Data;
+using Grand.Domain.Directory;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Common.Services.Directory.Tests
+{
+ [TestClass()]
+ public class CountryServiceTests
+ {
+ private IRepository<Country> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+
+ private CountryService _countryService;
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<Country>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _countryService = new CountryService(_repository, _mediatorMock.Object, _cacheBase);
+ }
+
+ [TestMethod()]
+ public async Task GetAllCountriesTest()
+ {
+ //Arrange
+ await _countryService.InsertCountry(new Country() { Published = true });
+ await _countryService.InsertCountry(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetAllCountries();
+ //Assert
+ Assert.IsTrue(result.Count > 0);
+ }
+
+ [TestMethod()]
+ public async Task GetAllCountriesForBillingTest()
+ {
+ //Arrange
+ await _countryService.InsertCountry(new Country() { Published = true, AllowsBilling = true });
+ await _countryService.InsertCountry(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetAllCountriesForBilling();
+ //Assert
+ Assert.IsTrue(result.Count == 1);
+ }
+
+ [TestMethod()]
+ public async Task GetAllCountriesForShippingTest()
+ {
+ //Arrange
+ await _countryService.InsertCountry(new Country() { Published = true, AllowsShipping = true });
+ await _countryService.InsertCountry(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetAllCountriesForShipping();
+ //Assert
+ Assert.IsTrue(result.Count == 1);
+ }
+
+ [TestMethod()]
+ public async Task GetCountryByIdTest()
+ {
+ //Arrange
+ var country = new Country();
+ await _repository.InsertAsync(country);
+ await _repository.InsertAsync(new Country() { Published = true, AllowsShipping = true });
+ await _repository.InsertAsync(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetCountryById(country.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetCountriesByIdsTest()
+ {
+ //Arrange
+ var country = new Country();
+ await _repository.InsertAsync(country);
+ await _repository.InsertAsync(new Country() { Published = true, AllowsShipping = true });
+ await _repository.InsertAsync(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetCountriesByIds(new[] { country.Id });
+ //Assert
+ Assert.IsTrue(result.Count == 1);
+ }
+
+ [TestMethod()]
+ public async Task GetCountryByTwoLetterIsoCodeTest()
+ {
+ //Arrange
+ var country = new Country() { TwoLetterIsoCode = "US" };
+ await _repository.InsertAsync(country);
+ await _repository.InsertAsync(new Country() { Published = true, AllowsShipping = true });
+ await _repository.InsertAsync(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetCountryByTwoLetterIsoCode(country.TwoLetterIsoCode);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetCountryByThreeLetterIsoCodeTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA" };
+ await _repository.InsertAsync(country);
+ await _repository.InsertAsync(new Country() { Published = true, AllowsShipping = true });
+ await _repository.InsertAsync(new Country() { Published = true });
+ //Act
+ var result = await _countryService.GetCountryByThreeLetterIsoCode(country.ThreeLetterIsoCode);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task InsertCountryTest()
+ {
+ //Act
+ await _countryService.InsertCountry(new Country());
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task UpdateCountryTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ await _repository.InsertAsync(country);
+ //Act
+ country.Name = "USA";
+ await _countryService.UpdateCountry(country);
+ //Assert
+ Assert.AreEqual("USA", _repository.Table.FirstOrDefault(x => x.Id == country.Id).Name);
+ }
+
+ [TestMethod()]
+ public async Task DeleteCountryTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ await _repository.InsertAsync(country);
+ //Act
+ await _countryService.DeleteCountry(country);
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task GetStateProvincesByCountryIdTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ country.StateProvinces.Add(new StateProvince());
+ await _repository.InsertAsync(country);
+ //Act
+ var result = await _countryService.GetStateProvincesByCountryId(country.Id);
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task InsertStateProvinceTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ await _repository.InsertAsync(country);
+ //Act
+ await _countryService.InsertStateProvince(new StateProvince(), country.Id);
+ //Assert
+ var result = await _countryService.GetStateProvincesByCountryId(country.Id);
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task UpdateStateProvinceTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ await _repository.InsertAsync(country);
+ var state = new StateProvince();
+ await _countryService.InsertStateProvince(state, country.Id);
+ //Act
+ state.Name = "TEST";
+ await _countryService.UpdateStateProvince(state, country.Id);
+ //Assert
+ var result = await _countryService.GetStateProvincesByCountryId(country.Id);
+ Assert.IsTrue(result.Any());
+ Assert.AreEqual("TEST", result.FirstOrDefault().Name);
+ }
+
+ [TestMethod()]
+ public async Task DeleteStateProvinceTest()
+ {
+ //Arrange
+ var country = new Country() { ThreeLetterIsoCode = "USA", Name = "United States" };
+ await _repository.InsertAsync(country);
+ var state = new StateProvince();
+ await _countryService.InsertStateProvince(state, country.Id);
+ //Act
+ await _countryService.DeleteStateProvince(state, country.Id);
+ //Assert
+ var result = await _countryService.GetStateProvincesByCountryId(country.Id);
+ Assert.IsFalse(result.Any());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Directory/DateTimeServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Directory/DateTimeServiceTests.cs
new file mode 100644
index 000000000..8e52a60c8
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Directory/DateTimeServiceTests.cs
@@ -0,0 +1,54 @@
+using Grand.Domain.Directory;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Business.Common.Services.Directory.Tests
+{
+ [TestClass()]
+ public class DateTimeServiceTests
+ {
+ private DateTimeService _dateTimeService;
+ private DateTimeSettings _dateTimeSettings;
+ private TimeZoneInfo _timeZone;
+ [TestInitialize()]
+ public void TestInitialize()
+ {
+
+ _timeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.BaseUtcOffset.Hours == 1);
+
+ _dateTimeSettings = new DateTimeSettings();
+ _dateTimeSettings.DefaultStoreTimeZoneId = _timeZone.Id;
+ _dateTimeService = new DateTimeService(_dateTimeSettings);
+ }
+
+ [TestMethod()]
+ public void FindTimeZoneByIdTest()
+ {
+ var timezone = _dateTimeService.FindTimeZoneById(_dateTimeSettings.DefaultStoreTimeZoneId);
+ Assert.IsNotNull(timezone);
+ }
+
+ [TestMethod()]
+ public void GetSystemTimeZonesTest()
+ {
+ var timezones = _dateTimeService.GetSystemTimeZones();
+ Assert.IsTrue(timezones.Any());
+ }
+
+ [TestMethod()]
+ public void ConvertToUserTimeTest()
+ {
+ var usertime = _dateTimeService.ConvertToUserTime(new DateTime(01, 01, 01, 01, 00, 00, DateTimeKind.Utc));
+ Assert.IsNotNull(usertime);
+ Assert.AreEqual(new DateTime(01, 01, 01, 02, 00, 00), usertime);
+ }
+
+ [TestMethod()]
+ public void ConvertToUtcTimeTest()
+ {
+ var usertime = _dateTimeService.ConvertToUtcTime(new DateTime(01, 01, 01, 02, 00, 00), _timeZone);
+ Assert.IsNotNull(usertime);
+ Assert.AreEqual(new DateTime(01, 01, 01, 01, 00, 00), usertime);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Directory/GroupServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Directory/GroupServiceTests.cs
new file mode 100644
index 000000000..f49dc9f99
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Directory/GroupServiceTests.cs
@@ -0,0 +1,216 @@
+using Grand.Business.Common.Services.Directory;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Grand.Domain.Directory;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Common.Services.Directory.Tests
+{
+ [TestClass()]
+ public class GroupServiceTests
+ {
+ private IRepository<CustomerGroup> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+
+ private GroupService _groupService;
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<CustomerGroup>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _groupService = new GroupService(_repository, _cacheBase, _mediatorMock.Object);
+ }
+
+
+ [TestMethod()]
+ public async Task GetCustomerGroupByIdTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup();
+ await _repository.InsertAsync(customerGroup);
+ //Act
+ var result = await _groupService.GetCustomerGroupById(customerGroup.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetCustomerGroupBySystemNameTest()
+ {
+ //Arrange
+ const string systemName = "system";
+ var customerGroup = new CustomerGroup() { SystemName = systemName };
+ await _repository.InsertAsync(customerGroup);
+ //Act
+ var result = await _groupService.GetCustomerGroupBySystemName(systemName);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetAllCustomerGroupsTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { Active = true };
+ await _repository.InsertAsync(customerGroup);
+ //Act
+ var result = await _groupService.GetAllCustomerGroups();
+ //Assert
+ Assert.IsTrue(result.Any());
+ }
+
+ [TestMethod()]
+ public async Task InsertCustomerGroupTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup();
+ //Act
+ await _groupService.InsertCustomerGroup(customerGroup);
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task UpdateCustomerGroupTest()
+ {
+ //Arrange
+ const string systemName = "system";
+ var customerGroup = new CustomerGroup();
+ await _groupService.InsertCustomerGroup(customerGroup);
+ //Act
+ customerGroup.SystemName = systemName;
+ await _groupService.UpdateCustomerGroup(customerGroup);
+ //Assert
+ Assert.IsTrue(_repository.Table.FirstOrDefault(x=>x.Id == customerGroup.Id).SystemName == systemName);
+ }
+
+ [TestMethod()]
+ public async Task DeleteCustomerGroupTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup();
+ await _groupService.InsertCustomerGroup(customerGroup);
+ //Act
+ await _groupService.DeleteCustomerGroup(customerGroup);
+ //Assert
+ Assert.IsFalse(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task IsStaffTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.Staff, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsStaff(customer);
+ //Assert
+ Assert.IsTrue(result);
+
+ }
+
+ [TestMethod()]
+ public async Task IsAdminTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.Administrators, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsAdmin(customer);
+ //Assert
+ Assert.IsTrue(result);
+
+ }
+
+ [TestMethod()]
+ public async Task IsSalesManagerTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.SalesManager, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsSalesManager(customer);
+ //Assert
+ Assert.IsTrue(result);
+ }
+
+ [TestMethod()]
+ public async Task IsVendorTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.Vendors, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsVendor(customer);
+ //Assert
+ Assert.IsTrue(result);
+ }
+
+ [TestMethod()]
+ public async Task IsOwnerTest()
+ {
+ //Arrange
+ var customer = new Customer() { OwnerId = ""};
+ //Act
+ var result = await _groupService.IsOwner(customer);
+ //Assert
+ Assert.IsTrue(result);
+ }
+
+ [TestMethod()]
+ public async Task IsGuestTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.Guests, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsGuest(customer);
+ //Assert
+ Assert.IsTrue(result);
+ }
+
+ [TestMethod()]
+ public async Task IsRegisteredTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup() { IsSystem = true, SystemName = SystemCustomerGroupNames.Registered, Active = true };
+ await _groupService.InsertCustomerGroup(customerGroup);
+ var customer = new Customer();
+ customer.Groups.Add(customerGroup.Id);
+ //Act
+ var result = await _groupService.IsRegistered(customer);
+ //Assert
+ Assert.IsTrue(result);
+ }
+
+ [TestMethod()]
+ public async Task GetAllByIdsTest()
+ {
+ //Arrange
+ var customerGroup = new CustomerGroup();
+ await _repository.InsertAsync(customerGroup);
+ //Act
+ var result = await _groupService.GetAllByIds(new[] { customerGroup.Id });
+ //Assert
+ Assert.AreEqual(1, result.Count);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Directory/UserFieldServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Directory/UserFieldServiceTests.cs
new file mode 100644
index 000000000..62203bd46
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Directory/UserFieldServiceTests.cs
@@ -0,0 +1,78 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Grand.Business.Common.Services.Directory;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Grand.Domain.Data;
+using Grand.Domain.Common;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Customers;
+using MongoDB.Bson.Serialization.Conventions;
+
+namespace Grand.Business.Common.Services.Directory.Tests
+{
+ [TestClass()]
+ public class UserFieldServiceTests
+ {
+ private IRepository<UserFieldBaseEntity> _repository;
+ private IRepository<Customer> _repositoryCustomer;
+ private UserFieldService _userFieldService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ //global set an equivalent of [BsonIgnoreExtraElements] for every Domain Model
+ var cp = new ConventionPack {
+ new IgnoreExtraElementsConvention(true)
+ };
+ ConventionRegistry.Register("ApplicationConventions", cp, t => true);
+
+ _repository = new MongoDBRepositoryTest<UserFieldBaseEntity>();
+ _repositoryCustomer = new MongoDBRepositoryTest<Customer>();
+ _userFieldService = new UserFieldService(_repository);
+ }
+
+ [TestMethod()]
+ public async Task SaveFieldTest_Insert()
+ {
+ //Arrange
+ var customer = new Customer();
+ _repositoryCustomer.Insert(customer);
+ //Act
+ await _userFieldService.SaveField(customer, "Field", "Value");
+ //Assert
+ var userFields = _repositoryCustomer.Table.FirstOrDefault(x => x.Id == customer.Id).UserFields;
+ Assert.IsNotNull(userFields.FirstOrDefault(x => x.Key == "Field"));
+ }
+ [TestMethod()]
+ public async Task SaveFieldTest_Update()
+ {
+ //Arrange
+ var customer = new Customer();
+ customer.UserFields.Add(new UserField() { Key = "Field", Value = "empty", StoreId = "" });
+ _repositoryCustomer.Insert(customer);
+ //Act
+ await _userFieldService.SaveField(customer, "Field", "Value");
+ //Assert
+ var userFields = _repositoryCustomer.Table.FirstOrDefault(x => x.Id == customer.Id).UserFields;
+ Assert.IsNotNull(userFields.FirstOrDefault(x => x.Key == "Field"));
+ Assert.AreEqual("Value", userFields.FirstOrDefault(x => x.Key == "Field").Value);
+ }
+ [TestMethod()]
+ public async Task SaveFieldTest_Delete()
+ {
+ //Arrange
+ var customer = new Customer();
+ customer.UserFields.Add(new UserField() { Key = "Field", Value = "Value", StoreId = "" });
+ _repositoryCustomer.Insert(customer);
+ //Act
+ await _userFieldService.SaveField(customer, "Field", String.Empty);
+ //Assert
+ var userFields = _repositoryCustomer.Table.FirstOrDefault(x => x.Id == customer.Id).UserFields;
+ Assert.IsNull(userFields.FirstOrDefault(x => x.Key == "Field"));
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Localization/LanguageServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Localization/LanguageServiceTests.cs
new file mode 100644
index 000000000..9135a6135
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Localization/LanguageServiceTests.cs
@@ -0,0 +1,109 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Data;
+using Grand.Domain.Localization;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Tests.Caching;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Common.Services.Localization.Tests
+{
+ [TestClass()]
+ public class LanguageServiceTests
+ {
+ private IRepository<Language> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+
+ private LanguageService _languageService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<Language>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _languageService = new LanguageService(_cacheBase, _repository, _mediatorMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task GetAllLanguagesTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new Language() { Published = true });
+ await _repository.InsertAsync(new Language() { Published = true });
+ await _repository.InsertAsync(new Language() { Published = true });
+ //Act
+ var result = await _languageService.GetAllLanguages();
+ //Assert
+ Assert.AreEqual(3, result.Count);
+
+ }
+
+ [TestMethod()]
+ public async Task GetLanguageByIdTest()
+ {
+ //Arrange
+ var language = new Language() { Published = true, Id = "1" };
+ await _repository.InsertAsync(language);
+ await _repository.InsertAsync(new Language() { Published = true });
+ await _repository.InsertAsync(new Language() { Published = true });
+ //Act
+ var result = await _languageService.GetLanguageById(language.Id);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetLanguageByCodeTest()
+ {
+ //Arrange
+ var language = new Language() { Published = true, Id = "1", UniqueSeoCode = "en" };
+ await _repository.InsertAsync(language);
+ await _repository.InsertAsync(new Language() { Published = true });
+ await _repository.InsertAsync(new Language() { Published = true });
+ //Act
+ var result = await _languageService.GetLanguageByCode(language.UniqueSeoCode);
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task InsertLanguageTest()
+ {
+ //Arrange
+ var language = new Language() { Published = true, Id = "1", UniqueSeoCode = "en" };
+ //Act
+ await _languageService.InsertLanguage(language);
+ //Assert
+ Assert.IsNotNull(_repository.Table.FirstOrDefault(x => x.Id == language.Id));
+ }
+
+ [TestMethod()]
+ public async Task UpdateLanguageTest()
+ {
+ //Arrange
+ var language = new Language() { Published = true, Id = "1", UniqueSeoCode = "en" };
+ await _languageService.InsertLanguage(language);
+ //Act
+ language.FlagImageFileName = "en.png";
+ await _languageService.UpdateLanguage(language);
+ //Assert
+ Assert.IsTrue(_repository.Table.FirstOrDefault(x => x.Id == language.Id).FlagImageFileName == "en.png");
+ }
+
+ [TestMethod()]
+ public async Task DeleteLanguageTest()
+ {
+ //Arrange
+ var language = new Language() { Published = true, Id = "1", UniqueSeoCode = "en" };
+ await _languageService.InsertLanguage(language);
+ //Act
+ await _languageService.DeleteLanguage(language);
+ //Assert
+ Assert.IsNull(_repository.Table.FirstOrDefault(x => x.Id == language.Id));
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Localization/TranslationServiceTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Localization/TranslationServiceTests.cs
new file mode 100644
index 000000000..8f95c3372
--- /dev/null
+++ b/src/Tests/Grand.Business.Common.Tests/Services/Localization/TranslationServiceTests.cs
@@ -0,0 +1,173 @@
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Customers;
+using Grand.Domain.Data;
+using Grand.Domain.Localization;
+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.Localization.Tests
+{
+ [TestClass()]
+ public class TranslationServiceTests
+ {
+ private IRepository<TranslationResource> _repository;
+ private Mock<IMediator> _mediatorMock;
+ private MemoryCacheBase _cacheBase;
+ private Mock<IWorkContext> _workContextMock;
+
+ private TranslationService _translationService;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _repository = new MongoDBRepositoryTest<TranslationResource>();
+
+ _mediatorMock = new Mock<IMediator>();
+ _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);
+ _workContextMock = new Mock<IWorkContext>();
+ _workContextMock.Setup(c => c.CurrentStore).Returns(() => new Domain.Stores.Store());
+ _workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());
+ _workContextMock.Setup(c => c.WorkingLanguage).Returns(() => new Language() { Id = "1" });
+
+ _translationService = new TranslationService(_cacheBase, _workContextMock.Object, _repository, _mediatorMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task GetTranslateResourceByIdTest()
+ {
+ //Arrange
+ var translationResource = new TranslationResource();
+ await _repository.InsertAsync(translationResource);
+
+ //Act
+ var result = await _translationService.GetTranslateResourceById(translationResource.Id);
+
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetTranslateResourceByNameTest()
+ {
+ //Arrange
+ var translationResource = new TranslationResource() { Name = "name", LanguageId = "1" };
+ await _repository.InsertAsync(translationResource);
+
+ //Act
+ var result = await _translationService.GetTranslateResourceByName(translationResource.Name, translationResource.LanguageId);
+
+ //Assert
+ Assert.IsNotNull(result);
+ }
+
+ [TestMethod()]
+ public async Task GetAllResourcesTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new TranslationResource() { LanguageId = "1" });
+ await _repository.InsertAsync(new TranslationResource() { LanguageId = "1" });
+ await _repository.InsertAsync(new TranslationResource() { LanguageId = "2" });
+
+ //Act
+ var result = _translationService.GetAllResources("1");
+
+ //Assert
+ Assert.AreEqual(2, result.Count);
+ }
+
+ [TestMethod()]
+ public async Task InsertTranslateResourceTest()
+ {
+ //Arrange
+ var translationResource = new TranslationResource() { Name = "name", LanguageId = "1" };
+
+ //Act
+ await _translationService.InsertTranslateResource(translationResource);
+
+ //Assert
+ Assert.AreEqual(1, _repository.Table.Count());
+ }
+
+ [TestMethod()]
+ public async Task UpdateTranslateResourceTest()
+ {
+ //Arrange
+ var translationResource = new TranslationResource() { Name = "name", Value = "value", LanguageId = "1" };
+ await _translationService.InsertTranslateResource(translationResource);
+
+ //Act
+ translationResource.Name = "name2";
+ await _translationService.UpdateTranslateResource(translationResource);
+
+ //Assert
+ Assert.AreEqual(translationResource.Name, _repository.Table.FirstOrDefault(x=>x.Id == translationResource.Id).Name);
+ }
+
+ [TestMethod()]
+ public async Task DeleteTranslateResourceTest()
+ {
+ //Arrange
+ var translationResource = new TranslationResource() { Name = "name", Value = "value", LanguageId = "1" };
+ await _translationService.InsertTranslateResource(translationResource);
+
+ //Act
+ await _translationService.DeleteTranslateResource(translationResource);
+
+ //Assert
+ Assert.AreEqual(0, _repository.Table.Count());
+ }
+
+ [TestMethod()]
+ public async Task GetResourceTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new TranslationResource() { Name = "name1", Value = "value1", LanguageId = "1" });
+ await _repository.InsertAsync(new TranslationResource() { Name = "name2", Value = "value2", LanguageId = "1" });
+ await _repository.InsertAsync(new TranslationResource() { Name = "name3", Value = "value3", LanguageId = "2" });
+
+ //Act
+ var result = _translationService.GetResource("name1");
+
+ //Assert
+ Assert.AreEqual("value1", result);
+ }
+
+
+ [TestMethod()]
+ public async Task ExportResourcesToXmlTest()
+ {
+ //Arrange
+ await _repository.InsertAsync(new TranslationResource() { Name = "name1", Value = "value1", LanguageId = "1" });
+
+ //Act
+ var result = await _translationService.ExportResourcesToXml(new Language() { Id = "1", Name = "en"});
+
+ //Assert
+ Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-16\"?><Language Name=\"en\"><Resource Name=\"name1\" Area=\"Common\"><Value>value1</Value></Resource></Language>", result);
+ }
+
+ [TestMethod()]
+ public async Task ImportResourcesFromXmlTest()
+ {
+ //Act
+ await _translationService.ImportResourcesFromXml(new Language() { Id = "1", Name = "en" }, "<?xml version=\"1.0\" encoding=\"utf-16\"?><Language Name=\"en\"><Resource Name=\"name1\" Area=\"Common\"><Value>value1</Value></Resource></Language>");
+
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+
+ [TestMethod()]
+ public async Task ImportResourcesFromXmlInstallTest()
+ {
+ //Act
+ await _translationService.ImportResourcesFromXmlInstall(new Language() { Id = "1", Name = "en" }, "<?xml version=\"1.0\" encoding=\"utf-16\"?><Language Name=\"en\"><Resource Name=\"name1\" Area=\"Common\"><Value>value1</Value></Resource></Language>");
+
+ //Assert
+ Assert.IsTrue(_repository.Table.Any());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs b/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
index 300a84102..f1a864505 100644
--- a/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
+++ b/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
@@ -1,6 +1,7 @@
using Grand.Domain;
using Grand.Domain.Data;
using Grand.Domain.Data.Mongo;
+using MongoDB.Bson.Serialization.Conventions;
namespace Grand.Data.Tests.MongoDb
{
@@ -8,7 +9,14 @@ namespace Grand.Data.Tests.MongoDb
{
public MongoDBRepositoryTest():base(DriverTestConfiguration.Client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName))
{
+ var cp = new ConventionPack {
+ new IgnoreExtraElementsConvention(true)
+ };
+ ConventionRegistry.Register("ApplicationConventions", cp, t => true);
+
var client = DriverTestConfiguration.Client;
+
+
_database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
_database.DropCollection(typeof(T).Name);
_collection = _database.GetCollection<T>(typeof(T).Name);
diff --git a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheTest.cs b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheTest.cs
index ff838111c..202234cbd 100644
--- a/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheTest.cs
+++ b/src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheTest.cs
@@ -6,19 +6,14 @@ namespace Grand.Infrastructure.Tests.Caching
{
public static class MemoryCacheTest
{
- private static IMemoryCache _memoryCache;
public static IMemoryCache Get()
{
CommonHelper.CacheTimeMinutes = 1;
- if (_memoryCache == null)
- {
- var services = new ServiceCollection();
- services.AddMemoryCache();
- var serviceProvider = services.BuildServiceProvider();
+ var services = new ServiceCollection();
+ services.AddMemoryCache();
+ var serviceProvider = services.BuildServiceProvider();
- _memoryCache = serviceProvider.GetService<IMemoryCache>();
- }
- return _memoryCache;
+ return serviceProvider.GetService<IMemoryCache>();
}
}
}