1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
"""
Testing Super Collections with their json serial output.
Several issues have be raised with serialization problem
and we need a way to really test different cases.
(C) Laurent Franceschetti 2025
"""
from datetime import datetime, date, time
import json
from collections import UserDict
from rich import print as rprint
from super_collections import SuperDict, SuperList, json_encode
# -------------------------
# Encoder (alone)
# -------------------------
# 1. Date/time objects
def test_datetime_serialization():
dt = datetime(2023, 5, 17, 15, 30)
encoded = json_encode(dt)
assert '"2023-05-17T15:30:00"' in encoded
def test_date_serialization():
d = date(2023, 5, 17)
encoded = json_encode(d)
assert '"2023-05-17"' in encoded
def test_time_serialization():
t = time(15, 30)
encoded = json_encode(t)
assert '"15:30:00"' in encoded
# 2. UserDict conversion
def test_userdict_serialization():
ud = UserDict({'a': 1, 'b': 2})
encoded = json_encode(ud)
assert '"a": 1' in encoded and '"b": 2' in encoded
# 3. Function object
def test_function_serialization():
def sample_func(x): """Adds one."""; return x + 1
encoded = json_encode(sample_func)
assert "Function:" in encoded
assert "Adds one." in encoded
# 4. Object with __str__ defined
class StrOnly:
def __str__(self):
return "I am str"
def test_str_fallback():
obj = StrOnly()
encoded = json_encode(obj)
assert '"I am str"' in encoded
# 5. Object with neither __str__ nor __repr__ working
class BrokenRepr:
def __repr__(self):
raise Exception("fail")
def test_repr_fallback():
obj = BrokenRepr()
encoded = json_encode(obj)
assert "<OBJECT BrokenRepr>" in encoded # extreme fallback
# 6. Object that uses str (which is repr)
class NotSerializable:
pass
def test_typeerror_fallback():
obj = NotSerializable()
encoded = json_encode(obj)
rprint(encoded)
assert ".NotSerializable object" in encoded
# -------------------------
# With Super Collections
# -------------------------
CITIES = 'Geneva', 'Lausanne', 'Bern', 'Zurich', 'Sankt-Gallen'
MIX1 = 'Foo', 1, datetime(2025, 9, 11, 14, 59), None, {'foo': 5, 'bar': 6}, date(2025, 9, 11)
MIX2 = {'Foo': 2, 'Bar': MIX1, 'Baz': CITIES, }
def test_simple():
"""
Test a simple super-collection
"""
tree = SuperList(CITIES)
t = tree.to_json()
rprint(t)
assert '"Geneva"' in t
print(tree.to_hjson())
def test_mix():
"""
Test mixed super-collection
"""
tree = SuperList(MIX1)
t = tree.to_json()
rprint(t)
assert '"2025-09-11' in t # startswith
assert 'null' in t
print(tree.to_hjson())
|