Skip to content

Commit

Permalink
tests reorganized
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Feb 11, 2020
1 parent e0fefe0 commit bbe34b0
Show file tree
Hide file tree
Showing 122 changed files with 1,962 additions and 2,634 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,3 @@ ENV/

# other
*.swp
*.got
30 changes: 23 additions & 7 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ def encode(self, data):
return data


def test_abstract_dumper_encode():
with pytest.raises(NotImplementedError):
cli.Dumper().encode('data')


def test_abstract_loader_decode():
with pytest.raises(NotImplementedError):
cli.Loader().decode('data')


def test_dumper_dump_default_with_tty(stringio_tty):
dumper = Dumper()
dumper.dump(stringio_tty, 'text')
Expand Down Expand Up @@ -71,21 +81,27 @@ class FakeFP(object):
'yml': 'yaml',
}
fake_fp = FakeFP()
app = cli.App(args=())

for ext in sorted(aliases):
fake_fp.name = 'filename.' + ext
assert aliases[ext] == app.guess_fmt(fake_fp, 'default')
assert aliases[ext] == cli.App(args=()).guess_fmt(fake_fp, 'default')


def test_guess_fmt_ignore_fp_defaults():
app = cli.App(args=())
for fp in sys.stdin, sys.stdout, sys.stderr:
assert 'default' == app.guess_fmt(fp, 'default')
assert 'default' == cli.App(args=()).guess_fmt(fp, 'default')


def test_get_loader_unsupported_fmt():
app = cli.App(args=())
def test_get_dumper_unsupported_fmt():
with pytest.raises(RuntimeError, match='Unsupported output format: garbage'):
cli.App(args=()).get_dumper('garbage')


def test_get_loader_unsupported_fmt():
with pytest.raises(RuntimeError, match='Unsupported input format: garbage'):
app.get_loader('garbage')
cli.App(args=()).get_loader('garbage')


def test_run():
with pytest.raises(NotImplementedError):
cli.App(args=()).run()
7 changes: 4 additions & 3 deletions tests/cli/test_diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ def test_enable_U_ops(capsys, expected, fullname):
assert expected == captured.out


def test_output_file(capsys, expected, fullname, testfile):
def test_output_file(capsys, content, expected, fullname, tmp_path):
result_file_name = '{}.got'.format(tmp_path)
nested_diff.diff_tool.App(args=(
fullname('lists.a.json', shared=True),
fullname('lists.b.json', shared=True),
'--ofmt', 'json',
'--out', fullname('got'),
'--out', result_file_name,
)).run()

captured = capsys.readouterr()
assert '' == captured.err
assert '' == captured.out

assert json.loads(expected) == json.loads(testfile('got'))
assert json.loads(expected) == json.loads(content(result_file_name))


def test_json_ofmt_opts(capsys, expected, fullname):
Expand Down
36 changes: 20 additions & 16 deletions tests/cli/test_patch_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import nested_diff.patch_tool


def test_default_patch(capsys, content, fullname):
def test_default_patch(capsys, content, fullname, tmp_path):
result_file_name = '{}.got.json'.format(tmp_path)
copyfile(
fullname('lists.a.json', shared=True),
fullname('got.json'),
result_file_name,
)
nested_diff.patch_tool.App(args=(
fullname('got.json'),
result_file_name,
fullname('lists.patch.yaml', shared=True),
)).run()

Expand All @@ -22,16 +23,17 @@ def test_default_patch(capsys, content, fullname):
assert '' == captured.err

assert json.loads(content(fullname('lists.b.json', shared=True))) == \
json.loads(content(fullname('got.json')))
json.loads(content(result_file_name))


