Skip to content

Commit 75edf91

Browse files
committed
feat: add initial tests
1 parent 6ff6c7f commit 75edf91

12 files changed

+1695
-9
lines changed

.genignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generate will overwrite this file, removing the tests project
2+
Shippo.sln

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bin/
33
debug/
44
.speakeasy/temp/
55
build/
6+
ShippoTests/TestResults/

Makefile

+18-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ CONFIGURATION ?= Debug
2121

2222
BUILD := $(DOTNET) build --nologo --no-restore -c ${CONFIGURATION}
2323
RESTORE := $(DOTNET) restore --nologo
24+
TEST := $(DOTNET) test --nologo --no-build -c ${CONFIGURATION}
2425

2526
build_dir := build
2627
framework := $(shell $(SED) -nr "s|^[[:blank:]]*<TargetFramework>(.*)</TargetFramework>[[:blank:]]*$$|\1|p" Shippo/Shippo.csproj)
2728

2829
cs_files := $(shell find . -name "*.cs" -not -path "*/obj/*")
2930
csproj_files := $(wildcard */*.csproj)
30-
dll := Shippo/bin/${CONFIGURATION}/${framework}/Shippo.dll
31+
dlls := \
32+
Shippo/bin/${CONFIGURATION}/${framework}/Shippo.dll \
33+
ShippoTests/bin/${CONFIGURATION}/${framework}/ShippoTests.dll
3134
packages_lock_files := $(wildcard */packages.lock.json)
32-
project_assets_files := Shippo/obj/project.assets.json
35+
project_assets_files := \
36+
Shippo/obj/project.assets.json \
37+
ShippoTests/obj/project.assets.json
3338
source_files := \
3439
${cs_files} \
3540
${csproj_files} \
@@ -45,23 +50,31 @@ ${project_assets_files}: ${csproj_files} ${packages_lock_files}
4550
$(RESTORE) --locked-mode
4651
@touch -c ${project_assets_files}
4752

48-
${dll}: ${packages_lock_files} ${project_assets_files} ${source_files}
53+
${dlls}: ${packages_lock_files} ${project_assets_files} ${source_files}
4954
$(BUILD)
50-
@touch -c ${dll}
55+
@touch -c ${dlls}
5156

5257

5358
.PHONY: build
54-
build: ${dll} ## build the library dll (default)
59+
build: ${dlls} ## build the library and tests dll's (default)
5560

5661
.PHONY: clean
5762
clean: ## remove all build artifacts and temp directories/files
5863
rm -fr Shippo/bin Shippo/obj
64+
rm -fr ShippoTests/bin ShippoTests/obj ShippoTests/TestResults
5965
rm -fr .speakeasy/temp
6066
rm -fr ${build_dir}
6167

6268
.PHONY: update-lock-files
6369
update-lock-files: ${packages_lock_files} ## update lock files
6470

71+
.PHONY: test
72+
test: build ## run tests
73+
ifndef SHIPPO_TOKEN
74+
@echo "Note: some tests require a valid SHIPPO_TOKEN-- \`export SHIPPO_TOKEN=<API_TOKEN>\`"
75+
endif
76+
$(TEST)
77+
6578

6679
api_spec := ${build_dir}/public-api.yaml
6780

