Create toggle for strict-mode inflate
This commit is contained in:
75
tests/test_strict_mode.py
Normal file
75
tests/test_strict_mode.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pytest
|
||||
|
||||
from megasniff import SchemaInflatorGenerator
|
||||
from megasniff.exceptions import FieldValidationException
|
||||
|
||||
|
||||
def test_global_strict_mode_basic():
|
||||
class A:
|
||||
def __init__(self, a: int):
|
||||
self.a = a
|
||||
|
||||
infl = SchemaInflatorGenerator(strict_mode=True)
|
||||
fn = infl.schema_to_inflator(A)
|
||||
a = fn({'a': 42})
|
||||
|
||||
assert a.a == 42
|
||||
|
||||
with pytest.raises(FieldValidationException):
|
||||
fn({'a': 42.0})
|
||||
|
||||
|
||||
def test_global_strict_mode_basic_override():
|
||||
class A:
|
||||
def __init__(self, a: int):
|
||||
self.a = a
|
||||
|
||||
infl = SchemaInflatorGenerator(strict_mode=False)
|
||||
fn = infl.schema_to_inflator(A, strict_mode_override=True)
|
||||
a = fn({'a': 42})
|
||||
|
||||
assert a.a == 42
|
||||
|
||||
with pytest.raises(FieldValidationException):
|
||||
fn({'a': 42.0})
|
||||
|
||||
|
||||
def test_global_strict_mode_list():
|
||||
@dataclass
|
||||
class A:
|
||||
a: list[int]
|
||||
|
||||
infl = SchemaInflatorGenerator(strict_mode=True)
|
||||
fn = infl.schema_to_inflator(A)
|
||||
a = fn({'a': [42]})
|
||||
|
||||
assert a.a == [42]
|
||||
|
||||
with pytest.raises(FieldValidationException):
|
||||
fn({'a': [42.0, 42]})
|
||||
|
||||
|
||||
def test_global_strict_mode_circular():
|
||||
@dataclass
|
||||
class A:
|
||||
a: list[int]
|
||||
|
||||
@dataclass
|
||||
class B:
|
||||
b: list[A | int]
|
||||
|
||||
infl = SchemaInflatorGenerator(strict_mode=True)
|
||||
fn = infl.schema_to_inflator(B)
|
||||
b = fn({'b': [{'a': []}, 42]})
|
||||
|
||||
assert len(b.b) == 2
|
||||
assert isinstance(b.b[0], A)
|
||||
assert isinstance(b.b[1], int)
|
||||
|
||||
with pytest.raises(FieldValidationException):
|
||||
fn({'b': [42.0]})
|
||||
|
||||
with pytest.raises(FieldValidationException):
|
||||
fn({'b': [{'a': [1.1]}]})
|
||||
Reference in New Issue
Block a user