From 8839019d66d497cb6cf9fc8f409aafabc026e94e Mon Sep 17 00:00:00 2001 From: Joshua Harms Date: Wed, 17 Jul 2024 23:30:01 -0500 Subject: [PATCH] Add missing test for CustomerService.ListOrdersForCustomerAsync --- .../Services/CustomerServiceTests.cs | 106 +++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/ShopifySharp.Tests/Services/CustomerServiceTests.cs b/ShopifySharp.Tests/Services/CustomerServiceTests.cs index 573116f6..df378cda 100644 --- a/ShopifySharp.Tests/Services/CustomerServiceTests.cs +++ b/ShopifySharp.Tests/Services/CustomerServiceTests.cs @@ -241,13 +241,39 @@ public async Task GetAccountActivationUrl_Customers() Assert.NotEmpty(url); Assert.Contains("account/activate", url); } + + [Fact] + public async Task ListOrdersForCustomer() + { + // Setup + var created = await Fixture.Create(); + var order = await Fixture.CreateOrder(created.Id.Value); + + // Act + var orders = await Fixture.Service.ListOrdersForCustomerAsync(created.Id.Value, new CustomerOrderListFilter + { + Status = "any" + }); + + // Assert + orders + .Should().NotBeNull() + .And.NotBeEmpty() + .And.AllSatisfy(x => + x.Id.Should().HaveValue() + .And.Be(order.Id)); + } } public class CustomerServiceTestsFixture : IAsyncLifetime { - public CustomerService Service { get; } = new CustomerService(Utils.MyShopifyUrl, Utils.AccessToken); + public CustomerService Service { get; } = new (Utils.MyShopifyUrl, Utils.AccessToken); - public List Created { get; } = new List(); + public OrderService OrderService { get; } = new (Utils.MyShopifyUrl, Utils.AccessToken); + + public List Created { get; } = []; + + public List CreatedOrders { get; } = []; public string FirstName => "John"; @@ -257,7 +283,9 @@ public class CustomerServiceTestsFixture : IAsyncLifetime public async Task InitializeAsync() { - Service.SetExecutionPolicy(new LeakyBucketExecutionPolicy()); + var policy = new LeakyBucketExecutionPolicy(); + Service.SetExecutionPolicy(policy); + OrderService.SetExecutionPolicy(policy); // Create one customer for use with count, list, get, etc. tests. await Create(); @@ -279,6 +307,21 @@ public async Task DisposeAsync() } } } + + foreach (var order in CreatedOrders) + { + try + { + await Service.DeleteAsync(order.Id.Value); + } + catch (ShopifyHttpException ex) + { + if (ex.HttpStatusCode != HttpStatusCode.NotFound) + { + Console.WriteLine($"Failed to delete created Order with id {order.Id.Value}. {ex.Message}"); + } + } + } } public async Task Create(bool skipAddToCreatedList = false, CustomerCreateOptions options = null) @@ -322,4 +365,61 @@ public async Task Create(bool skipAddToCreatedList = false, CustomerCr return obj; } + + public async Task CreateOrder(long customerId) + { + var obj = await OrderService.CreateAsync(new Order + { + CreatedAt = DateTime.UtcNow, + BillingAddress = new Address + { + Address1 = "123 4th Street", + City = "Minneapolis", + Province = "Minnesota", + ProvinceCode = "MN", + Zip = "55401", + Phone = "555-555-5555", + FirstName = "John", + LastName = "Doe", + Company = "Tomorrow Corporation", + Country = "United States", + CountryCode = "US", + Default = true, + }, + LineItems = new List() + { + new LineItem() + { + Name = "Test Line Item", + Title = "Test Line Item Title", + Quantity = 2, + Price = 5 + }, + new LineItem() + { + Name = "Test Line Item 2", + Title = "Test Line Item Title 2", + Quantity = 2, + Price = 5 + } + }, + FinancialStatus = "paid", + TotalPrice = 5.00m, + Note = Note, + Test = true, + Customer = new Customer + { + Id = customerId + } + }, new OrderCreateOptions + { + SendReceipt = false, + SendWebhooks = false, + SendFulfillmentReceipt = false, + }); + + CreatedOrders.Add(obj); + + return obj; + } }