Skip to content

Commit

Permalink
(consolidate) dhl_express flag options processing with unit test for …
Browse files Browse the repository at this point in the history
…non supported paperless trade shipment
  • Loading branch information
danh91 committed Dec 16, 2023
1 parent ffc02d9 commit 9f1f9e2
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def rate_request(
origin_country=payload.shipper.country_code,
initializer=provider_units.shipping_options_initializer,
)

option_items = [
option for _, option in options.items() if option.state is not False
]
weight_unit, dim_unit = (
provider_units.COUNTRY_PREFERED_UNITS.get(payload.shipper.country_code)
or packages.compatible_units
Expand Down Expand Up @@ -192,9 +194,9 @@ def rate_request(
QtdShpExChrg=(
[
dhl.QtdShpExChrgType(SpecialServiceType=option.code)
for _, option in options.items()
for option in option_items
]
if any(options.items())
if any(option_items)
else None
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def shipment_request(
origin_country=shipper.country_code,
initializer=provider_units.shipping_options_initializer,
)

option_items = [
option for _, option in options.items() if option.state is not False
]
duty = customs.duty or models.Duty(paid_by="sender")
content = packages[0].parcel.content or customs.content_description or "N/A"
reference = payload.reference or getattr(payload, "id", None)
Expand All @@ -119,7 +121,6 @@ def shipment_request(
or settings.default_currency
)

dhl_special_services = {k: v for k, v in options.items() if v}
request = dhl.ShipmentRequest(
schemaVersion="10.0",
Request=settings.Request(
Expand Down Expand Up @@ -277,7 +278,7 @@ def shipment_request(
]
)
if (
options.paperless_trade.state == True
options.dhl_paperless_trade.state == True
and any(options.doc_references.state or [])
)
else None
Expand Down Expand Up @@ -393,10 +394,8 @@ def shipment_request(
currency if lib.to_money(svc.state) is not None else None
),
)
for _, svc in dhl_special_services
]
if dhl_special_services
else None,
for svc in option_items
],
Notification=(
dhl.Notification(
EmailAddress=options.email_notification_to.state or recipient.email
Expand Down Expand Up @@ -424,7 +423,7 @@ def shipment_request(
]
)
if (
options.paperless_trade.state == True
options.dhl_paperless_trade.state == True
and any(options.doc_files.state or [])
)
else None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,13 @@ def shipping_options_initializer(
_options = options.copy()

if origin_country in UNSUPPORTED_PAPERLESS_COUNTRIES:
_options.update({"paperless_trade": False})
_options.update({"dhl_paperless_trade": False})

if package_options is not None:
_options.update(package_options.content)

def items_filter(key: str) -> bool:
return (key in ShippingOption) and (_options.get(key) not in [False]) # type: ignore
return key in ShippingOption # type: ignore

return lib.units.ShippingOptions(
_options, ShippingOption, items_filter=items_filter
Expand Down
211 changes: 210 additions & 1 deletion modules/connectors/dhl_express/tests/dhl_express/test_shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class TestDHLShipment(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.ShipmentRequest = ShipmentRequest(**shipment_data)
self.NonPaperlessShipmentRequest = ShipmentRequest(
**non_paperless_intl_shipment_data
)

def test_create_shipment_request(self):
request = gateway.mapper.create_shipment_request(self.ShipmentRequest)
Expand All @@ -25,6 +28,20 @@ def test_create_shipment_request(self):

self.assertEqual(serialized_request, ShipmentRequestXml)

def test_create_non_paperless_shipment_request(self):
request = gateway.mapper.create_shipment_request(
self.NonPaperlessShipmentRequest
)

# remove MessageTime, Date for testing purpose
serialized_request = re.sub(
" <MessageTime>[^>]+</MessageTime>",
"",
request.serialize(),
)

self.assertEqual(serialized_request, NonParelessShipmentRequestXml)

@patch("karrio.mappers.dhl_express.proxy.lib.request", return_value="<a></a>")
def test_create_shipment(self, http_mock):
karrio.Shipment.create(self.ShipmentRequest).from_(gateway)
Expand Down Expand Up @@ -110,7 +127,7 @@ def test_not_supported_cancel_shipment(self):
}
],
"service": "dhl_express_worldwide_nondoc",
"options": {"paperless_trade": True, "insurance": 148.0},
"options": {"dhl_paperless_trade": True, "insurance": 148.0},
"payment": {"paid_by": "sender", "account_number": "123456789"},
"customs": {
"incoterm": "DAP",
Expand All @@ -125,6 +142,81 @@ def test_not_supported_cancel_shipment(self):
},
}

non_paperless_intl_shipment_data = {
"customs": {
"certify": True,
"commercial_invoice": True,
"commodities": [
{
"description": "description",
"hs_code": "12345",
"metadata": {},
"quantity": 1,
"sku": "sku",
"title": "title",
"value_amount": 928.1,
"value_currency": "EUR",
"weight": 0.847,
"weight_unit": "KG",
}
],
"content_type": "merchandise",
"duty": {"currency": "EUR", "declared_value": 928.1, "paid_by": "sender"},
"incoterm": "DDP",
"invoice": "36892319",
"invoice_date": "2023-12-15",
"options": {},
},
"options": {
"currency": "EUR",
"declared_value": 928.1,
"paperless_trade": False,
"shipment_date": "2023-12-15",
},
"parcels": [
{
"dimension_unit": "CM",
"height": 5,
"is_document": False,
"items": [
{
"description": "description",
"hs_code": "123456",
"metadata": {},
"quantity": 1,
"sku": "sku",
"title": "title",
"value_amount": 928.1,
"value_currency": "EUR",
"weight": 0.847,
"weight_unit": "KG",
}
],
"length": 30,
"packaging_type": "small_box",
"weight": 0.847,
"weight_unit": "KG",
"width": 22,
}
],
"recipient": {
"address_line1": "Biryat Hadid 34",
"city": "Istanbul",
"country_code": "TR",
"email": "store@customer.com",
"person_name": "Store Customer",
"postal_code": "34020",
},
"service": "dhl_express_easy_nondoc",
"shipper": {
"address_line1": "address_line_1",
"city": "city",
"country_code": "CZ",
"person_name": "person_name",
},
}


ParsedShipmentMissingArgsError = [
None,
[
Expand Down Expand Up @@ -355,6 +447,123 @@ def test_not_supported_cancel_shipment(self):
</req:ShipmentRequest>
"""

NonParelessShipmentRequestXml = """<req:ShipmentRequest xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaVersion="10.0">
<Request>
<ServiceHeader>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>site_id</SiteID>
<Password>password</Password>
</ServiceHeader>
<MetaData>
<SoftwareName>3PV</SoftwareName>
<SoftwareVersion>10.0</SoftwareVersion>
</MetaData>
</Request>
<RegionCode>EU</RegionCode>
<LanguageCode>en</LanguageCode>
<Billing>
<ShipperAccountNumber>123456789</ShipperAccountNumber>
<ShippingPaymentType>S</ShippingPaymentType>
<BillingAccountNumber>123456789</BillingAccountNumber>
</Billing>
<Consignee>
<CompanyName>N/A</CompanyName>
<AddressLine1>34 Biryat Hadid</AddressLine1>
<City>Istanbul</City>
<PostalCode>34020</PostalCode>
<CountryCode>TR</CountryCode>
<CountryName>Turkey</CountryName>
<Contact>
<PersonName>Store Customer</PersonName>
<PhoneNumber>0000</PhoneNumber>
<Email>store@customer.com</Email>
</Contact>
<StreetName>Biryat Hadid</StreetName>
<StreetNumber>34</StreetNumber>
</Consignee>
<Commodity>
<CommodityCode>sku</CommodityCode>
<CommodityName>title</CommodityName>
</Commodity>
<Dutiable>
<DeclaredValue>928.1</DeclaredValue>
<DeclaredCurrency>EUR</DeclaredCurrency>
<TermsOfTrade>DDP</TermsOfTrade>
</Dutiable>
<UseDHLInvoice>Y</UseDHLInvoice>
<ExportDeclaration>
<ExportReason>merchandise</ExportReason>
<ExportReasonCode>C</ExportReasonCode>
<InvoiceNumber>36892319</InvoiceNumber>
<InvoiceDate>2023-12-15</InvoiceDate>
<ExportLineItem>
<LineNumber>1</LineNumber>
<Quantity>1</Quantity>
<QuantityUnit>PCS</QuantityUnit>
<Description>title</Description>
<Value>928.1</Value>
<CommodityCode>sku</CommodityCode>
<Weight>
<Weight>0.85</Weight>
<WeightUnit>K</WeightUnit>
</Weight>
<GrossWeight>
<Weight>0.85</Weight>
<WeightUnit>K</WeightUnit>
</GrossWeight>
<ManufactureCountryCode>CZ</ManufactureCountryCode>
<ManufactureCountryName>Czech Republic</ManufactureCountryName>
<ImportCommodityCode>12345</ImportCommodityCode>
</ExportLineItem>
<PlaceOfIncoterm>N/A</PlaceOfIncoterm>
<ShipmentPurpose>COMMERCIAL</ShipmentPurpose>
</ExportDeclaration>
<ShipmentDetails>
<Pieces>
<Piece>
<PieceID>1</PieceID>
<PackageType>JJ</PackageType>
<Weight>0.85</Weight>
<Width>22</Width>
<Height>5</Height>
<Depth>30</Depth>
</Piece>
</Pieces>
<WeightUnit>K</WeightUnit>
<GlobalProductCode>8</GlobalProductCode>
<LocalProductCode>8</LocalProductCode>
<Date>2023-12-15</Date>
<Contents>N/A</Contents>
<DimensionUnit>C</DimensionUnit>
<PackageType>JJ</PackageType>
<IsDutiable>Y</IsDutiable>
<CurrencyCode>EUR</CurrencyCode>
</ShipmentDetails>
<Shipper>
<ShipperID>123456789</ShipperID>
<CompanyName>N/A</CompanyName>
<RegisteredAccount>123456789</RegisteredAccount>
<AddressLine1>address_line_1</AddressLine1>
<City>city</City>
<CountryCode>CZ</CountryCode>
<CountryName>Czech Republic</CountryName>
<Contact>
<PersonName>person_name</PersonName>
<PhoneNumber>0000</PhoneNumber>
</Contact>
<StreetNumber>address_line_1</StreetNumber>
</Shipper>
<Notification>
<EmailAddress>store@customer.com</EmailAddress>
</Notification>
<LabelImageFormat>PDF</LabelImageFormat>
<Label>
<LabelTemplate>6X4_PDF</LabelTemplate>
</Label>
</req:ShipmentRequest>
"""

ShipmentResponseXml = """<?xml version="1.0" encoding="UTF-8"?><res:ShipmentResponse xmlns:res='http://www.dhl.com' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation= 'http://www.dhl.com ship-val-res.xsd'>
<Response>
<ServiceHeader>
Expand Down

0 comments on commit 9f1f9e2

Please sign in to comment.