|
| 1 | +using Grand.Business.Checkout.Queries.Handlers.Orders; |
| 2 | +using Grand.Business.Core.Queries.Checkout.Orders; |
| 3 | +using Grand.Data.Tests.MongoDb; |
| 4 | +using Grand.Domain.Customers; |
| 5 | +using Grand.Domain.Orders; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | + |
| 8 | +namespace Grand.Business.Checkout.Tests.Queries.Handlers.Orders; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public class GetRecurringPaymentsQueryHandlerTests |
| 12 | +{ |
| 13 | + private MongoDBRepositoryTest<Customer> _customerRepository; |
| 14 | + private GetRecurringPaymentsQueryHandler _handler; |
| 15 | + private MongoDBRepositoryTest<Order> _orderRepository; |
| 16 | + private MongoDBRepositoryTest<RecurringPayment> _repository; |
| 17 | + |
| 18 | + [TestInitialize] |
| 19 | + public void Init() |
| 20 | + { |
| 21 | + _repository = new MongoDBRepositoryTest<RecurringPayment>(); |
| 22 | + _customerRepository = new MongoDBRepositoryTest<Customer>(); |
| 23 | + _orderRepository = new MongoDBRepositoryTest<Order>(); |
| 24 | + _handler = new GetRecurringPaymentsQueryHandler(_repository, _customerRepository, _orderRepository); |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public async Task Handle_ByStore_ReturnsOnlyThatStore() |
| 29 | + { |
| 30 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp1", StoreId = "s1" }); |
| 31 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp2", StoreId = "s2" }); |
| 32 | + |
| 33 | + var result = await _handler.Handle(new GetRecurringPaymentsQuery { StoreId = "s1" }, CancellationToken.None); |
| 34 | + |
| 35 | + Assert.AreEqual(1, result.Count); |
| 36 | + Assert.AreEqual("rp1", result[0].Id); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public async Task Handle_ByStatus_ReturnsOnlyThatStatus() |
| 41 | + { |
| 42 | + await _repository.Collection.InsertOneAsync(new RecurringPayment |
| 43 | + { Id = "rp1", Status = RecurringPaymentStatus.PastDue }); |
| 44 | + await _repository.Collection.InsertOneAsync(new RecurringPayment |
| 45 | + { Id = "rp2", Status = RecurringPaymentStatus.Active }); |
| 46 | + |
| 47 | + var result = await _handler.Handle( |
| 48 | + new GetRecurringPaymentsQuery { Status = RecurringPaymentStatus.PastDue }, CancellationToken.None); |
| 49 | + |
| 50 | + Assert.AreEqual(1, result.Count); |
| 51 | + Assert.AreEqual("rp1", result[0].Id); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public async Task Handle_ByCustomerEmail_MatchesEveryAccountWithThatEmailExactly() |
| 56 | + { |
| 57 | + await _customerRepository.Collection.InsertOneAsync(new Customer { Id = "c1", Email = "jan@example.com", StoreId = "s1" }); |
| 58 | + await _customerRepository.Collection.InsertOneAsync(new Customer { Id = "c2", Email = "jan@example.com", StoreId = "s2" }); |
| 59 | + await _customerRepository.Collection.InsertOneAsync(new Customer { Id = "c3", Email = "xjan@example.com" }); |
| 60 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp1", CustomerId = "c1" }); |
| 61 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp2", CustomerId = "c2" }); |
| 62 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp3", CustomerId = "c3" }); |
| 63 | + |
| 64 | + var result = await _handler.Handle( |
| 65 | + new GetRecurringPaymentsQuery { CustomerEmail = " Jan@Example.com " }, CancellationToken.None); |
| 66 | + |
| 67 | + CollectionAssert.AreEquivalent(new[] { "rp1", "rp2" }, result.Select(x => x.Id).ToArray()); |
| 68 | + } |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public async Task Handle_ByUnknownCustomerEmail_ReturnsNothing() |
| 72 | + { |
| 73 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp1", CustomerId = "c1" }); |
| 74 | + |
| 75 | + var result = await _handler.Handle( |
| 76 | + new GetRecurringPaymentsQuery { CustomerEmail = "nobody@example.com" }, CancellationToken.None); |
| 77 | + |
| 78 | + Assert.AreEqual(0, result.Count); |
| 79 | + } |
| 80 | + |
| 81 | + [TestMethod] |
| 82 | + public async Task Handle_BySalesEmployee_ReturnsOnlyPaymentsOfTheirInitialOrders() |
| 83 | + { |
| 84 | + await _orderRepository.Collection.InsertOneAsync(new Order { Id = "o1", SeId = "se1", RecurringCycleNumber = 1 }); |
| 85 | + await _orderRepository.Collection.InsertOneAsync(new Order { Id = "o2", SeId = "se2", RecurringCycleNumber = 1 }); |
| 86 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp1", InitialOrderId = "o1" }); |
| 87 | + await _repository.Collection.InsertOneAsync(new RecurringPayment { Id = "rp2", InitialOrderId = "o2" }); |
| 88 | + |
| 89 | + var result = await _handler.Handle( |
| 90 | + new GetRecurringPaymentsQuery { SalesEmployeeId = "se1" }, CancellationToken.None); |
| 91 | + |
| 92 | + Assert.AreEqual(1, result.Count); |
| 93 | + Assert.AreEqual("rp1", result[0].Id); |
| 94 | + } |
| 95 | + |
| 96 | + [TestMethod] |
| 97 | + public async Task Handle_ReturnsNewestFirst() |
| 98 | + { |
| 99 | + await _repository.Collection.InsertOneAsync(new RecurringPayment |
| 100 | + { Id = "old", CreatedOnUtc = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc) }); |
| 101 | + await _repository.Collection.InsertOneAsync(new RecurringPayment |
| 102 | + { Id = "new", CreatedOnUtc = new DateTime(2026, 2, 1, 0, 0, 0, DateTimeKind.Utc) }); |
| 103 | + |
| 104 | + var result = await _handler.Handle(new GetRecurringPaymentsQuery(), CancellationToken.None); |
| 105 | + |
| 106 | + Assert.AreEqual("new", result[0].Id); |
| 107 | + Assert.AreEqual("old", result[1].Id); |
| 108 | + } |
| 109 | +} |
0 commit comments