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.

1.8 KB
Raw Blame
using Grand.Business.Common.Events;
using Grand.Data;
using Grand.Data.Tests.MongoDb;
using Grand.Domain.Catalog;
using Grand.Domain.Customers;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Events;
using Grand.Infrastructure.Tests.Caching;
using Grand.Mediator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Common.Tests.Events;

[TestClass]
public class GroupDeletedEventHandlerTests
{
    private MemoryCacheBase _cacheBase;
    private GroupDeletedEventHandler _handler;
    private IRepository<Product> _product;

    private IRepository<Customer> _repository;

    [TestInitialize]
    public void Init()
    {
        _repository = new MongoDBRepositoryTest<Customer>();
        _product = new MongoDBRepositoryTest<Product>();
        _cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), new Mock<IMediator>().Object,
            new CacheConfig { DefaultCacheTimeMinutes = 1 });

        _handler = new GroupDeletedEventHandler(_repository, _product, _cacheBase);
    }

    [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 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);
    }
}