Shippo.sln

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
32
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shippo", "Shippo\Shippo.csproj", "{F0CE92B5-F3CC-45A2-AA83-118C38724EB1}"
43
EndProject
5-
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShippoTests", "ShippoTests\ShippoTests.csproj", "{8F6635ED-6B7B-4FFD-AC8C-9DAB6E8599A1}"
5+
EndProject
66
Global
77
GlobalSection(SolutionConfigurationPlatforms) = preSolution
88
Debug|Any CPU = Debug|Any CPU
@@ -13,5 +13,9 @@ Global
1313
{F0CE92B5-F3CC-45A2-AA83-118C38724EB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
1414
{F0CE92B5-F3CC-45A2-AA83-118C38724EB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
1515
{F0CE92B5-F3CC-45A2-AA83-118C38724EB1}.Release|Any CPU.Build.0 = Release|Any CPU
16+
{8F6635ED-6B7B-4FFD-AC8C-9DAB6E8599A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{8F6635ED-6B7B-4FFD-AC8C-9DAB6E8599A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{8F6635ED-6B7B-4FFD-AC8C-9DAB6E8599A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{8F6635ED-6B7B-4FFD-AC8C-9DAB6E8599A1}.Release|Any CPU.Build.0 = Release|Any CPU
1620
EndGlobalSection
17-
EndGlobal
21+
EndGlobal

ShippoTests/Integration/Fixtures.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace ShippoTests.Integration;
2+
3+
public class SDKFixture
4+
{
5+
public ShippoSDK SDK { get; private set; }
6+
7+
public SDKFixture()
8+
{
9+
SDK = new ShippoSDK(
10+
apiKeyHeader: Environment.GetEnvironmentVariable("SHIPPO_TOKEN"),
11+
serverUrl: Environment.GetEnvironmentVariable("SHIPPO_URL"),
12+
shippoApiVersion: Environment.GetEnvironmentVariable("SHIPPO_API_VERSION")
13+
);
14+
}
15+
}
16+
17+
[CollectionDefinition("Integration")]
18+
public class TestsCollection : ICollectionFixture<SDKFixture>
19+
{
20+
}

ShippoTests/Integration/Helpers.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace ShippoTests.Integration;
2+
3+
public class Helpers
4+
{
5+
public async static Task<CarrierAccountWithExtraInfo?> GetCarrierAccount(
6+
ShippoSDK sdk, CarriersEnum carrier)
7+
{
8+
return (await GetCarrierAccounts(sdk, carrier))?[0];
9+
}
10+
11+
public async static Task<List<CarrierAccountWithExtraInfo>?> GetCarrierAccounts(
12+
ShippoSDK sdk, CarriersEnum carrier)
13+
{
14+
return (await sdk.CarrierAccounts.ListAsync(
15+
new ListCarrierAccountsRequest()
16+
{
17+
Carrier = carrier
18+
}
19+
)).Results;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
namespace ShippoTests.Integration;
2+
3+
public class SetupFixture
4+
{
5+
public SetupFixture()
6+
{
7+
Initialization = DeleteAllServiceGroups();
8+
}
9+
10+
public Task Initialization { get; private set; }
11+
12+
async Task DeleteAllServiceGroups()
13+
{
14+
ShippoSDK sdk = new SDKFixture().SDK;
15+
List<ServiceGroup> serviceGroups = await sdk.ServiceGroups.ListAsync();
16+
await Task.WhenAll(
17+
serviceGroups.Select(
18+
async serviceGroup => await
19+
sdk.ServiceGroups.DeleteAsync(serviceGroup.ObjectId)
20+
).ToList()
21+
);
22+
}
23+
}
24+
25+
[Collection("Integration")]
26+
public class RatesAtCheckout : IClassFixture<SetupFixture>
27+
{
28+
SDKFixture sdkFixture;
29+
SetupFixture setupFixture;
30+
31+
public RatesAtCheckout(SDKFixture sdkFixture, SetupFixture setupFixture)
32+
{
33+
this.sdkFixture = sdkFixture;
34+
this.setupFixture = setupFixture;
35+
}
36+
37+
[Fact]
38+
public async void Test()
39+
{
40+
await setupFixture.Initialization;
41+
42+
CarrierAccountWithExtraInfo carrierAccount = (
43+
await Helpers.GetCarrierAccount(sdkFixture.SDK, CarriersEnum.Ups)
44+
)!;
45+
carrierAccount.Should().NotBeNull();
46+
string accountId = carrierAccount.ObjectId!;
47+
accountId.Should().NotBeNullOrEmpty();
48+
49+
List<ServiceLevelUPSEnum> availableServiceLevels = new List<ServiceLevelUPSEnum>
50+
{
51+
ServiceLevelUPSEnum.UpsGround, ServiceLevelUPSEnum.UpsNextDayAirSaver
52+
};
53+
54+
List<ServiceGroupAccountAndServiceLevel> serviceLevels =
55+
availableServiceLevels.Select(
56+
availableServiceLevel => new ServiceGroupAccountAndServiceLevel()
57+
{
58+
AccountObjectId = accountId,
59+
ServiceLevelToken = availableServiceLevel.Value(),
60+
}
61+
).ToList();
62+
63+
ServiceGroup serviceGroup = await sdkFixture.SDK.ServiceGroups.CreateAsync(
64+
new ServiceGroupCreateRequest()
65+
{
66+
Name = "UPS shipping",
67+
Description = "UPS shipping options",
68+
FlatRate = "5",
69+
FlatRateCurrency = "USD",
70+
Type = ServiceGroupTypeEnum.LiveRate,
71+
ServiceLevels = serviceLevels,
72+
}
73+
);
74+
serviceGroup.Should().NotBeNull();
75+
76+
LiveRatePaginatedList liveRates =
77+
await sdkFixture.SDK.RatesAtCheckout.CreateAsync(
78+
new LiveRateCreateRequest()
79+
{
80+
AddressFrom =
81+
LiveRateCreateRequestAddressFrom.CreateAddressCompleteCreateRequest(
82+
new AddressCompleteCreateRequest()
83+
{
84+
Name = "Rachael",
85+
Street1 = "1092 Indian Summer Ct",
86+
City = "San Jose",
87+
State = "CA",
88+
Zip = "95122",
89+
Country = "US",
90+
Phone = "4159876543",
91+
Email = "[email protected]",
92+
}
93+
),
94+
AddressTo =
95+
LiveRateCreateRequestAddressTo.CreateAddressCompleteCreateRequest(
96+
new AddressCompleteCreateRequest()
97+
{
98+
Name = "Mr Hippo",
99+
Street1 = "965 Mission St #572",
100+
City = "San Francisco",
101+
State = "CA",
102+
Zip = "94103",
103+
Country = "US",
104+
Phone = "4151234567",
105+
Email = "[email protected]",
106+
}
107+
),
108+
LineItems = new List<LineItem>()
109+
{
110+
new LineItem()
111+
{
112+
Quantity = 1,
113+
TotalPrice = "12.00",
114+
Currency = "USD",
115+
Weight = "1.0",
116+
WeightUnit = WeightUnitEnum.Lb,
117+
Title = "Hippo Snax",
118+
ManufactureCountry = "US",
119+
Sku = "HM-123",
120+
}
121+
},
122+
Parcel = LiveRateCreateRequestParcel.CreateParcel(
123+
new Parcel()
124+
{
125+
Length = "10",
126+
Width = "15",
127+
Height = "10",
128+
DistanceUnit = DistanceUnitEnum.In,
129+
Weight = "1",
130+
MassUnit = WeightUnitEnum.Lb,
131+
}
132+
),
133+
}
134+
);
135+
liveRates.Should().NotBeNull();
136+
liveRates.Results.Should().NotBeNullOrEmpty();
137+
foreach (LiveRate liveRate in liveRates.Results!)
138+
{
139+
liveRate.Title.Should().Be(serviceGroup.Name);
140+
}
141+
}
142+
}
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
namespace ShippoTests.Integration;
2+
3+
using Parcels = Shippo.Models.Components.Parcels;
4+
5+
[Collection("Integration")]
6+
public class Shipments
7+
{
8+
SDKFixture sdkFixture;
9+
10+
public Shipments(SDKFixture sdkFixture)
11+
{
12+
this.sdkFixture = sdkFixture;
13+
}
14+
15+
[Fact]
16+
public async void TestInternationalLabel()
17+
{
18+
var addressFromTask = sdkFixture.SDK.Addresses.CreateAsync(
19+
new AddressCreateRequest()
20+
{
21+
Name = "Sarah",
22+
Company = "We Sell Guitars",
23+
Street1 = "215 Clayton St.",
24+
City = "San Francisco",
25+
State = "CA",
26+
Zip = "94117",
27+
Country = "US",
28+
Phone = "+1 555 341 9393",
29+
Email = "[email protected]",
30+
IsResidential = false,
31+
Metadata = "We Sell Guitars HQ",
32+
}
33+
);
34+
var customsDeclarationTask = sdkFixture.SDK.CustomsDeclarations.CreateAsync(
35+
new CustomsDeclarationCreateRequest()
36+
{
37+
ContentsType = CustomsDeclarationContentsTypeEnum.Merchandise,
38+
NonDeliveryOption = CustomsDeclarationNonDeliveryOptionEnum.Return,
39+
Certify = true,
40+
CertifySigner = "Tom Marks",
41+
Incoterm = CustomsDeclarationIncotermEnum.Ddp,
42+
Items = new List<CustomsItemCreateRequest>()
43+
{
44+
new CustomsItemCreateRequest()
45+
{
46+
Description = "Guitar Pedal",
47+
Quantity = 1,
48+
NetWeight = "5",
49+
MassUnit = WeightUnitEnum.Lb,
50+
ValueAmount = "200",
51+
ValueCurrency = "USD",
52+
OriginCountry = "US",
53+
}
54+
}
55+
}
56+
);
57+
await Task.WhenAll(addressFromTask, customsDeclarationTask);
58+
Address addressFrom = await addressFromTask;
59+
CustomsDeclaration customsDeclaration = await customsDeclarationTask;
60+
addressFrom.ObjectId.Should().NotBeNullOrEmpty();
61+
customsDeclaration.ObjectId.Should().NotBeNullOrEmpty();
62+
63+
Shipment shipment = await sdkFixture.SDK.Shipments.CreateAsync(
64+
new ShipmentCreateRequest()
65+
{
66+
AddressFrom = AddressFrom.CreateStr(addressFrom.ObjectId!),
67+
AddressTo = AddressTo.CreateAddressCreateRequest(
68+
new AddressCreateRequest()
69+
{
70+
Name = "Tom Marks",
71+
Street1 = "159 Broadhurst Gardens",
72+
City = "London",
73+
Zip = "NW6 3AU",
74+
Country = "GB",
75+
Phone = "4159876543",
76+
Email = "[email protected]",
77+
}
78+
),
79+
Parcels = new List<Parcels>()
80+
{
81+
Parcels.CreateParcelCreateRequest(
82+
new ParcelCreateRequest()
83+
{
84+
Weight = "5",
85+
Length = "10",
86+
Width = "5",
87+
Height = "4",
88+
DistanceUnit = DistanceUnitEnum.In,
89+
MassUnit = WeightUnitEnum.Lb,
90+
}
91+
)
92+
},
93+
CustomsDeclaration = ShipmentCreateRequestCustomsDeclaration.CreateStr(
94+
customsDeclaration.ObjectId!),
95+
Extra = new ShipmentExtra()
96+
{
97+
Insurance = new Insurance()
98+
{
99+
Amount = "200",
100+
Currency = "USD",
101+
Content = "guitar pedal",
102+
}
103+
},
104+
}
105+
);
106+
shipment.Should().NotBeNull();
107+
}
108+
}

0 commit comments

Comments
 (0)