File: test_json_pointer.py

package info (click to toggle)
knot-resolver 6.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,392 kB
  • sloc: javascript: 42,732; ansic: 40,312; python: 12,616; cpp: 2,121; sh: 1,997; xml: 193; makefile: 181
file content (72 lines) | stat: -rw-r--r-- 2,171 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from pytest import raises

from knot_resolver.utils.modeling.json_pointer import json_ptr_resolve

# example adopted from https://www.sitepoint.com/json-server-example/
TEST = {
    "clients": [
        {
            "id": "59761c23b30d971669fb42ff",
            "isActive": True,
            "age": 36,
            "name": "Dunlap Hubbard",
            "gender": "male",
            "company": "CEDWARD",
            "email": "dunlaphubbard@cedward.com",
            "phone": "+1 (890) 543-2508",
            "address": "169 Rutledge Street, Konterra, Northern Mariana Islands, 8551",
        },
        {
            "id": "59761c233d8d0f92a6b0570d",
            "isActive": True,
            "age": 24,
            "name": "Kirsten Sellers",
            "gender": "female",
            "company": "EMERGENT",
            "email": "kirstensellers@emergent.com",
            "phone": "+1 (831) 564-2190",
            "address": "886 Gallatin Place, Fannett, Arkansas, 4656",
        },
        {
            "id": "59761c23fcb6254b1a06dad5",
            "isActive": True,
            "age": 30,
            "name": "Acosta Robbins",
            "gender": "male",
            "company": "ORGANICA",
            "email": "acostarobbins@organica.com",
            "phone": "+1 (882) 441-3367",
            "address": "697 Linden Boulevard, Sattley, Idaho, 1035",
        },
    ]
}


def test_json_ptr():
    parent, res, token = json_ptr_resolve(TEST, "")
    assert parent is None
    assert res is TEST

    parent, res, token = json_ptr_resolve(TEST, "/")
    assert parent is TEST
    assert res is None
    assert token == ""

    parent, res, token = json_ptr_resolve(TEST, "/clients/2/gender")
    assert parent is TEST["clients"][2]
    assert res == "male"
    assert token == "gender"

    with raises(ValueError):
        _ = json_ptr_resolve(TEST, "//")

    with raises(SyntaxError):
        _ = json_ptr_resolve(TEST, "invalid/ptr")

    with raises(ValueError):
        _ = json_ptr_resolve(TEST, "/clients/2/gender/invalid")

    parent, res, token = json_ptr_resolve(TEST, "/~01")
    assert parent is TEST
    assert res is None
    assert token == "~1"