def test_json_ofmt_opts(capsys, content, expected, fullname):
def test_json_ofmt_opts(capsys, content, expected, fullname, tmp_path):
result_file_name = '{}.got.json'.format(tmp_path)
copyfile(
fullname('lists.a.json', shared=True),
fullname('got.json'),
result_file_name,
)
nested_diff.patch_tool.App(args=(
fullname('got.json'),
result_file_name,
fullname('lists.patch.json', shared=True),
'--ofmt', 'json',
'--ofmt-opts', '{"indent": null}',
Expand All @@ -41,16 +43,17 @@ def test_json_ofmt_opts(capsys, content, expected, fullname):
assert '' == captured.out
assert '' == captured.err

assert json.loads(expected) == json.loads(content(fullname('got.json')))
assert json.loads(expected) == json.loads(content(result_file_name))


def test_yaml_ifmt(capsys, content, fullname):
def test_yaml_ifmt(capsys, content, fullname, tmp_path):
result_file_name = '{}.got'.format(tmp_path)
copyfile(
fullname('lists.a.yaml', shared=True),
fullname('got'),
result_file_name,
)
nested_diff.patch_tool.App(args=(
fullname('got'),
result_file_name,
fullname('lists.patch.yaml', shared=True),
'--ifmt', 'yaml',
)).run()
Expand All @@ -61,16 +64,17 @@ def test_yaml_ifmt(capsys, content, fullname):

# output is json by default
assert json.loads(content(fullname('lists.b.json', shared=True))) == \
json.loads(content(fullname('got')))
json.loads(content(result_file_name))


def test_yaml_ofmt(capsys, content, expected, fullname):
def test_yaml_ofmt(capsys, content, expected, fullname, tmp_path):
result_file_name = '{}.got.json'.format(tmp_path)
copyfile(
fullname('lists.a.json', shared=True),
fullname('got.json'),
result_file_name,
)
nested_diff.patch_tool.App(args=(
fullname('got.json'),
result_file_name,
fullname('lists.patch.json', shared=True),
'--ofmt', 'yaml',
)).run()
Expand All @@ -79,7 +83,7 @@ def test_yaml_ofmt(capsys, content, expected, fullname):
assert '' == captured.out
assert '' == captured.err

assert expected == content(fullname('got.json'))
assert expected == content(result_file_name)


def test_entry_point(capsys):
Expand Down
85 changes: 85 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import pytest

import nested_diff


def do_test_function(test, func):
if 'diff' not in test:
print(nested_diff.diff(test['a'], test['b']))

if 'raises' in test:
with test['raises']:
func(test)
else:
assert test['result'] == func(test)


def iterate_test_suite(tests_map, results_mod, func):
results_map = {} if 'CANONIZE' in os.environ else results_mod.RESULTS

for name, test in sorted(tests_map.items()):
if 'CANONIZE' in os.environ:
try:
test['result'] = func(test)
results_map[name] = {'result': test['result']}
except Exception as e:
results_map[name] = {
'raises': e if type(e) is type else type(e),
}
else:
try:
test['result'] = results_map[name]['result']
except KeyError:
test['raises'] = pytest.raises(results_map[name]['raises'])

if 'result' in test or 'raises' in test:
marks = []
else:
marks = [pytest.mark.xfail]

yield pytest.param(test, func, id=name, marks=marks)

if 'CANONIZE' in os.environ:
save_test_suite(results_mod.__file__, results_map)


def save_test_suite(filename, results):
with open(filename, 'w') as f:
f.write('''"""
Autogenerated, do not edit manually!
"""
import sys
RESULTS = {
''')

for k, v in sorted(results.items()):
t = 'result' if 'result' in v else 'raises'
if 'raises' in v:
t = 'raises'
r = v['raises'].__name__
else:
t = 'result'
r = repr(v['result'])

f.write("""\
'{name}': {{
'{type}': {result},
}},
""".format(name=k, type=t, result=r))

f.write("""}
if __name__ == '__main__':
names = sys.argv[1:] if len(sys.argv) > 1 else sorted(RESULTS.keys())
headers = len(names) > 1
for name in names:
if headers:
print('========== ' + name + ' ==========')
print(RESULTS[name].get('result', None), end='')
""")
13 changes: 3 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from io import StringIO


pytest.register_assert_rewrite('tests.common')


def get_testfile_name(request, suffix='dat', shared=False):
if shared:
return os.path.join(
Expand Down Expand Up @@ -39,16 +42,6 @@ def _name_getter(suffix, shared=False):
return _name_getter


@pytest.fixture
def testfile(request):
def _content_getter(suffix, shared=False):
filename = get_testfile_name(request, suffix=suffix, shared=shared)
with open(filename) as f:
return f.read()

return _content_getter


@pytest.fixture
def stringio():
return StringIO()
Expand Down

0 comments on commit bbe34b0

Please sign in to comment.