class S(Supermodel): a = Attribute(str) b = Attribute(int) @staticmethod async def _find(**kwargs) -> AsyncIterator[dict]: return SIter(**kwargs)
class S1(Supermodel): a = Attribute(str) b = Attribute(Optional[int]) @staticmethod async def _create(raw: dict): return raw
def test_strict(): assert Attribute(str).strict is False assert Attribute(str, strict=True).strict is True assert Attribute(str, strict=False).strict is False with raises(TypeError): # noinspection PyTypeChecker Attribute(str, strict=5)
def test_name(): name = 'a' a = Attribute(Any) a.name = name assert a.name == name assert a.private_name == '.' + name with raises(TypeError): Attribute(Any).name = 123
class S(Supermodel): _ttl = 0.01 a = Attribute(str) b = Attribute(float) @staticmethod async def _get(id_: str) -> Optional[dict]: return {'a': id_, 'b': time()}
def test_case_insensitive(): a = Attribute(Any) a.name = 'some_name' assert 'some-name' in set(a.ciname.cases()) a.case_insensitive = False assert a.ciname is None with raises(TypeError): # noinspection PyTypeChecker Attribute(str, case_insensitive='-')
class S1(Supermodel): a = Attribute(str) b = Attribute(Optional[int]) @staticmethod async def _create(raw: dict): return raw @staticmethod async def _get(id_: str): if id_ == 'f': return {'a': 'f', 'b': 123}
def test_eq(): a1 = Attribute(Any) a1.name = 'a' a2 = Attribute(Any) a2.name = 'a' b = Attribute(Any) b.name = 'b' assert a1 == a2 assert hash(a1) == hash(a2) assert a1 != b assert hash(a1) != hash(b) # noinspection PyTypeChecker assert a1.__eq__('a') is NotImplemented
class S(Supermodel): some_attr = Attribute(str) some_ci_attr = Attribute(str) @staticmethod async def _create(raw: dict): return raw @staticmethod async def _get(id_: str) -> Optional[dict]: pass @staticmethod async def _update(id_: str, raw: dict): pass
def test_without_parameters(): a = Attribute(Any) assert a.type is Any assert a.strict is False assert a.default is UNSET assert a.min is UNSET assert a.max is UNSET assert a.case_insensitive is True
class S(Supermodel): _ttl = 0.02 a = Attribute(str) @staticmethod async def _get(id_: str) -> Optional[dict]: await sleep(0.01) return
class _Jsonrpc(Model): jsonrpc = Attribute(str, strict=True, default='2.0') def __init__(self, *args, **kwargs): if args and args[0] != '2.0': args = ('2.0',) + args super().__init__(*args, **kwargs) if self.jsonrpc != '2.0': raise ModelValueError('MUST be exactly "2.0"', model=type(self).__name__, attr='jsonrpc')
class S(Supermodel): _ttl = 0.01 a = Attribute(str) @staticmethod async def _create(raw: dict): return raw @staticmethod async def _get(id_: str) -> Optional[dict]: pass
def test_default(): assert Attribute(Optional[str], default='').default == '' assert Attribute(Optional[str], default=5).default == '5' assert Attribute(Optional[str], default=None).default is None with raises(ValueError): Attribute(int, default='a') with raises(TypeError): Attribute(int, default='a', strict=True) with raises(ValueError): Attribute(int, default=0, min=1) with raises(ValueError): Attribute(int, default=1000, max=999) with raises(ValueError): Attribute(str, default='a', min=2) with raises(ValueError): Attribute(str, default='abc', max=2)
def test_min(): with raises(TypeError): Attribute(str, min=type) assert Attribute(str, min=0).min == 0 assert Attribute(str, min='000000').min == '000000'
class M1(Model): a = Attribute(int) b = Attribute(M2)
def test_type(): with raises(TypeError): # noinspection PyTypeChecker Attribute('123') assert Attribute(str).type == str
class M1(Model): a = Attribute(int) b = Attribute(str) foo = Attribute(Foo)
def test_case_sensitive_name(): a = Attribute(Any, case_insensitive=False) assert a.ciname is None
class M1(Model): foo = Attribute(int)
class M2(Model): x = Attribute(str)
class M3(Model): lst = Attribute(List[M2])
class Params(Model): a = Attribute(str) b = Attribute(Optional[int]) c = Attribute(C)
class C(Model): x = Attribute(float) y = Attribute(float)
class M(Model): a = Attribute(str) b = Attribute(str)
class M2(Model): foo = Attribute(bool)
def test_max(): with raises(TypeError): Attribute(str, max=type) assert Attribute(str, max=0).max == 0 assert Attribute(str, max='FFFFFF').max == 'FFFFFF'
def test_ci_name(name: str, ci_names: List[str]): a = Attribute(Any) a.name = name cases = set(a.ciname.cases()) assert a.name == name assert all(n in cases for n in ci_names)
class M(Model): foo = Attribute(int) bar = Attribute(Optional[int])
class M4(Model): key = Attribute(M1) val = Attribute(Dict[str, M3])