Example #1
0
def test_dot_access_nested():
    jd = JSON({"socket.io": {"key": "value", "two": 2}})
    expected_dot_keys_list = [('socket.io', 'key'), ('socket.io', 'two')]
    assert jd.dot_keys_list() == expected_dot_keys_list
    assert jd.dot_items_list() == [
        (('socket.io', 'key'), 'value'),
        (('socket.io', 'two'), 2),
    ]
    with pytest.raises(AttributeError):
        socketiothing = jd.socket.io
def test_dataclass_stringify():
    a = JsonObj(**{"a": 'c', "herm": 123})

    from dataclasses import dataclass

    @dataclass()
    class DataThing:
        n: int
        s: str

    data = DataThing(n=1, s='stringy')
    data_string = JSON.stringify(data)
    assert data_string == '{"n":1,"s":"stringy"}'
    a.data = data
    nested_str = JSON.stringify(a)
    assert nested_str == '{"a":"c","herm":123,"data":{"n":1,"s":"stringy"}}'
Example #3
0
def test_lookup_ops():
    data = {
        "key": "value",
        "list": [1, 2, 3, 4, 5],
        "dt": datetime.datetime(1970, 1, 1, 0, 0, 0, 1),
        "sub": {
            'b': 3,
            'key': 'val',
            'a': 1,
        },
        "timedelta": datetime.timedelta(days=2),
    }

    d = JSON(data)

    assert d.dot_lookup('sub.key') == 'val'
    assert d.dot_lookup(('sub', 'key')) == 'val'
    assert 'val' == d['sub.key']
    assert 'val' == d['sub', 'key']
    assert 'val' == d[('sub', 'key')]
Example #4
0
def _JSON(self: Response, **kwargs: Any) -> Any:
    return JSON(self.json())  # type: ignore
Example #5
0
def test_dot_items():
    jd: JsonObj = JSON(data)

    expected = [
        (('id', ), 1),
        (('code', ), None),
        (('subd', 'a'), 23),
        (('subd', 'b', 'herm'), 2),
        (('type', ), 'foo'),
        (('root', 'string_list'), ['one', 'two', 'octopus', 'what_is_up']),
        (('root', 'mixed_dict', 'num'), 123),
        (('root', 'mixed_dict', 'obj', 'k'), 'v'),
        (('root', 'mixed_dict', 'list'), ['s', 123,
                                          JsonObj(**{'k': 'v'})]),
        (
            ('bars', ),
            [
                JsonObj(**{'id': 6934900}),
                JsonObj(**{'id': 6934977}),
                JsonObj(**{'id': 6934992}),
                JsonObj(**{'id': 6934993}),
                JsonObj(**{'id': 6935014}),
            ],
        ),
        (('n', ), 10),
        (('date_str', ), '2013-07-08 00:00:00'),
        (('float_here', ), 0.454545),
        (
            ('complex', ),
            [
                JsonObj(**{
                    'id': 83865,
                    'goal': Decimal('2.000000'),
                    'state': 'active'
                })
            ],
        ),
        (('profile_id', ), None),
        (('state', ), 'active'),
    ]
    expected_strings = [
        ('id', 1),
        ('code', None),
        ('subd.a', 23),
        ('subd.b.herm', 2),
        ('type', 'foo'),
        ('root.string_list', ['one', 'two', 'octopus', 'what_is_up']),
        ('root.mixed_dict.num', 123),
        ('root.mixed_dict.obj.k', 'v'),
        ('root.mixed_dict.list', ['s', 123, JsonObj(**{'k': 'v'})]),
        (
            'bars',
            [
                JsonObj(**{'id': 6934900}),
                JsonObj(**{'id': 6934977}),
                JsonObj(**{'id': 6934992}),
                JsonObj(**{'id': 6934993}),
                JsonObj(**{'id': 6935014}),
            ],
        ),
        ('n', 10),
        ('date_str', '2013-07-08 00:00:00'),
        ('float_here', 0.454545),
        (
            'complex',
            [
                JsonObj(**{
                    'id': 83865,
                    'goal': Decimal('2.000000'),
                    'state': 'active'
                })
            ],
        ),
        ('profile_id', None),
        ('state', 'active'),
    ]

    dkl = jd.dot_items_list()
    assert expected == dkl

    # from time import time
    # ta = time()
    # # for i in range(10):
    # for i in range(100):
    #     a = list(jd.dot_items_chain())
    # tb = time()
    # for i in range(100):
    #     b = list(jd.dot_items_chain2())
    # tc = time()
    # for i in range(100):
    #     c = list(jd.dot_items())
    # td = time()
    # print('yielding', tc-tb,'-- chain: ', tb-ta, '-- og: ', td-tc)
    expected_json_str = JSON.stringify(expected, sort_keys=True, pretty=True)
    output_json_str = JSON.stringify(dkl, sort_keys=True, pretty=True)
    assert output_json_str == expected_json_str
    try:
        from deepdiff import DeepDiff

        print('deepdiff', DeepDiff(expected, dkl))
        assert not DeepDiff(expected, dkl)
    except ModuleNotFoundError as mnfe:
        print(mnfe)
Example #6
0
def test_dot_access_attr_vs_item():
    jd = JSON({"socket.io": "data"})
    assert jd['socket.io'] == "data"
    with pytest.raises(AttributeError):
        socketiothing = jd.socket.io
Example #7
0
 def render(self, content: Any) -> bytes:
     """Return JSON string for content as bytes"""
     return JSON.binify(data=content)