-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_callback.py
More file actions
65 lines (46 loc) · 1.37 KB
/
test_callback.py
File metadata and controls
65 lines (46 loc) · 1.37 KB
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
import pytest
import csonpath
import sys
def split_line(parent, idx, cur, data):
parent[idx] = cur.splitlines()
def test_callback():
o = csonpath.CsonPath("$.c")
d = {"c": "new\nline"}
o.callback(d, split_line)
assert type(d["c"]) == list
assert d["c"][0] == "new"
assert d["c"][1] == "line"
VALUE_REF: int = 0
def set_data(parent, idx, cur, data):
parent[idx] = data
@pytest.mark.skipif(
not hasattr(sys, 'getrefcount'),
reason="refcount not available on PyPy"
)
def test_callback_data():
o = csonpath.CsonPath("$.c")
d = {"c": "new\nline"}
value = [123]
global VALUE_REF
VALUE_REF = sys.getrefcount(value)
assert VALUE_REF > 0
o.callback(d, set_data, value)
assert VALUE_REF == sys.getrefcount(value) - 1
assert d["c"] == [123]
def syncronyse_in_out(source_path, json_in, json_out):
if json_in is json_out:
return
jp = csonpath.CsonPath(source_path)
all = jp.find_all(json_in)
d = [all, 0]
def syn_cp(parent, idx, cur, data):
parent[idx] = data[0][data[1]]
data[1] += 1
jp.update_or_create_callback(json_out, syn_cp, d)
def test_callback_more():
d = {"a": "wololo\nayoyoyo\nw0l0l0"}
e = {"a": []}
jp = csonpath.CsonPath("$.a")
syncronyse_in_out("$.a", d, e)
jp.callback(e, split_line)
assert e == {"a": ["wololo", "ayoyoyo", "w0l0l0"]}