|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using EasyPost.BetaFeatures.Parameters; |
| 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.BetaFeaturesTests.ModelsTests |
| 12 | +{ |
| 13 | + public class BatchTests : UnitTest |
| 14 | + { |
| 15 | + // NOTE: Due to an issue EasyVCR has with multiple exact matches of the same request in the same cassette, we have to split the add/remove overloads into separate unit tests. |
| 16 | + |
| 17 | + public BatchTests() : base("batch_with_parameters") |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + #region Tests |
| 22 | + |
| 23 | + #region Test CRUD Operations |
| 24 | + |
| 25 | + [Fact] |
| 26 | + [CrudOperations.Create] |
| 27 | + [Testing.Function] |
| 28 | + public async Task TestCreateWithShipments() |
| 29 | + { |
| 30 | + UseVCR("create_with_shipments"); |
| 31 | + |
| 32 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 33 | + |
| 34 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 35 | + |
| 36 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 37 | + |
| 38 | + BetaFeatures.Parameters.Batches.Create batchParameters = new BetaFeatures.Parameters.Batches.Create |
| 39 | + { |
| 40 | + Shipments = new List<IShipmentParameter> { shipment }, |
| 41 | + }; |
| 42 | + |
| 43 | + Batch batch = await Client.Batch.Create(batchParameters); |
| 44 | + |
| 45 | + Assert.IsType<Batch>(batch); |
| 46 | + Assert.Equal(1, batch.NumShipments); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + [CrudOperations.Create] |
| 51 | + [Testing.Function] |
| 52 | + public async Task TestCreateWithShipmentParameters() |
| 53 | + { |
| 54 | + UseVCR("create_with_shipment_parameters"); |
| 55 | + |
| 56 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 57 | + |
| 58 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 59 | + |
| 60 | + BetaFeatures.Parameters.Batches.Create batchParameters = new BetaFeatures.Parameters.Batches.Create |
| 61 | + { |
| 62 | + Shipments = new List<IShipmentParameter> { shipmentParameters }, |
| 63 | + }; |
| 64 | + |
| 65 | + Batch batch = await Client.Batch.Create(batchParameters); |
| 66 | + |
| 67 | + Assert.IsType<Batch>(batch); |
| 68 | + Assert.Equal(1, batch.NumShipments); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + [CrudOperations.Update] |
| 73 | + [Testing.Function] |
| 74 | + public async Task TestAddShipments() |
| 75 | + { |
| 76 | + UseVCR("add_shipments"); |
| 77 | + |
| 78 | + Batch batch = await Client.Batch.Create(); |
| 79 | + |
| 80 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 81 | + |
| 82 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 83 | + |
| 84 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 85 | + |
| 86 | + BetaFeatures.Parameters.Batches.AddShipments addShipmentsParameters = new BetaFeatures.Parameters.Batches.AddShipments |
| 87 | + { |
| 88 | + Shipments = new List<Shipment> { shipment }, |
| 89 | + }; |
| 90 | + |
| 91 | + batch = await batch.AddShipments(addShipmentsParameters); |
| 92 | + |
| 93 | + Assert.Equal(1, batch.NumShipments); |
| 94 | + } |
| 95 | + |
| 96 | + // Shipments have to exist before they can be added to a batch, so we don't need to test adding shipment via shipment creation parameters, since it's not possible. |
| 97 | + |
| 98 | + [Fact] |
| 99 | + [CrudOperations.Update] |
| 100 | + [Testing.Function] |
| 101 | + public async Task TestGenerateLabel() |
| 102 | + { |
| 103 | + UseVCR("generate_label"); |
| 104 | + |
| 105 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 106 | + |
| 107 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 108 | + |
| 109 | + BetaFeatures.Parameters.Batches.Create batchParameters = new BetaFeatures.Parameters.Batches.Create |
| 110 | + { |
| 111 | + Shipments = new List<IShipmentParameter> { shipmentParameters }, |
| 112 | + }; |
| 113 | + |
| 114 | + Batch batch = await Client.Batch.Create(batchParameters); |
| 115 | + |
| 116 | + if (IsRecording()) // Yes, this is needed. Otherwise, the API says we can't modify a batch while it's being created. |
| 117 | + { |
| 118 | + Thread.Sleep(10000); // Wait enough time to process |
| 119 | + } |
| 120 | + |
| 121 | + batch = await batch.Buy(); |
| 122 | + |
| 123 | + if (IsRecording()) |
| 124 | + { |
| 125 | + Thread.Sleep(10000); // Wait enough time to process |
| 126 | + } |
| 127 | + |
| 128 | + BetaFeatures.Parameters.Batches.GenerateLabel generateLabelParameters = new BetaFeatures.Parameters.Batches.GenerateLabel |
| 129 | + { |
| 130 | + FileFormat = "ZPL", |
| 131 | + }; |
| 132 | + |
| 133 | + await batch.GenerateLabel(generateLabelParameters); |
| 134 | + |
| 135 | + // We can't assert anything meaningful here because the label gets queued for generation and may not be immediately available |
| 136 | + Assert.IsType<Batch>(batch); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + [CrudOperations.Update] |
| 141 | + [Testing.Function] |
| 142 | + public async Task TestGenerateScanForm() |
| 143 | + { |
| 144 | + UseVCR("generate_scan_form"); |
| 145 | + |
| 146 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 147 | + |
| 148 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 149 | + |
| 150 | + BetaFeatures.Parameters.Batches.Create batchParameters = new BetaFeatures.Parameters.Batches.Create |
| 151 | + { |
| 152 | + Shipments = new List<IShipmentParameter> { shipmentParameters }, |
| 153 | + }; |
| 154 | + |
| 155 | + Batch batch = await Client.Batch.Create(batchParameters); |
| 156 | + |
| 157 | + if (IsRecording()) // Yes, this is needed. Otherwise, the API says we can't modify a batch while it's being created. |
| 158 | + { |
| 159 | + Thread.Sleep(10000); // Wait enough time to process |
| 160 | + } |
| 161 | + |
| 162 | + batch = await batch.Buy(); |
| 163 | + |
| 164 | + if (IsRecording()) |
| 165 | + { |
| 166 | + Thread.Sleep(10000); // Wait enough time to process |
| 167 | + } |
| 168 | + |
| 169 | + BetaFeatures.Parameters.Batches.GenerateScanForm generateScanFormParameters = new BetaFeatures.Parameters.Batches.GenerateScanForm |
| 170 | + { |
| 171 | + FileFormat = "ZPL", |
| 172 | + }; |
| 173 | + |
| 174 | + await batch.GenerateScanForm(generateScanFormParameters); |
| 175 | + |
| 176 | + // We can't assert anything meaningful here because the scanform gets queued for generation and may not be immediately available |
| 177 | + Assert.IsType<Batch>(batch); |
| 178 | + } |
| 179 | + |
| 180 | + [Fact] |
| 181 | + [CrudOperations.Update] |
| 182 | + [Testing.Function] |
| 183 | + public async Task TestRemoveShipments() |
| 184 | + { |
| 185 | + UseVCR("remove_shipments"); |
| 186 | + |
| 187 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 188 | + |
| 189 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 190 | + |
| 191 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 192 | + |
| 193 | + BetaFeatures.Parameters.Batches.Create batchParameters = new BetaFeatures.Parameters.Batches.Create |
| 194 | + { |
| 195 | + Shipments = new List<IShipmentParameter> { shipment }, |
| 196 | + }; |
| 197 | + |
| 198 | + Batch batch = await Client.Batch.Create(batchParameters); |
| 199 | + |
| 200 | + Assert.Equal(1, batch.NumShipments); |
| 201 | + |
| 202 | + BetaFeatures.Parameters.Batches.RemoveShipments removeShipmentsParameters = new BetaFeatures.Parameters.Batches.RemoveShipments |
| 203 | + { |
| 204 | + Shipments = new List<Shipment> { shipment }, |
| 205 | + }; |
| 206 | + |
| 207 | + batch = await batch.RemoveShipments(removeShipmentsParameters); |
| 208 | + |
| 209 | + Assert.Equal(0, batch.NumShipments); |
| 210 | + } |
| 211 | + |
| 212 | + // Shipments have to exist before they can be added/removed to a batch, so we don't need to test removing shipment via shipment creation parameters, since it's not possible. |
| 213 | + |
| 214 | + #endregion |
| 215 | + |
| 216 | + #endregion |
| 217 | + } |
| 218 | +} |
0 commit comments