File: test_json.py

package info (click to toggle)
sbws 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,084 kB
  • sloc: python: 10,432; sh: 146; makefile: 38
file content (65 lines) | stat: -rw-r--r-- 1,447 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
"""json.py unit tests."""

import json
import os.path

from sbws.util.json import CustomDecoder, CustomEncoder, load_ignore_errors

STATE = """{
    "min_perc_reached": null,
    "recent_consensus_count": [
        "2020-03-04T10:00:00",
        "2020-03-05T10:00:00",
        "2020-03-06T10:00:00"
    ],
    "recent_measurement_attempt": [
        [
            "2020-03-04T10:00:00",
            2
        ],
        [
            "2020-03-05T10:00:00",
            2
        ],
        [
            "2020-03-06T10:00:00",
            2
        ]
    ],
    "recent_priority_list": [
        "2020-03-04T10:00:00",
        "2020-03-05T10:00:00",
        "2020-03-06T10:00:00"
    ],
    "recent_priority_relay": [
        [
            "2020-03-04T10:00:00",
            2
        ],
        [
            "2020-03-05T10:00:00",
            2
        ],
        [
            "2020-03-06T10:00:00",
            2
        ]
    ],
    "scanner_started": "2020-03-14T16:15:22",
    "uuid": "x"
}"""


def test_decode_encode_roundtrip():
    d = json.loads(STATE, cls=CustomDecoder)
    s = json.dumps(d, cls=CustomEncoder, indent=4, sort_keys=True)
    assert s == STATE


def test_read_bad_json(tmpdir):
    invalidjsonstr = "{"
    invalidjsonpath = os.path.join(str(tmpdir), "invalid.json")
    with open(invalidjsonpath, "w") as fd:
        fd.write(invalidjsonstr)
    jsonstr = load_ignore_errors(invalidjsonpath)
    assert jsonstr == {}