Exemple #1
0
def test_type_check_int():
    try:
        jsontofu.decode(
            '''{"test_str": "test", "test_list": [{"test_str": "abc", "test_int": ""}]}''',
            ListData)
    except:
        assert True
    else:
        assert False
Exemple #2
0
    async def get_devices(
            self,
            page: Optional[int] = 1,
            per_page: Optional[int] = 10,
            sort: Optional[str] = None,
            has_group: Optional[bool] = None,
            attributes: Optional[Dict[str,
                                      Any]] = None) -> AsyncIterable[Device]:
        """
        List devices

        GET /devices

        Usage:

        ```
        async for i in mender.get_devices():
            print(i)
        ```

        :param int page: Starting page.
        :param int per_page: Number of results per page.
        :param str sort: Supports sorting the device list by attribute values.  The parameter is formatted as a list of attribute names and sort directions, e.g.:  '?sort=attr1:asc, attr2:desc'  will sort by 'attr1' ascending, and then by 'attr2' descending. 'desc' is the default sort direction, and can be omitted.
        :param bool has_group: If present, limits the results only to devices assigned/not assigned to a group.

        :return: AsyncIterable[Device]
        """
        async for res in self._get_devicesList(page=page,
                                               per_page=per_page,
                                               sort=sort,
                                               has_group=has_group,
                                               attributes=attributes):
            for device in await res.json():
                yield jsontofu.decode(device, Device)
Exemple #3
0
def test_invalid():
    try:
        obj = jsontofu.decode('''{''', Data)
    except:
        assert True
    else:
        assert False
Exemple #4
0
def test_dict_obj():
    obj = jsontofu.decode(
        '''{"account": {"user_id": "12", "account_id": "34", "location_id": "56", "group_id": "78"}}''',
        UserInfo)

    assert obj == UserInfo(account=UserAccount(
        user_id='12', account_id='34', location_id='56', group_id='78'))
Exemple #5
0
def test_list():
    obj = jsontofu.decode(
        '''{"test_str": "test", "test_list": [{"test_str": "abc", "test_int": 11}]}''',
        ListData)

    assert obj == ListData(test_str="test",
                           test_list=[Data(test_str="abc", test_int=11)])
Exemple #6
0
def test_dict_nokey():
    obj = jsontofu.decode(
        '''{"test_str": "test",
                              "data": {"test_str": "abc", "test_int": 11, "more_key": 22}}''',
        OptionalData)

    assert not hasattr(obj.data, 'more_key')
Exemple #7
0
def test_optional2():
    obj = jsontofu.decode(
        '''{"test_str": "test", "data":{"test_str": "nm", "test_int": 1}}''',
        OptionalData)

    assert obj == OptionalData(test_str='test',
                               data=Data(test_str='nm', test_int=1))
Exemple #8
0
    async def get_devices_paged(
            self,
            page: Optional[int] = 1,
            per_page: Optional[int] = 10,
            sort: Optional[str] = None,
            has_group: Optional[bool] = None,
            attributes: Optional[Dict[str, Any]] = None) -> Iterable[Device]:
        """
        List devices

        GET /devices


        :param int page: Starting page.
        :param int per_page: Number of results per page.
        :param str sort: Supports sorting the device list by attribute values.  The parameter is formatted as a list of attribute names and sort directions, e.g.:  '?sort=attr1:asc, attr2:desc'  will sort by 'attr1' ascending, and then by 'attr2' descending. 'desc' is the default sort direction, and can be omitted.
        :param bool has_group: If present, limits the results only to devices assigned/not assigned to a group.

        :return: Iterable[Device]
        """
        res = await self._get_devices_paged(page=page,
                                            per_page=per_page,
                                            sort=sort,
                                            has_group=has_group,
                                            attributes=attributes)
        return map(lambda x: jsontofu.decode(x, Device), await res.json())
