|
| 1 | +"""field.currency read decoding — the Python ObjectManager. |
| 2 | +
|
| 3 | +Currency is integer minor units, natively and on the wire. A table column stores it as |
| 4 | +BIGINT, which pg8000 already hands back as an ``int``. A VIEW column need not: ``SUM`` over |
| 5 | +BIGINT is Postgres ``numeric``, which pg8000 hands back as a ``Decimal`` — and a ``Decimal`` |
| 6 | +leaves a FastAPI route as the JSON STRING ``"100000"`` (Pydantic v2 serializes it that way), |
| 7 | +where the contract says the number ``100000``. So the value's wire form depended on the |
| 8 | +physical column type rather than on the declared field. The read codec now decodes an |
| 9 | +integral currency value to ``int`` whatever the column's type. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from decimal import Decimal |
| 14 | + |
| 15 | +from metaobjects.loader.meta_data_loader import MetaDataLoader |
| 16 | +from metaobjects.loader.sources import InMemoryStringSource |
| 17 | +from metaobjects.runtime.object_manager import _decode_read_value |
| 18 | + |
| 19 | +MODEL = """{"metadata.root": {"package": "acme", "children": [ |
| 20 | + {"object.entity": {"name": "Leg", "children": [ |
| 21 | + {"source.rdb": {"@table": "legs"}}, |
| 22 | + {"field.long": {"name": "id"}}, |
| 23 | + {"field.currency": {"name": "rate", "@currency": "USD"}}, |
| 24 | + {"field.decimal": {"name": "weight", "@precision": 10, "@scale": 2}}, |
| 25 | + {"identity.primary": {"name": "pk", "@fields": "id", "@generation": "increment"}} |
| 26 | + ]}} |
| 27 | +]}}""" |
| 28 | + |
| 29 | + |
| 30 | +def _field(name: str): |
| 31 | + result = MetaDataLoader().load([InMemoryStringSource(MODEL, "test.json")]) |
| 32 | + assert result.errors == [] |
| 33 | + leg = next(c for c in result.root.children() if c.name == "Leg") |
| 34 | + return next(f for f in leg.fields() if f.name == name) |
| 35 | + |
| 36 | + |
| 37 | +def test_integral_decimal_currency_decodes_to_int_minor_units() -> None: |
| 38 | + value = _decode_read_value(_field("rate"), Decimal("100000")) |
| 39 | + assert value == 100000 |
| 40 | + assert type(value) is int |
| 41 | + |
| 42 | + |
| 43 | +def test_int_currency_is_unchanged() -> None: |
| 44 | + value = _decode_read_value(_field("rate"), 1099) |
| 45 | + assert value == 1099 |
| 46 | + assert type(value) is int |
| 47 | + |
| 48 | + |
| 49 | +def test_null_currency_stays_null() -> None: |
| 50 | + assert _decode_read_value(_field("rate"), None) is None |
| 51 | + |
| 52 | + |
| 53 | +def test_fractional_decimal_currency_is_left_alone() -> None: |
| 54 | + # An avg over minor units can be fractional; no int is honest, so the value is |
| 55 | + # returned as read rather than silently rounded. |
| 56 | + value = _decode_read_value(_field("rate"), Decimal("12.5")) |
| 57 | + assert value == Decimal("12.5") |
| 58 | + |
| 59 | + |
| 60 | +def test_field_decimal_keeps_its_decimal() -> None: |
| 61 | + value = _decode_read_value(_field("weight"), Decimal("12.50")) |
| 62 | + assert value == Decimal("12.50") |
| 63 | + assert type(value) is Decimal |
0 commit comments