owenrusk.dev

errands

small jobs the runtime does for the studio.

git clone https://owenrusk.dev/errands.git

commit 0387268294b3e99699fdcbd96d500ce1422d91e5
parent f6f3edbe66fe0e3571b80fd1a6f6595032dcc411
author Owen Rusk <owen@papermothgames.com>
date   2025-01-07 13:10:15 -0600
hosting: dollars as decimal, not float
errands/hosting.py+7-2
tests/test_hosting.py+5-1
diff --git a/errands/hosting.py b/errands/hosting.py
index 2cdb5cc..9ddf4ef 100644
--- a/errands/hosting.py
+++ b/errands/hosting.py
@@ -1,6 +1,7 @@
 # stillwater, account 20417. the balance is on their public account lookup.
 import json
 import urllib.request
+from decimal import Decimal
 
 from . import settings
 
@@ -14,5 +15,9 @@ def account(fetch=urllib.request.urlopen) -> dict:
         return json.load(response)
 
 
-def balance(fetch=urllib.request.urlopen) -> float:
-    return float(account(fetch)["balance"].lstrip("$").replace(",", ""))
+def dollars(text: str) -> Decimal:
+    return Decimal(text.strip().lstrip("$").replace(",", ""))
+
+
+def balance(fetch=urllib.request.urlopen) -> Decimal:
+    return dollars(account(fetch)["balance"])
diff --git a/tests/test_hosting.py b/tests/test_hosting.py
index 74ea2a8..4283fea 100644
--- a/tests/test_hosting.py
+++ b/tests/test_hosting.py
@@ -1,6 +1,7 @@
 import io
 import json
 import unittest
+from decimal import Decimal
 
 from errands import hosting
 
@@ -19,5 +20,8 @@ def fake(payload, seen=None):
 class BalanceTest(unittest.TestCase):
     def test_reads_the_lookup(self):
         seen = []
-        self.assertEqual(hosting.balance(fake(LOOKUP, seen)), 1204.5)
+        self.assertEqual(hosting.balance(fake(LOOKUP, seen)), Decimal("1204.50"))
         self.assertEqual(seen, ["https://stillwater-hosting.com/api/accounts/20417"])
+
+    def test_paid_up(self):
+        self.assertEqual(hosting.balance(fake({**LOOKUP, "balance": "$0.00"})), 0)