Skip to content

Commit 7eef9f0

Browse files
committed
nationex: normalize additional tracking statuses
1 parent 2b5c47e commit 7eef9f0

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

plugins/nationex/karrio/providers/nationex/tracking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _extract_details(
4040
for status in list(provider_units.TrackingStatus)
4141
if shipment.ShipmentStatus in status.value
4242
),
43-
provider_units.TrackingStatus.in_transit.name,
43+
provider_units.TrackingStatus.unknown.name,
4444
)
4545

4646
return models.TrackingDetails(

plugins/nationex/karrio/providers/nationex/units.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ def items_filter(key: str) -> bool:
8989

9090

9191
class TrackingStatus(lib.Enum):
92-
on_hold = ["OnHold", "Attention"]
93-
delivered = ["Delivered"]
94-
in_transit = ["Pickup", "Transit"]
95-
delivery_failed = ["ReturnToSender", "RefusedDelivery"]
92+
# Nationex shipment states normalized to Karrio tracker status values.
93+
pending = ["Creation", "DataReceived"]
94+
picked_up = ["Pickup", "PartiallyPickedUp"]
95+
in_transit = ["Transit", "PartiallyInTransit"]
9696
out_for_delivery = ["OutForDelivery", "PartiallyOutForDelivery"]
97+
delivered = ["Delivered", "PartiallyDelivered"]
98+
on_hold = ["OnHold", "Attention", "PickupAttemptFailed", "RefusedDelivery"]
99+
cancelled = ["Cancelled"]
100+
return_to_sender = ["ReturnCompleted", "ReturnToSender"]
101+
ready_for_pickup = ["OutForPickup"]
102+
unknown = ["Unknown"]
97103

98104

99105
class TrackingIncidentReason(lib.Enum):

plugins/nationex/tests/nationex/test_tracking.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import json
23
from unittest.mock import patch, ANY
34
from .fixture import gateway
45

@@ -36,6 +37,44 @@ def test_parse_tracking_response(self):
3637

3738
self.assertListEqual(lib.to_dict(parsed_response), ParsedTrackingResponse)
3839

40+
def test_status_mapping(self):
41+
status_expectations = {
42+
"Creation": "pending",
43+
"DataReceived": "pending",
44+
"Pickup": "picked_up",
45+
"PartiallyPickedUp": "picked_up",
46+
"Transit": "in_transit",
47+
"PartiallyInTransit": "in_transit",
48+
"OutForDelivery": "out_for_delivery",
49+
"PartiallyOutForDelivery": "out_for_delivery",
50+
"Delivered": "delivered",
51+
"PartiallyDelivered": "delivered",
52+
"OnHold": "on_hold",
53+
"Attention": "on_hold",
54+
"PickupAttemptFailed": "on_hold",
55+
"RefusedDelivery": "on_hold",
56+
"Cancelled": "cancelled",
57+
"ReturnCompleted": "return_to_sender",
58+
"ReturnToSender": "return_to_sender",
59+
"OutForPickup": "ready_for_pickup",
60+
"SomethingUnexpected": "unknown",
61+
}
62+
63+
for nationex_status, expected_karrio_status in status_expectations.items():
64+
with self.subTest(nationex_status=nationex_status):
65+
tracking_response = json.loads(TrackingResponse)
66+
tracking_response["ShipmentStatus"] = nationex_status
67+
tracking_response["StatusHistories"][0]["ShipmentStatus"] = nationex_status
68+
69+
with patch("karrio.mappers.nationex.proxy.lib.request") as mock:
70+
mock.return_value = json.dumps(tracking_response)
71+
parsed_response = (
72+
karrio.Tracking.fetch(self.TrackingRequest).from_(gateway).parse()
73+
)
74+
75+
tracking = lib.to_dict(parsed_response)[0][0]
76+
self.assertEqual(tracking["status"], expected_karrio_status)
77+
3978
def test_parse_error_response(self):
4079
with patch("karrio.mappers.nationex.proxy.lib.request") as mock:
4180
mock.return_value = ErrorResponse
@@ -66,6 +105,7 @@ def test_parse_error_response(self):
66105
"date": "2019-08-24",
67106
"description": "Data received",
68107
"location": "St-Hubert",
108+
"status": "pending",
69109
"time": "14:15 PM",
70110
"timestamp": "2019-08-24T14:15:22.000Z",
71111
}
@@ -83,7 +123,7 @@ def test_parse_error_response(self):
83123
"shipping_date": "2021-03-30",
84124
},
85125
"meta": {"accounty_number": "165556", "reference": "CX4335"},
86-
"status": "in_transit",
126+
"status": "pending",
87127
"tracking_number": "103882774",
88128
}
89129
],

0 commit comments

Comments
 (0)