askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_money.py · 7.6 KB · 167 lines
| 1 | """Money representation (plan/value-accounts-and-denominations-spec.md §1): |
| 2 | an amount is an integer in its denomination's smallest unit. |
| 3 | |
| 4 | The cases that matter are the ones the predecessor system's fixed |
| 5 | Decimal(28,8) column could not express: yen with no minor unit at all, |
| 6 | ether with eighteen places, and the refusal to silently round away |
| 7 | precision a denomination cannot hold. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | import pathlib |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | import object_money |
| 16 | import object_records |
| 17 | |
| 18 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 19 | PACKAGES = REPO_ROOT / "packages" |
| 20 | FINANCE = PACKAGES / "app-finance" |
| 21 | |
| 22 | |
| 23 | def setup_env(tmp_path, monkeypatch, *, seed=True): |
| 24 | data_dir = tmp_path / "data" |
| 25 | schema_dir = data_dir / "schemas" |
| 26 | schema_dir.mkdir(parents=True) |
| 27 | (schema_dir / "denominations.json").write_text( |
| 28 | (FINANCE / "schemas" / "denominations.json").read_text()) |
| 29 | coll = data_dir / "collections" / "denominations" |
| 30 | coll.mkdir(parents=True) |
| 31 | seed_text = (FINANCE / "seed" / "denominations.tsv").read_text() |
| 32 | header = seed_text.splitlines()[0] + "\n" |
| 33 | (coll / "records.tsv").write_text(seed_text if seed else header) |
| 34 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 35 | return data_dir |
| 36 | |
| 37 | |
| 38 | # --- scale resolution --------------------------------------------------------- |
| 39 | |
| 40 | def test_seeded_denominations_carry_the_right_scales(tmp_path, monkeypatch): |
| 41 | data_dir = setup_env(tmp_path, monkeypatch) |
| 42 | for code, scale in (("USD", 2), ("JPY", 0), ("BTC", 8), ("ETH", 18), |
| 43 | ("USDC", 6), ("XAU", 4)): |
| 44 | assert object_money.scale_for(code, base_dir=data_dir) == scale, code |
| 45 | # Case-insensitive, like the schema says. |
| 46 | assert object_money.scale_for("btc", base_dir=data_dir) == 8 |
| 47 | |
| 48 | |
| 49 | def test_scale_falls_back_without_the_collection_and_never_raises(tmp_path, monkeypatch): |
| 50 | data_dir = tmp_path / "empty" |
| 51 | data_dir.mkdir() |
| 52 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 53 | assert object_money.scale_for("USD", base_dir=data_dir) == 2 |
| 54 | assert object_money.scale_for("BTC", base_dir=data_dir) == 8 |
| 55 | # An unknown code renders as if it were dollars rather than exploding: |
| 56 | # a missing denomination is a display problem, not a reason to 500. |
| 57 | assert object_money.scale_for("WAT", base_dir=data_dir) == 2 |
| 58 | assert object_money.scale_for("", base_dir=data_dir) == 2 |
| 59 | |
| 60 | |
| 61 | def test_operator_defined_denominations_win_over_the_fallback(tmp_path, monkeypatch): |
| 62 | data_dir = setup_env(tmp_path, monkeypatch) |
| 63 | object_records.create_collection_record( |
| 64 | "denominations", |
| 65 | {"id": "den-points", "code": "POINTS", "name": "Loyalty Points", |
| 66 | "kind": "points", "scale": "0", "owner_id": "dan"}, |
| 67 | base_dir=data_dir) |
| 68 | assert object_money.scale_for("POINTS", base_dir=data_dir) == 0 |
| 69 | |
| 70 | |
| 71 | # --- conversion --------------------------------------------------------------- |
| 72 | |
| 73 | def test_to_minor_and_back_across_scales(): |
| 74 | assert object_money.to_minor("1500.00", 2) == 150000 |
| 75 | assert object_money.to_minor("1,500", 2) == 150000 # grouped input |
| 76 | assert object_money.to_minor("1500", 0) == 1500 # JPY: no minor unit |
| 77 | assert object_money.to_minor("0.05", 8) == 5000000 # satoshis |
| 78 | assert object_money.to_minor("", 2) == 0 |
| 79 | assert str(object_money.from_minor(150000, 2)) == "1500.00" |
| 80 | assert str(object_money.from_minor(5000000, 8)) == "0.05000000" |
| 81 | |
| 82 | |
| 83 | def test_eighteen_decimals_survive_exactly(tmp_path, monkeypatch): |
| 84 | """The case the predecessor's fixed 8-decimal column could not hold: |
| 85 | one wei is a real amount and must round-trip without loss.""" |
| 86 | one_wei = object_money.to_minor("0.000000000000000001", 18) |
| 87 | assert one_wei == 1 |
| 88 | assert str(object_money.from_minor(one_wei, 18)) == "1E-18" |
| 89 | big = object_money.to_minor("1234567.123456789012345678", 18) |
| 90 | assert object_money.from_minor(big, 18) == __import__("decimal").Decimal( |
| 91 | "1234567.123456789012345678") |
| 92 | |
| 93 | |
| 94 | def test_precision_that_does_not_fit_is_refused_not_rounded(): |
| 95 | """Silently turning half a cent into a cent is how a rounding error |
| 96 | becomes a real number at scale, so to_minor refuses and the caller must |
| 97 | ask for rounding explicitly.""" |
| 98 | with pytest.raises(object_money.MoneyError): |
| 99 | object_money.to_minor("0.005", 2) |
| 100 | with pytest.raises(object_money.MoneyError): |
| 101 | object_money.to_minor("1500.5", 0) # yen has no half |
| 102 | assert object_money.quantize_minor("0.005", 2) == 1 # deliberate |
| 103 | assert object_money.quantize_minor("0.004", 2) == 0 |
| 104 | with pytest.raises(object_money.MoneyError): |
| 105 | object_money.to_minor("not a number", 2) |
| 106 | |
| 107 | |
| 108 | def test_negative_amounts_round_trip(): |
| 109 | assert object_money.to_minor("-25.00", 2) == -2500 |
| 110 | assert str(object_money.from_minor(-2500, 2)) == "-25.00" |
| 111 | |
| 112 | |
| 113 | # --- display and entry -------------------------------------------------------- |
| 114 | |
| 115 | def test_formatting_uses_the_denominations_scale_and_symbol(tmp_path, monkeypatch): |
| 116 | data_dir = setup_env(tmp_path, monkeypatch) |
| 117 | assert object_money.format_amount(150000, "USD", base_dir=data_dir) == "$1,500.00 USD" |
| 118 | assert object_money.format_amount(1500, "JPY", base_dir=data_dir) == "¥1,500 JPY" |
| 119 | # Trailing zeros are kept: "0.05" would hide whether the remaining six |
| 120 | # places are zero or merely unshown, and for BTC that is real money. |
| 121 | assert object_money.format_amount(5000000, "BTC", base_dir=data_dir) == "0.05000000 BTC" |
| 122 | assert object_money.format_amount(150000, "USD", base_dir=data_dir, |
| 123 | with_code=False) == "$1,500.00" |
| 124 | |
| 125 | |
| 126 | def test_parse_amount_accepts_what_format_produces(tmp_path, monkeypatch): |
| 127 | data_dir = setup_env(tmp_path, monkeypatch) |
| 128 | for minor, code in ((150000, "USD"), (1500, "JPY"), (5000000, "BTC")): |
| 129 | rendered = object_money.format_amount(minor, code, base_dir=data_dir) |
| 130 | assert object_money.parse_amount(rendered, code, base_dir=data_dir) == minor |
| 131 | |
| 132 | |
| 133 | def test_denominations_of_different_kinds_are_never_summed(): |
| 134 | """A total of dollars and satoshis is not a number. Combining them |
| 135 | requires converting through a rate stamped at the transaction moment |
| 136 | (docs/logic-decisions.md #1), which is a deliberate act, not an |
| 137 | accidental addition.""" |
| 138 | assert object_money.same_denomination("USD", "USD", "usd") is True |
| 139 | assert object_money.same_denomination("USD", "BTC") is False |
| 140 | assert object_money.same_denomination("USD", "") is True # blank = unspecified |
| 141 | |
| 142 | |
| 143 | # --- the collection itself ---------------------------------------------------- |
| 144 | |
| 145 | def test_seed_matches_the_schema_and_covers_each_kind(tmp_path, monkeypatch): |
| 146 | data_dir = setup_env(tmp_path, monkeypatch) |
| 147 | schema = json.loads((FINANCE / "schemas" / "denominations.json").read_text()) |
| 148 | header = (FINANCE / "seed" / "denominations.tsv").read_text().splitlines()[0].split("\t") |
| 149 | assert header == [f["name"] for f in schema["fields"]] |
| 150 | |
| 151 | rows = object_records.read_collection_records("denominations", base_dir=data_dir) |
| 152 | by_code = {r["code"]: r for r in rows} |
| 153 | assert {"USD", "JPY", "BTC", "ETH", "USDC", "XAU"} <= set(by_code) |
| 154 | assert by_code["BTC"]["kind"] == "crypto" |
| 155 | assert by_code["USDC"]["kind"] == "stablecoin" |
| 156 | assert by_code["XAU"]["kind"] == "metal" |
| 157 | assert all(r["is_system"] == "true" for r in rows) |
| 158 | # Every seeded scale must be one object_money can actually use. |
| 159 | for row in rows: |
| 160 | assert 0 <= int(row["scale"]) <= object_money.MAX_SCALE |
| 161 | |
| 162 | |
| 163 | def test_denomination_lookup_returns_the_record(tmp_path, monkeypatch): |
| 164 | data_dir = setup_env(tmp_path, monkeypatch) |
| 165 | btc = object_money.denomination("BTC", base_dir=data_dir) |
| 166 | assert btc["name"] == "Bitcoin" and btc["scale"] == "8" |
| 167 | assert object_money.denomination("NOPE", base_dir=data_dir) is None |