-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_git_json_tree.py
84 lines (69 loc) · 2.31 KB
/
test_git_json_tree.py
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
73
74
75
76
77
78
79
80
81
82
83
84
"""Test Git-JSON-Tree library."""
import json
import os
from string import printable
import hypothesis.strategies as st
import pytest
from click.testing import CliRunner
from dulwich.repo import Repo
from hypothesis import assume, given, settings
from git_json_tree import cli, decode, encode
@pytest.fixture()
def repo(tmpdir):
"""Create a repo."""
curdir = os.getcwd()
try:
os.chdir(str(tmpdir))
yield Repo.init_bare(str(tmpdir))
finally:
os.chdir(curdir)
@pytest.fixture()
def runner(repo):
"""Define a click runner."""
yield CliRunner()
def json_data(allow_nan=True):
"""Generate JSON data."""
return st.recursive(
st.none() |
st.booleans() |
st.integers() |
st.floats(allow_nan=allow_nan) |
st.text(printable),
lambda children:
st.lists(children) |
st.dictionaries(
st.text(
set(printable) - set('/'),
min_size=1,
),
children,
),
max_leaves=50,
)
@given(data=json_data())
@settings(max_examples=1000, deadline=10000)
def test_encode_decode(data, repo):
"""Test (d)encoding."""
assume(isinstance(data, (dict, list)))
repo_data = json.dumps(decode(repo, encode(repo, data)), sort_keys=True)
assert repo_data == json.dumps(data, sort_keys=True)
@given(data=json_data(allow_nan=False))
@settings(max_examples=100, deadline=10000)
def test_cli_encoder(data, runner):
"""Test cli encoder."""
assume(isinstance(data, (dict, list)) and data)
encoded = runner.invoke(cli, ['encode'], input=json.dumps(data))
assert encoded.exit_code == 0
decoded = runner.invoke(cli, ['decode', encoded.output.strip()])
assert decoded.exit_code == 0
assert json.loads(decoded.output_bytes.decode('utf-8')) == data
@given(data=json_data(allow_nan=False))
@settings(max_examples=100, deadline=10000)
def test_smudge_clean(data, runner):
"""Test Git integration."""
assume(isinstance(data, (dict, list)) and data)
cleaned = runner.invoke(cli, ['clean'], input=json.dumps(data))
assert cleaned.exit_code == 0
smudged = runner.invoke(cli, ['smudge'], input=cleaned.output)
assert smudged.exit_code == 0
assert json.loads(smudged.output_bytes.decode('utf-8')) == data