Exemple #9
0
def test_list_nokey():
    obj = jsontofu.decode(
        '''{"test_str": "test",
                              "test_list": [{"test_str": "abc", "test_int": 11, "more_key": 22}]}''',
        ListData)

    assert not hasattr(obj.test_list[0], 'more_key')
    assert obj == ListData(test_str="test",
                           test_list=[Data(test_str="abc", test_int=11)])
Exemple #10
0
def test_optional_empty():
    obj = jsontofu.decode('''{"test_str": "test", "data": {}}''', OptionalData)
    # best spec
    #assert obj != OptionalData(test_str="test")
    #assert obj == OptionalData(test_str="test", data={})
    #assert obj != OptionalData(test_str="test", data=None)
    # ok spec
    assert obj == OptionalData(test_str="test")
    assert obj != OptionalData(test_str="test", data={})
    assert obj == OptionalData(test_str="test", data=None)
Exemple #11
0
def test_meta():
    obj = jsontofu.decode(
        '''{"type": "built_in", "func": "memory_info", "args": [1, 2, 3], "kwargs": {"a": "123"}, "rtype": "json", "concurrent": false}''',
        Meta)
    assert obj == Meta(type='built_in',
                       func='memory_info',
                       rtype='json',
                       concurrent=False,
                       args=[1, 2, 3],
                       kwargs={'a': '123'})
    obj = jsontofu.decode(
        '''{"type": "built_in", "func": "memory_info", "args": [1, 2, 3], "rtype": "json", "concurrent": false}''',
        Meta)
    assert obj == Meta(type='built_in',
                       func='memory_info',
                       rtype='json',
                       concurrent=False,
                       args=[1, 2, 3],
                       kwargs=None)
    obj = jsontofu.decode(
        '''{"type": "built_in", "func": "memory_info", "kwargs": {"a": "123"}, "rtype": "json", "concurrent": false}''',
        Meta)
    assert obj == Meta(type='built_in',
                       func='memory_info',
                       rtype='json',
                       concurrent=False,
                       args=None,
                       kwargs={'a': '123'})
    obj = jsontofu.decode(
        '''{"type": "built_in", "func": "memory_info", "args": [], "kwargs": {}, "rtype": "json", "concurrent": false}''',
        Meta)
    assert obj == Meta(type='built_in',
                       func='memory_info',
                       rtype='json',
                       concurrent=False,
                       args=[],
                       kwargs=None)
Exemple #12
0
def test_no_dataclass():
    obj = jsontofu.decode(
        '''{
      "ty": "shell",
      "func": "memory_info",
      "args": [],
      "kwargs": {},
      "rtype": "string",
      "concurrent": true
    }''', MetaNone)

    obj2 = jsontofu.decode(
        '''{
      "ty": "built_in",
      "func": "memory_info",
      "cmd": "aux",
      "args": [],
      "kwargs": {},
      "rtype": "json",
      "path": "/root/home",
      "concurrent": true
    }''', MetaNone)

    assert obj.cmd == None
    assert obj.func == obj2.func
    assert obj.args == obj2.args
    assert obj.kwargs == None
    assert obj.ty == "shell"
    assert obj.rtype == "string"
    assert obj.concurrent == True
    assert obj.path != "/root/home"

    assert obj2.cmd == "aux"
    assert obj2.path == "/root/home"
    assert obj2.rtype == "json"
    assert obj2.rtype != obj.rtype
    assert obj2.ty == "built_in"
Exemple #13
0
def test_nested():
    obj = jsontofu.decode('''{"test_str": "data", "data": {}}''', NestedData)
    # best spec
    #try:
    #    obj != NestedData(test_str="test", data=Data(test_str="test", test_int=0))
    #except:
    #    assert True
    #else:
    #    assert False
    # ok spec
    assert obj != NestedData(test_str="data",
                             data=Data(test_str="test", test_int=0))
    assert obj == NestedData(test_str="data", data=None)
    assert obj != NestedData(test_str="data")
    assert True
