Skip to content

Commit dc17ea9

Browse files
committed
doc adjustments and test coverage
1 parent 2c8c6ab commit dc17ea9

File tree

4 files changed

+220
-212
lines changed

4 files changed

+220
-212
lines changed

docs/create_label_from_rate_id_example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def create_label_from_rate_id_demo():
5252
rate_id="se-799373193", params=params
5353
)
5454
print("::SUCCESS::")
55-
p.pprint(result)
55+
print(result)
5656
except ShipEngineError as err:
5757
print("::ERROR::")
5858
print(err.to_json())
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""Testing the create label from rate ID functionality in the ShipEngine SDK."""
2+
import json
3+
import unittest
4+
import urllib.parse as urlparse
5+
6+
import responses
7+
8+
from shipengine.enums import BaseURL
9+
from tests.util import stub_shipengine_instance
10+
11+
12+
class TestCreateLabelFromRateID(unittest.TestCase):
13+
@responses.activate
14+
def test_create_label_from_rate_id_(self) -> None:
15+
"""Test purchase label from rate_id."""
16+
responses.add(
17+
**{
18+
"method": responses.POST,
19+
"url": urlparse.urljoin(
20+
BaseURL.SHIPENGINE_RPC_URL.value, "v1/labels/rates/se-799373193"
21+
),
22+
"body": json.dumps(
23+
{
24+
"batch_id": "",
25+
"carrier_code": "stamps_com",
26+
"carrier_id": "se-656171",
27+
"charge_event": "carrier_default",
28+
"created_at": "2021-08-05T16:47:47.8768838Z",
29+
"display_scheme": "label",
30+
"form_download": None,
31+
"insurance_claim": None,
32+
"insurance_cost": {"amount": 0.0, "currency": "usd"},
33+
"is_international": False,
34+
"is_return_label": False,
35+
"label_download": {
36+
"href": "https://api.shipengine.com/v1/downloads/10/_EKGeA4yuEuLzLq81iOzew/label-75693596.pdf", # noqa
37+
"pdf": "https://api.shipengine.com/v1/downloads/10/_EKGeA4yuEuLzLq81iOzew/label-75693596.pdf", # noqa
38+
"png": "https://api.shipengine.com/v1/downloads/10/_EKGeA4yuEuLzLq81iOzew/label-75693596.png", # noqa
39+
"zpl": "https://api.shipengine.com/v1/downloads/10/_EKGeA4yuEuLzLq81iOzew/label-75693596.zpl", # noqa
40+
},
41+
"label_format": "pdf",
42+
"label_id": "se-799373193",
43+
"label_image_id": None,
44+
"label_layout": "4x6",
45+
"package_code": "package",
46+
"packages": [
47+
{
48+
"dimensions": {
49+
"height": 0.0,
50+
"length": 0.0,
51+
"unit": "inch",
52+
"width": 0.0,
53+
},
54+
"external_package_id": None,
55+
"insured_value": {"amount": 0.0, "currency": "usd"},
56+
"label_messages": {
57+
"reference1": None,
58+
"reference2": None,
59+
"reference3": None,
60+
},
61+
"package_code": "package",
62+
"package_id": 80328023,
63+
"sequence": 1,
64+
"tracking_number": "9400111899560334651289",
65+
"weight": {"unit": "ounce", "value": 1.0},
66+
}
67+
],
68+
"rma_number": None,
69+
"service_code": "usps_first_class_mail",
70+
"ship_date": "2021-08-05T00:00:00Z",
71+
"shipment_cost": {"amount": 3.35, "currency": "usd"},
72+
"shipment_id": "se-144794216",
73+
"status": "completed",
74+
"trackable": True,
75+
"tracking_number": "9400111899560334651289",
76+
"tracking_status": "in_transit",
77+
"voided": False,
78+
"voided_at": None,
79+
}
80+
),
81+
"status": 200,
82+
"content_type": "application/json",
83+
}
84+
)
85+
86+
shipengine = stub_shipengine_instance()
87+
params = {
88+
"validate_address": "no_validation",
89+
"label_layout": "4x6",
90+
"label_format": "pdf",
91+
"label_download_type": "url",
92+
"display_scheme": "label",
93+
}
94+
result = shipengine.create_label_from_rate_id(rate_id="se-799373193", params=params)
95+
self.assertEqual("se-799373193", result["label_id"])
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""Testing the create label from shipment functionality."""
2+
import json
3+
import unittest
4+
import urllib.parse as urlparse
5+
6+
import responses
7+
8+
from shipengine.enums import BaseURL, Endpoints
9+
from tests.util import stub_shipengine_instance
10+
11+
12+
class TestCreateLabelFromShipment(unittest.TestCase):
13+
def test_create_label_from_shipment(self) -> None:
14+
"""Test purchase label from shipment."""
15+
responses.add(
16+
**{
17+
"method": responses.POST,
18+
"url": urlparse.urljoin(
19+
BaseURL.SHIPENGINE_RPC_URL.value, Endpoints.GET_RATE_FROM_SHIPMENT.value
20+
),
21+
"body": json.dumps(
22+
{
23+
"label_id": "se-75714944",
24+
"status": "completed",
25+
"shipment_id": "se-144835189",
26+
"ship_date": "2021-08-05T00:00:00Z",
27+
"created_at": "2021-08-05T18:01:46.0013168Z",
28+
"shipment_cost": {"currency": "usd", "amount": 27.98},
29+
"insurance_cost": {"currency": "usd", "amount": 0.0},
30+
"tracking_number": "1Z63R0960311549776",
31+
"is_return_label": False,
32+
"rma_number": None,
33+
"is_international": False,
34+
"batch_id": "",
35+
"carrier_id": "se-656172",
36+
"service_code": "ups_ground",
37+
"package_code": "package",
38+
"voided": False,
39+
"voided_at": None,
40+
"label_format": "pdf",
41+
"display_scheme": "label",
42+
"label_layout": "4x6",
43+
"trackable": True,
44+
"label_image_id": None,
45+
"carrier_code": "ups",
46+
"tracking_status": "in_transit",
47+
"label_download": {
48+
"pdf": "https://api.shipengine.com/v1/downloads/10/nRpNbEgTzkaBB1HitZNfjQ/label-75714944.pdf", # noqa
49+
"png": "https://api.shipengine.com/v1/downloads/10/nRpNbEgTzkaBB1HitZNfjQ/label-75714944.png", # noqa
50+
"zpl": "https://api.shipengine.com/v1/downloads/10/nRpNbEgTzkaBB1HitZNfjQ/label-75714944.zpl", # noqa
51+
"href": "https://api.shipengine.com/v1/downloads/10/nRpNbEgTzkaBB1HitZNfjQ/label-75714944.pdf", # noqa
52+
},
53+
"form_download": None,
54+
"insurance_claim": None,
55+
"packages": [
56+
{
57+
"package_id": 80350838,
58+
"package_code": "package",
59+
"weight": {"value": 20.0, "unit": "ounce"},
60+
"dimensions": {
61+
"unit": "inch",
62+
"length": 24.0,
63+
"width": 12.0,
64+
"height": 6.0,
65+
},
66+
"insured_value": {"currency": "usd", "amount": 0.0},
67+
"tracking_number": "1Z63R0960311549776",
68+
"label_messages": {
69+
"reference1": None,
70+
"reference2": None,
71+
"reference3": None,
72+
},
73+
"external_package_id": None,
74+
"sequence": 1,
75+
}
76+
],
77+
"charge_event": "carrier_default",
78+
}
79+
),
80+
"status": 200,
81+
"content_type": "application/json",
82+
}
83+
)
84+
85+
shipengine = stub_shipengine_instance()
86+
result = shipengine.create_label_from_shipment(
87+
shipment={
88+
"shipment": {
89+
"service_code": "ups_ground",
90+
"ship_to": {
91+
"name": "Jane Doe",
92+
"address_line1": "525 S Winchester Blvd",
93+
"city_locality": "San Jose",
94+
"state_province": "CA",
95+
"postal_code": "95128",
96+
"country_code": "US",
97+
"address_residential_indicator": "yes",
98+
},
99+
"ship_from": {
100+
"name": "John Doe",
101+
"company_name": "Example Corp",
102+
"phone": "555-555-5555",
103+
"address_line1": "4009 Marathon Blvd",
104+
"city_locality": "Austin",
105+
"state_province": "TX",
106+
"postal_code": "78756",
107+
"country_code": "US",
108+
"address_residential_indicator": "no",
109+
},
110+
"packages": [
111+
{
112+
"weight": {"value": 20, "unit": "ounce"},
113+
"dimensions": {
114+
"height": 6,
115+
"width": 12,
116+
"length": 24,
117+
"unit": "inch",
118+
},
119+
}
120+
],
121+
}
122+
}
123+
)
124+
self.assertEqual(result["status"], "completed")

0 commit comments

Comments
 (0)