Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/nationex/karrio/providers/nationex/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _extract_details(
for status in list(provider_units.TrackingStatus)
if shipment.ShipmentStatus in status.value
),
provider_units.TrackingStatus.in_transit.name,
provider_units.TrackingStatus.unknown.name,
)

return models.TrackingDetails(
Expand Down
14 changes: 10 additions & 4 deletions plugins/nationex/karrio/providers/nationex/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ def items_filter(key: str) -> bool:


class TrackingStatus(lib.Enum):
on_hold = ["OnHold", "Attention"]
delivered = ["Delivered"]
in_transit = ["Pickup", "Transit"]
delivery_failed = ["ReturnToSender", "RefusedDelivery"]
# Nationex shipment states normalized to Karrio tracker status values.
pending = ["Creation", "DataReceived"]
picked_up = ["Pickup", "PartiallyPickedUp"]
in_transit = ["Transit", "PartiallyInTransit"]
out_for_delivery = ["OutForDelivery", "PartiallyOutForDelivery"]
delivered = ["Delivered", "PartiallyDelivered"]
on_hold = ["OnHold", "Attention", "PickupAttemptFailed", "RefusedDelivery"]
cancelled = ["Cancelled"]
return_to_sender = ["ReturnCompleted", "ReturnToSender"]
ready_for_pickup = ["OutForPickup"]
unknown = ["Unknown"]


class TrackingIncidentReason(lib.Enum):
Expand Down
42 changes: 41 additions & 1 deletion plugins/nationex/tests/nationex/test_tracking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import json
from unittest.mock import patch, ANY
from .fixture import gateway

Expand Down Expand Up @@ -36,6 +37,44 @@ def test_parse_tracking_response(self):

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

def test_status_mapping(self):
status_expectations = {
"Creation": "pending",
"DataReceived": "pending",
"Pickup": "picked_up",
"PartiallyPickedUp": "picked_up",
"Transit": "in_transit",
"PartiallyInTransit": "in_transit",
"OutForDelivery": "out_for_delivery",
"PartiallyOutForDelivery": "out_for_delivery",
"Delivered": "delivered",
"PartiallyDelivered": "delivered",
"OnHold": "on_hold",
"Attention": "on_hold",
"PickupAttemptFailed": "on_hold",
"RefusedDelivery": "on_hold",
"Cancelled": "cancelled",
"ReturnCompleted": "return_to_sender",
"ReturnToSender": "return_to_sender",
"OutForPickup": "ready_for_pickup",
"SomethingUnexpected": "unknown",
}

for nationex_status, expected_karrio_status in status_expectations.items():
with self.subTest(nationex_status=nationex_status):
tracking_response = json.loads(TrackingResponse)
tracking_response["ShipmentStatus"] = nationex_status
tracking_response["StatusHistories"][0]["ShipmentStatus"] = nationex_status

with patch("karrio.mappers.nationex.proxy.lib.request") as mock:
mock.return_value = json.dumps(tracking_response)
parsed_response = (
karrio.Tracking.fetch(self.TrackingRequest).from_(gateway).parse()
)

tracking = lib.to_dict(parsed_response)[0][0]
self.assertEqual(tracking["status"], expected_karrio_status)

def test_parse_error_response(self):
with patch("karrio.mappers.nationex.proxy.lib.request") as mock:
mock.return_value = ErrorResponse
Expand Down Expand Up @@ -66,6 +105,7 @@ def test_parse_error_response(self):
"date": "2019-08-24",
"description": "Data received",
"location": "St-Hubert",
"status": "pending",
"time": "14:15 PM",
"timestamp": "2019-08-24T14:15:22.000Z",
}
Expand All @@ -83,7 +123,7 @@ def test_parse_error_response(self):
"shipping_date": "2021-03-30",
},
"meta": {"accounty_number": "165556", "reference": "CX4335"},
"status": "in_transit",
"status": "pending",
"tracking_number": "103882774",
}
],
Expand Down