Skip to content

Commit 701a4c1

Browse files
authored
[feat] Parameter sets, part 3, take 2 (#416)
- Add remaining parameter sets for classes A-H (Addresses - Events) - Add overload methods using Parameter objects for classes A-H - Add unit tests, fixtures needed, cassettes for overloaded methods - Minor cleanup, bug fixes of existing classes as needed - Lower coverage to 89%
1 parent 7b2d69c commit 701a4c1

File tree

108 files changed

+6069
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+6069
-189
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,12 @@ dotnet_diagnostic.IDE0025.severity = suggestion
558558
### Simplify null check
559559
### https://github.com/dotnet/roslyn/issues/65769#issuecomment-1337237203
560560
dotnet_diagnostic.IDE0270.severity = suggestion
561+
### Don't use redundant casts
562+
dotnet_diagnostic.IDE0004.severity = suggestion
563+
### Populate switch
564+
dotnet_diagnostic.IDE0010.severity = suggestion
565+
### "new" expression can be simplified
566+
dotnet_diagnostic.IDE0090.severity = suggestion
561567

562568
# Custom StyleCop (SA) rules
563569
[*.{cs,vb}]
@@ -588,6 +594,12 @@ dotnet_diagnostic.SA1309.severity = none
588594
dotnet_diagnostic.SA1515.severity = none
589595
### Use "gets" for properties docstrings
590596
dotnet_diagnostic.SA1623.severity = none
597+
### Don't omit braces
598+
dotnet_diagnostic.SA1503.severity = none
599+
### Type constraints should be on their own line
600+
dotnet_diagnostic.SA1127.severity = none
601+
### Single lint comments should not be followed by blank line
602+
dotnet_diagnostic.SA1512.severity = none
591603

592604
# Custom quality (CA) rules
593605
[*.{cs,vb}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using EasyPost.Tests._Utilities;
2+
3+
namespace EasyPost.Tests.BetaFeaturesTests.ModelsTests
4+
{
5+
public class AddressTests : UnitTest
6+
{
7+
public AddressTests() : base("address_with_parameters")
8+
{
9+
}
10+
11+
#region Tests
12+
13+
#region Test CRUD Operations
14+
15+
16+
#endregion
17+
18+
#endregion
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using EasyPost.Models.API;
4+
using EasyPost.Tests._Utilities;
5+
using EasyPost.Tests._Utilities.Attributes;
6+
using EasyPost.Utilities.Internal.Attributes;
7+
using Xunit;
8+
9+
namespace EasyPost.Tests.BetaFeaturesTests.ModelsTests
10+
{
11+
public class CarrierAccountTests : UnitTest
12+
{
13+
public CarrierAccountTests() : base("carrier_account_with_parameters", TestUtils.ApiKey.Production) =>
14+
CleanupFunction = async id =>
15+
{
16+
try
17+
{
18+
CarrierAccount retrievedCarrierAccount = await Client.CarrierAccount.Retrieve(id);
19+
await retrievedCarrierAccount.Delete();
20+
return true;
21+
}
22+
catch
23+
{
24+
// trying to delete something that doesn't exist, pass
25+
return false;
26+
}
27+
};
28+
29+
#region Tests
30+
31+
#region Test CRUD Operations
32+
33+
[Fact]
34+
[CrudOperations.Update]
35+
[Testing.Function]
36+
public async Task TestUpdate()
37+
{
38+
UseVCR("update");
39+
40+
Dictionary<string, object> data = Fixtures.BasicCarrierAccount;
41+
42+
BetaFeatures.Parameters.CarrierAccounts.Create createParameters = Fixtures.Parameters.CarrierAccounts.Create(data);
43+
44+
CarrierAccount carrierAccount = await Client.CarrierAccount.Create(createParameters);
45+
CleanUpAfterTest(carrierAccount.Id);
46+
47+
const string testDescription = "my custom description";
48+
49+
BetaFeatures.Parameters.CarrierAccounts.Update updateParameters = new BetaFeatures.Parameters.CarrierAccounts.Update
50+
{
51+
Description = testDescription,
52+
};
53+
54+
carrierAccount = await carrierAccount.Update(updateParameters);
55+
56+
Assert.IsType<CarrierAccount>(carrierAccount);
57+
Assert.StartsWith("ca_", carrierAccount.Id);
58+
Assert.Equal(testDescription, carrierAccount.Description);
59+
}
60+
61+
#endregion
62+
63+
#endregion
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using EasyPost.Models.API;
4+
using EasyPost.Tests._Utilities;
5+
using EasyPost.Tests._Utilities.Attributes;
6+
using EasyPost.Utilities.Internal.Attributes;
7+
using Xunit;
8+
9+
namespace EasyPost.Tests.BetaFeaturesTests.ModelsTests
10+
{
11+
public class EndShipperTests : UnitTest
12+
{
13+
public EndShipperTests() : base("end_shipper_with_parameters")
14+
{
15+
}
16+
17+
#region Tests
18+
19+
#region Test CRUD Operations
20+
21+
[Fact]
22+
[CrudOperations.Update]
23+
[Testing.Function]
24+
public async Task TestUpdate()
25+
{
26+
UseVCR("update");
27+
28+
Dictionary<string, object> data = Fixtures.CaAddress1;
29+
30+
BetaFeatures.Parameters.EndShippers.Create createParameters = Fixtures.Parameters.EndShippers.Create(data);
31+
32+
EndShipper endShipper = await Client.EndShipper.Create(createParameters);
33+
34+
const string testName = "NEW NAME";
35+
36+
// Updating an EndShipper requires all the original data to be sent back + the updated data
37+
BetaFeatures.Parameters.EndShippers.Update updateParameters = new BetaFeatures.Parameters.EndShippers.Update
38+
{
39+
Name = testName,
40+
City = createParameters.City,
41+
Company = createParameters.Company,
42+
Country = createParameters.Country,
43+
Email = createParameters.Email,
44+
Phone = createParameters.Phone,
45+
State = createParameters.State,
46+
Street1 = createParameters.Street1,
47+
Street2 = createParameters.Street2,
48+
Zip = createParameters.Zip,
49+
};
50+
51+
endShipper = await endShipper.Update(updateParameters);
52+
53+
Assert.IsType<EndShipper>(endShipper);
54+
Assert.StartsWith("es_", endShipper.Id);
55+
Assert.Equal(testName, endShipper.Name);
56+
}
57+
58+
#endregion
59+
60+
#endregion
61+
}
62+
}

0 commit comments

Comments
 (0)