Skip to content

Commit

Permalink
[ADD] bpost_address_validation: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kouffsamuel committed Apr 17, 2023
1 parent d9dda70 commit 65a0ce0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 83 deletions.
163 changes: 81 additions & 82 deletions bpost_address_validation/wizards/bpost_address_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,107 +12,106 @@ class BpostAddressValidationWizard(models.TransientModel):
_name = "bpost.address.validation.wizard"

partner_id = fields.Many2one("res.partner", readonly=True)
response_address = fields.Json(compute="_compute_response_address")
is_valid = fields.Boolean(compute="_compute_is_valid_address")
is_valid = fields.Boolean(compute="_compute_response_address")
bad_address = fields.Boolean()
warning_message = fields.Char(readonly=True)
suggest_changes = fields.Char(readonly=True)
bpost_address = fields.Json(compute="_compute_bpost_address")
bpost_address = fields.Json()

@api.depends("partner_id")
def _compute_response_address(self):
for rec in self:
partner = rec.partner_id
if partner.country_id.code == "BE":
# This is the JSON that should be sent as input to the API.
playload = {
"ValidateAddressesRequest": {
"AddressToValidateList": {
"AddressToValidate": [
{
"@id": "1",
"PostalAddress": {
"DeliveryPointLocation": {
"UnstructuredDeliveryPointLocation": partner.street
},
"PostalCodeMunicipality": {
"UnstructuredPostalCodeMunicipality": partner.zip
+ " "
+ partner.city
},
},
}
]
},
"ValidateAddressOptions": {
"IncludeFormatting": "true",
"IncludeSuggestions": "true",
"IncludeSubmittedAddress": "true",
"IncludeListOfBoxes": "true",
"IncludeNumberOfBoxes": "true",
"IncludeDefaultGeoLocation": "true",
"IncludeDefaultGeoLocationForBoxes": "true",
},
}
}
response = requests.post(
"https://webservices-pub.bpost.be/ws/"
+ "ExternalMailingAddressProofingCSREST_v1/address/validateAddresses",
json=playload,
timeout=10,
headers={"content-type": "application/json"},
)
response = self.send_request(partner)
if response.ok:
rec.response_address = response.json()
json = response.json()
# Transform the result into a BpostAddress object.
rec.bpost_address = BpostAddress(json).toJson()
if rec.bpost_address:
if "error" in rec.bpost_address:
if (
"street_name" in rec.bpost_address
and "postal_code" in rec.bpost_address
and "municipality_name" in rec.bpost_address
and "street_number" in rec.bpost_address
):
self.invalid_address_and_suggest_changes(rec)
else:
self.invalid_address(rec)
else:
rec.is_valid = True
else:
rec.response_address = {}
raise UserError(
_("An error occurred when fetching data from bpost API.")
)
else:
rec.response_address = {}
rec.is_valid = True

@api.depends("response_address")
def _compute_bpost_address(self):
for rec in self:
# Transform the result into a BpostAddress object.
rec.bpost_address = BpostAddress(rec.response_address).toJson()
def invalid_address(self, rec):
rec.warning_message = _(
"The given address is not complete or the address cannot be found"
)
rec.bad_address = True
rec.is_valid = False

@api.depends("bpost_address")
def _compute_is_valid_address(self):
for rec in self:
if rec.bpost_address:
if "error" in rec.bpost_address:
if (
"street_name" in rec.bpost_address
and "postal_code" in rec.bpost_address
and "municipality_name" in rec.bpost_address
and "street_number" in rec.bpost_address
):
rec.warning_message = _(
"An error has been detected in the given address. "
+ "Would you like to keep the suggest change ?"
)
changes = "{} {} {}, {}".format(
rec.bpost_address["street_name"],
rec.bpost_address["street_number"],
rec.bpost_address["postal_code"],
rec.bpost_address["municipality_name"],
)
def invalid_address_and_suggest_changes(self, rec):
rec.warning_message = _(
"An error has been detected in the given address. "
+ "Would you like to keep the suggest change ?"
)
changes = "{} {} {}, {}".format(
rec.bpost_address["street_name"],
rec.bpost_address["street_number"],
rec.bpost_address["postal_code"],
rec.bpost_address["municipality_name"],
)

rec.suggest_changes = changes
rec.bad_address = False
rec.is_valid = False
else:
rec.warning_message = _(
"The given address is not complete or the address cannot be found"
)
rec.bad_address = True
rec.is_valid = False
else:
rec.is_valid = True
else:
rec.is_valid = True
rec.suggest_changes = changes
rec.bad_address = False
rec.is_valid = False

def send_request(self, partner):
playload = {
"ValidateAddressesRequest": {
"AddressToValidateList": {
"AddressToValidate": [
{
"@id": "1",
"PostalAddress": {
"DeliveryPointLocation": {
"UnstructuredDeliveryPointLocation": partner.street
},
"PostalCodeMunicipality": {
"UnstructuredPostalCodeMunicipality": partner.zip
+ " "
+ partner.city
},
},
}
]
},
"ValidateAddressOptions": {
"IncludeFormatting": "true",
"IncludeSuggestions": "true",
"IncludeSubmittedAddress": "true",
"IncludeListOfBoxes": "true",
"IncludeNumberOfBoxes": "true",
"IncludeDefaultGeoLocation": "true",
"IncludeDefaultGeoLocationForBoxes": "true",
},
}
}
response = requests.post(
"https://webservices-pub.bpost.be/ws/"
+ "ExternalMailingAddressProofingCSREST_v1/address/validateAddresses",
json=playload,
timeout=10,
headers={"content-type": "application/json"},
)

return response

def apply_changes(self):
for rec in self:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<form>
<field name="bpost_address" invisible="1" />
<field name="partner_id" invisible="1" />
<field name="response_address" invisible="1" />
<field name="is_valid" invisible="1" />
<field name="bad_address" invisible="1" />
<div
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
freezegun
vcrpy-unittest
responses

0 comments on commit 65a0ce0

Please sign in to comment.