|
1 | 1 | import json |
2 | | -import unittest |
3 | 2 |
|
4 | 3 | import yaml |
5 | 4 |
|
|
8 | 7 | from tests.test_loaders_dumpers.models.enum_model import Organism, StateEnum |
9 | 8 |
|
10 | 9 |
|
11 | | -class EnumTestCase(unittest.TestCase): |
12 | | - def test_enum(self): |
13 | | - """ |
14 | | - Tests that enums are encoded as json correctly |
15 | | -
|
16 | | - * https://github.com/linkml/linkml/issues/337 |
17 | | - * https://github.com/linkml/linkml/issues/119 |
18 | | - """ |
19 | | - i = Organism(state="LIVING") |
20 | | - print(i) |
21 | | - print(i.state) |
22 | | - print(i.state.code) |
23 | | - print(i.state.code.text) |
24 | | - print(type(i.state)) |
25 | | - print(StateEnum.LIVING) |
26 | | - assert str(i.state) == "LIVING" |
27 | | - assert i.state.code == StateEnum.LIVING |
28 | | - obj = json.loads(json_dumper.dumps(i)) |
29 | | - assert obj["state"] == "LIVING" |
30 | | - obj = yaml.safe_load(yaml_dumper.dumps(i)) |
31 | | - assert obj["state"] == "LIVING" |
32 | | - reconstituted = json_loader.loads(json_dumper.dumps(i), target_class=Organism) |
33 | | - print(f"RECONSTITUTED = {reconstituted}") |
34 | | - assert reconstituted.state.code == StateEnum.LIVING |
35 | | - |
36 | | - |
37 | | -if __name__ == "__main__": |
38 | | - unittest.main() |
| 10 | +def test_enum(): |
| 11 | + """ |
| 12 | + Tests that enums are encoded as json correctly |
| 13 | +
|
| 14 | + * https://github.com/linkml/linkml/issues/337 |
| 15 | + * https://github.com/linkml/linkml/issues/119 |
| 16 | + """ |
| 17 | + i = Organism(state="LIVING") |
| 18 | + print(i) |
| 19 | + print(i.state) |
| 20 | + print(i.state.code) |
| 21 | + print(i.state.code.text) |
| 22 | + print(type(i.state)) |
| 23 | + print(StateEnum.LIVING) |
| 24 | + |
| 25 | + # Test string representation and enum code |
| 26 | + assert str(i.state) == "LIVING" |
| 27 | + assert i.state.code == StateEnum.LIVING |
| 28 | + |
| 29 | + # Test JSON dumping/loading |
| 30 | + obj = json.loads(json_dumper.dumps(i)) |
| 31 | + assert obj["state"] == "LIVING" |
| 32 | + |
| 33 | + # Test YAML dumping |
| 34 | + obj = yaml.safe_load(yaml_dumper.dumps(i)) |
| 35 | + assert obj["state"] == "LIVING" |
| 36 | + |
| 37 | + # Test round-trip JSON serialization |
| 38 | + reconstituted = json_loader.loads(json_dumper.dumps(i), target_class=Organism) |
| 39 | + print(f"RECONSTITUTED = {reconstituted}") |
| 40 | + assert reconstituted.state.code == StateEnum.LIVING |
0 commit comments