Support Enum types
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import Optional, get_type_hints
|
||||
|
||||
from megasniff import SchemaDeflatorGenerator
|
||||
from src.megasniff import SchemaInflatorGenerator
|
||||
|
||||
|
||||
@@ -60,3 +64,36 @@ def test_optional():
|
||||
fn = infl.schema_to_inflator(C)
|
||||
c = fn({})
|
||||
assert c.a is None
|
||||
|
||||
|
||||
class AEnum(Enum):
|
||||
a = 'a'
|
||||
b = 'b'
|
||||
c = 42
|
||||
e1 = {'a': 'b'}
|
||||
e2 = ['a', 'b']
|
||||
|
||||
|
||||
@dataclass
|
||||
class Z:
|
||||
a: Optional[AEnum] = None
|
||||
|
||||
|
||||
def test_enum():
|
||||
infl = SchemaInflatorGenerator()
|
||||
defl = SchemaDeflatorGenerator()
|
||||
infl_fn = infl.schema_to_inflator(Z)
|
||||
defl_fn = defl.schema_to_deflator(Z)
|
||||
|
||||
for it in AEnum:
|
||||
ref = {'a': it.value}
|
||||
ref_str = json.dumps(ref)
|
||||
z = infl_fn(json.loads(ref_str))
|
||||
assert z.a is not None
|
||||
assert z.a.value == it.value
|
||||
assert z.a.name == it.name
|
||||
zdict = defl_fn(z)
|
||||
assert len(zdict) == 1
|
||||
assert zdict['a'] == it.value
|
||||
assert json.dumps(zdict) == ref_str
|
||||
assert infl_fn(zdict) == z
|
||||
|
||||
Reference in New Issue
Block a user