DEV Community

fulfillmentyx
fulfillmentyx

Posted on Edited on

Testing a quantity field received as a string

By YX Fulfillment

The partner column contains text, including the value 0. This import accepts whole numbers from zero through 999,999 and allows surrounding whitespace. Blank, missing, signed and decimal values each get their own result. A planning quantity can be zero here; a physical pick instruction may have a different rule.

A result carries both its disposition and the parsed count. That makes it straightforward to quarantine bad rows without replacing their quantities with zero.

import re

DIGITS = re.compile(r"[0-9]{1,6}")

def parse_units(raw):
    if raw is None:
        return "missing", None
    if not isinstance(raw, str):
        return "type error", None
    value = raw.strip()
    if not value:
        return "blank", None
    if DIGITS.fullmatch(value) is None:
        return "invalid integer", None
    return "accepted", int(value)

inputs = ["0", " 003 ", "-1", None, "", "1.5", "1.0", "1000000", 7]
actual = [parse_units(item) for item in inputs]
assert actual == [
    ("accepted", 0), ("accepted", 3), ("invalid integer", None),
    ("missing", None), ("blank", None), ("invalid integer", None),
    ("invalid integer", None), ("invalid integer", None), ("type error", None),
]
for raw, outcome in zip(inputs, actual):
    print(repr(raw), "=>", outcome)
Enter fullscreen mode Exit fullscreen mode

Output from a local run on Python 3.9.6

'0' => ('accepted', 0)
' 003 ' => ('accepted', 3)
'-1' => ('invalid integer', None)
None => ('missing', None)
'' => ('blank', None)
'1.5' => ('invalid integer', None)
'1.0' => ('invalid integer', None)
'1000000' => ('invalid integer', None)
7 => ('type error', None)
Enter fullscreen mode Exit fullscreen mode

The decimal form 1.0 is rejected even though its magnitude is a whole number. The source agreed to send an integer token. Accepting more forms quietly makes integrations difficult to correct later. The six-character bound also prevents the parser from treating a misplaced identifier as an order quantity of arbitrary size.

Leading zeroes are fine here because the field records a magnitude. Keep the original token in the import audit when it helps diagnose the source. Handle measured quantities such as weight with a separate contract covering units and decimal precision. A malformed line should surface at intake, where a reviewer can still request a corrected file.

Top comments (0)