|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using EasyPost.Exceptions.General; |
| 5 | +using EasyPost.Models.API; |
| 6 | +using EasyPost.Tests._Utilities; |
| 7 | +using EasyPost.Tests._Utilities.Attributes; |
| 8 | +using EasyPost.Utilities.Internal.Attributes; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace EasyPost.Tests.ServicesTests.WithParameters |
| 12 | +{ |
| 13 | + public class SmartRateServiceTests : UnitTest |
| 14 | + { |
| 15 | + public SmartRateServiceTests() : base("smartrate_service_with_parameters") |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + #region Tests |
| 20 | + |
| 21 | + #region Test CRUD Operations |
| 22 | + |
| 23 | + [Fact] |
| 24 | + [CrudOperations.Read] |
| 25 | + [Testing.Function] |
| 26 | + public async Task TestEstimateDeliveryDate() |
| 27 | + { |
| 28 | + UseVCR("estimate_delivery_date"); |
| 29 | + |
| 30 | + Dictionary<string, object> address1Data = Fixtures.CaAddress1; |
| 31 | + Dictionary<string, object> address2Data = Fixtures.CaAddress2; |
| 32 | + Parameters.Address.Create address1Parameters = Fixtures.Parameters.Addresses.Create(address1Data); |
| 33 | + Parameters.Address.Create address2Parameters = Fixtures.Parameters.Addresses.Create(address2Data); |
| 34 | + |
| 35 | + Parameters.SmartRate.EstimateDeliveryDateForZipPair estimateDeliveryDateForZipPairParameters = new() |
| 36 | + { |
| 37 | + FromZip = address1Parameters.Zip, |
| 38 | + ToZip = address2Parameters.Zip, |
| 39 | + PlannedShipDate = Fixtures.PlannedShipDate, |
| 40 | + Carriers = ["USPS", "FedEx", "UPS", "DHL"], |
| 41 | + }; |
| 42 | + |
| 43 | + EstimateDeliveryDateForZipPairResult results = await Client.SmartRate.EstimateDeliveryDate(estimateDeliveryDateForZipPairParameters); |
| 44 | + |
| 45 | + Assert.Equal(results.FromZip, estimateDeliveryDateForZipPairParameters.FromZip); |
| 46 | + Assert.Equal(results.ToZip, estimateDeliveryDateForZipPairParameters.ToZip); |
| 47 | + Assert.Equal(results.PlannedShipDate, estimateDeliveryDateForZipPairParameters.PlannedShipDate); |
| 48 | + Assert.NotNull(results.Results); |
| 49 | + Assert.NotEmpty(results.Results); |
| 50 | + foreach (var estimate in results.Results) |
| 51 | + { |
| 52 | + Assert.NotNull(estimate.Carrier); |
| 53 | + Assert.NotNull(estimate.Service); |
| 54 | + Assert.NotNull(estimate.TimeInTransitDetails); |
| 55 | + Assert.NotNull(estimate.TimeInTransitDetails.DaysInTransit); |
| 56 | + Assert.NotNull(estimate.TimeInTransitDetails.DaysInTransit.Percentile75); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + [CrudOperations.Read] |
| 62 | + [Testing.Function] |
| 63 | + public async Task TestRecommendShipDate() |
| 64 | + { |
| 65 | + UseVCR("recommend_ship_date"); |
| 66 | + |
| 67 | + Dictionary<string, object> address1Data = Fixtures.CaAddress1; |
| 68 | + Dictionary<string, object> address2Data = Fixtures.CaAddress2; |
| 69 | + Parameters.Address.Create address1Parameters = Fixtures.Parameters.Addresses.Create(address1Data); |
| 70 | + Parameters.Address.Create address2Parameters = Fixtures.Parameters.Addresses.Create(address2Data); |
| 71 | + |
| 72 | + Parameters.SmartRate.RecommendShipDateForZipPair recommendShipDateForZipPairParameters = new() |
| 73 | + { |
| 74 | + FromZip = address1Parameters.Zip, |
| 75 | + ToZip = address2Parameters.Zip, |
| 76 | + DesiredDeliveryDate = Fixtures.DesiredDeliveryDate, |
| 77 | + Carriers = ["USPS", "FedEx", "UPS", "DHL"], |
| 78 | + }; |
| 79 | + |
| 80 | + RecommendShipDateForZipPairResult results = await Client.SmartRate.RecommendShipDate(recommendShipDateForZipPairParameters); |
| 81 | + |
| 82 | + Assert.Equal(results.FromZip, recommendShipDateForZipPairParameters.FromZip); |
| 83 | + Assert.Equal(results.ToZip, recommendShipDateForZipPairParameters.ToZip); |
| 84 | + Assert.Equal(results.DesiredDeliveryDate, recommendShipDateForZipPairParameters.DesiredDeliveryDate); |
| 85 | + Assert.NotNull(results.Results); |
| 86 | + Assert.NotEmpty(results.Results); |
| 87 | + foreach (var estimate in results.Results) |
| 88 | + { |
| 89 | + Assert.NotNull(estimate.Carrier); |
| 90 | + Assert.NotNull(estimate.Service); |
| 91 | + Assert.NotNull(estimate.EasyPostTimeInTransitData); |
| 92 | + Assert.NotNull(estimate.EasyPostTimeInTransitData.DaysInTransit); |
| 93 | + Assert.NotNull(estimate.EasyPostTimeInTransitData.DaysInTransit.Percentile75); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + #endregion |
| 98 | + |
| 99 | + [Fact] |
| 100 | + [Testing.Function] |
| 101 | + public async Task TestLowestSmartRateFiltering() |
| 102 | + { |
| 103 | + /*** |
| 104 | + * Mock rates since these can change from the API and we want to test the local filtering logic, not the API call. |
| 105 | + * The API call is tested in <see cref="TestGetSmartRates"/> |
| 106 | + */ |
| 107 | + List<SmartRate> smartRates = |
| 108 | + [ |
| 109 | + new SmartRate |
| 110 | + { |
| 111 | + Service = "Priority", |
| 112 | + Carrier = "USPS", |
| 113 | + Rate = 1.00, // this rate is cheaper but doesn't meet the filters |
| 114 | + TimeInTransit = new TimeInTransit |
| 115 | + { |
| 116 | + Percentile90 = 3, |
| 117 | + }, |
| 118 | + }, |
| 119 | + |
| 120 | + new SmartRate |
| 121 | + { |
| 122 | + Service = "First", |
| 123 | + Carrier = "USPS", |
| 124 | + Rate = 6.07, |
| 125 | + TimeInTransit = new TimeInTransit |
| 126 | + { |
| 127 | + Percentile90 = 2, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + ]; |
| 132 | + |
| 133 | + // test lowest SmartRate with valid filters |
| 134 | + SmartRate lowestSmartRate = Utilities.Rates.GetLowestSmartRate(smartRates, 2, SmartRateAccuracy.Percentile90); |
| 135 | + Assert.Equal("First", lowestSmartRate.Service); |
| 136 | + Assert.Equal(6.07, lowestSmartRate.Rate); |
| 137 | + Assert.Equal("USPS", lowestSmartRate.Carrier); |
| 138 | + |
| 139 | + // test lowest SmartRate with invalid filters (should error due to strict delivery_days) |
| 140 | + await Assert.ThrowsAsync<FilteringError>(() => Task.FromResult(Utilities.Rates.GetLowestSmartRate(smartRates, 0, SmartRateAccuracy.Percentile90))); |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + } |
| 145 | +} |
0 commit comments