Exemple #14
0
def test_none():
    obj = jsontofu.decode('''{"test_str": null, "test_int": null}''', Data)
    assert obj.test_str is None
    assert obj.test_int is None
Exemple #15
0
def test_encode():
    frozen = jsontofu.encode(
        jsontofu.decode('''{"test_str": "test", "test_int": 123}''', Data))
    assert frozen == {"test_str": "test", "test_int": 123}

    frozen = jsontofu.encode(
        jsontofu.decode(
            '''{"account": {"user_id": "12", "account_id": "34", "location_id": "56", "group_id": "78"}}''',
            UserInfo))
    assert frozen == {
        "account": {
            "user_id": "12",
            "account_id": "34",
            "location_id": "56",
            "group_id": "78"
        }
    }

    frozen = jsontofu.encode(
        jsontofu.decode(
            '''{"test_str": "test", "test_list": [{"test_str": "abc", "test_int": 11}]}''',
            ListData))
    assert frozen == {
        "test_str": "test",
        "test_list": [{
            "test_str": "abc",
            "test_int": 11
        }]
    }

    frozen = jsontofu.encode(
        jsontofu.decode(
            '''{"test_str": "test", "test_list": ["1", 1, true]}''',
            ListData2))
    assert frozen == {"test_str": "test", "test_list": ["1", 1, True]}

    frozen = jsontofu.encode(
        jsontofu.decode(
            '''{"test_str": "test", "data":{"test_str": "nm", "test_int": 1}}''',
            OptionalData))
    assert frozen == {
        "test_str": "test",
        "data": {
            "test_str": "nm",
            "test_int": 1
        }
    }

    frozen = jsontofu.encode(
        jsontofu.decode(
            '''{"type": "built_in", "func": "memory_info", "args": [1, 2, 3], "kwargs": {"a": "123"}, "rtype": "json", "concurrent": false}''',
            Meta))
    assert frozen == {
        "type": "built_in",
        "func": "memory_info",
        "args": [1, 2, 3],
        "kwargs": {
            "a": "123"
        },
        "rtype": "json",
        "concurrent": False
    }
Exemple #16
0
def test_nokey():
    obj = jsontofu.decode(
        '''{"type": "built_in", "func": "memory_info", "rtype": "json", "concurrent": false}''',
        MetaLostKey)
    assert obj.type == "built_in"
    assert not hasattr(obj, 'concurrent')
Exemple #17
0
def test_zero_typr():
    obj = jsontofu.decode({"test_flo": 0, "test_int": 0}, FloatKey)
    assert obj == FloatKey(test_flo=0, test_int=0)
    obj = jsontofu.decode({"test_flo": 0.22, "test_int": -100}, FloatKey)
    assert obj == FloatKey(test_flo=0.22, test_int=-100)
Exemple #18
0
def test_list2():
    obj = jsontofu.decode(
        '''{"test_str": "test", "test_list": ["1", 1, true]}''', ListData2)
    assert obj == ListData2(test_str="test", test_list=["1", 1, True])
Exemple #19
0
def test_int_str():
    obj = jsontofu.decode('''{"test_str": "test", "test_int": 123}''', Data)

    assert obj.test_str == 'test'
    assert obj.test_int == 123
Exemple #20
0
def test_empty():
    obj = jsontofu.decode('''{}''', Data)

    assert obj is None
Exemple #21
0
def test_normal_dict():
    obj = jsontofu.decode(
        '''{"test_str": "data", "test_dict": {"g": "h", "v": "w"}}''',
        DictData)

    assert obj == DictData(test_str="data", test_dict={"g": "h", "v": "w"})
Exemple #22
0
def test_optional():
    obj = jsontofu.decode('''{"test_str": "test"}''', OptionalData)
    obj2 = OptionalData(test_str="test")

    assert obj == obj2