Postal codes are strings: test a CSV/JSON fixture round trip in Python
A postal code can look like a number without being one. If an importer converts "00123" to 123 , the leading zeros have already disappeared. Converting the result back to a string does not recover them. Canadian-shaped values such as "A1A 1A1" also contain letters and a significant space. For an address form test, preserving the postal string is only half the job. The street, locality, region and…
A postal code can appear numeric, yet lack numerical properties. When converting a postal code such as "00123" to numeric form "123", leading zeros are lost irretrievably. Restoring the original string representation is impossible. Canada-specific postal codes like "A1A 1A1" incorporate alphabetical characters and a space. When testing address forms, preserving the postal string is insufficient.
Street, locality, region, and postal fields must remain linked to a single record. An innocuously formatted code transplanted into a different locality may render a fixture deceptive. This guide constructs an offline verification for an address export containing a JSON addresses array and a CSV ZIP column. Developed using Python's built-in libraries, the provided test fixtures are fabricated, devoid of real postal code assignments or deliverable addresses. No customer records or external databases are required.
To ensure reproducibility, employ Python 3.10 or later. Save the following code snippet as postal_roundtrip.py and execute it within a new directory, allowing the output files to be clearly distinguished. The script performs the following steps:
1. Import the necessary modules: csv, io, json, and pathlib.
2. Define a function named check that accepts a document and CSV text as parameters.
3. Extract the addresses from the document and iterate over them, beginning with an index of 1.
4. Verify that the ZIP code for each address is a string. If not, raise a ValueError indicating the row number and that postal codes must be stored as strings.
5. Check if the sourceId and sourceRecordUrl fields are present for each address. If either is missing, raise a ValueError stating that source references must be preserved.
6. Convert the CSV text into a list of dictionaries using DictReader, stripping any leading whitespace.
7. Compare the number of rows in the CSV data with the number of addresses in the document. If they differ, raise a ValueError indicating a discrepancy in row counts.
8. Establish a dictionary named fields containing the columns Street, City, State, ZIP, Country, and FullAddress. These correspond to the address fields in the document.
9. Iterate through the addresses and rows concurrently using zip, comparing each field between the original address and the row data.
10. If any field values do not match, raise a ValueError specifying the row number and the column with differing values.
11. If the function completes without raising any exceptions, it returns the original addresses.
The provided example document includes two fictitious addresses with varying postal code formats: one numeric (00123) and one alphanumeric (A1A 1A1). These illustrative fixtures are not intended to represent actual postal codes or deliverable addresses but serve solely to demonstrate the round-trip conversion process. No customer records or third-party databases are necessary for this test.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.