Esempio n. 1
0
    class S(Supermodel):
        a = Attribute(str)
        b = Attribute(int)

        @staticmethod
        async def _find(**kwargs) -> AsyncIterator[dict]:
            return SIter(**kwargs)
Esempio n. 2
0
    class S1(Supermodel):
        a = Attribute(str)
        b = Attribute(Optional[int])

        @staticmethod
        async def _create(raw: dict):
            return raw
Esempio n. 3
0
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)
Esempio n. 4
0
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
Esempio n. 5
0
    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()}
Esempio n. 6
0
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='-')
Esempio n. 7
0
    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}
Esempio n. 8
0
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
Esempio n. 9
0
    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
Esempio n. 10
0
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
Esempio n. 11
0
    class S(Supermodel):
        _ttl = 0.02

        a = Attribute(str)

        @staticmethod
        async def _get(id_: str) -> Optional[dict]:
            await sleep(0.01)
            return
Esempio n. 12
0
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')
Esempio n. 13
0
    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
Esempio n. 14
0
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)
Esempio n. 15
0
def test_min():
    with raises(TypeError):
        Attribute(str, min=type)

    assert Attribute(str, min=0).min == 0
    assert Attribute(str, min='000000').min == '000000'
Esempio n. 16
0
 class M1(Model):
     a = Attribute(int)
     b = Attribute(M2)
Esempio n. 17
0
def test_type():
    with raises(TypeError):
        # noinspection PyTypeChecker
        Attribute('123')

    assert Attribute(str).type == str
Esempio n. 18
0
 class M1(Model):
     a = Attribute(int)
     b = Attribute(str)
     foo = Attribute(Foo)
Esempio n. 19
0
def test_case_sensitive_name():
    a = Attribute(Any, case_insensitive=False)
    assert a.ciname is None
Esempio n. 20
0
 class M1(Model):
     foo = Attribute(int)
Esempio n. 21
0
 class M2(Model):
     x = Attribute(str)
Esempio n. 22
0
 class M3(Model):
     lst = Attribute(List[M2])
Esempio n. 23
0
class Params(Model):
    a = Attribute(str)
    b = Attribute(Optional[int])
    c = Attribute(C)
Esempio n. 24
0
class C(Model):
    x = Attribute(float)
    y = Attribute(float)
Esempio n. 25
0
 class M(Model):
     a = Attribute(str)
     b = Attribute(str)
Esempio n. 26
0
 class M2(Model):
     foo = Attribute(bool)
Esempio n. 27
0
def test_max():
    with raises(TypeError):
        Attribute(str, max=type)

    assert Attribute(str, max=0).max == 0
    assert Attribute(str, max='FFFFFF').max == 'FFFFFF'
Esempio n. 28
0
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)
Esempio n. 29
0
 class M(Model):
     foo = Attribute(int)
     bar = Attribute(Optional[int])
Esempio n. 30
0
 class M4(Model):
     key = Attribute(M1)
     val = Attribute(Dict[str, M3])