| 1 |
import os |
| 2 |
import pytest |
| 3 |
from shutil import copyfile |
| 4 |
from click.testing import CliRunner |
| 5 |
from deepdiff.commands import diff, patch, grep, extract |
| 6 |
from conftest import FIXTURES_DIR |
| 7 |
from deepdiff.helper import pypy3 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.skipif(pypy3, reason='clevercsv is not supported in pypy3') |
| 11 |
class TestCommands: |
| 12 |
|
| 13 |
@pytest.mark.parametrize('name1, name2, expected_in_stdout, expected_exit_code', [ |
| 14 |
('t1.json', 't2.json', """dictionary_item_added": {\n "root[0]['key3']": "value3\"""", 0), |
| 15 |
('t1_corrupt.json', 't2.json', "Error when loading t1:", 1), |
| 16 |
('t1.json', 't2_json.csv', '"old_value": "value2"', 0), |
| 17 |
('t2_json.csv', 't1.json', '"old_value": "value3"', 0), |
| 18 |
('t1.csv', 't2.csv', '"new_value": "James"', 0), |
| 19 |
('t1.toml', 't2.toml', "10.0.0.2", 0), |
| 20 |
('t1.pickle', 't2.pickle', '"new_value": 5,\n "old_value": 1', 0), |
| 21 |
('t1.yaml', 't2.yaml', '"new_value": 61,\n "old_value": 65', 0), |
| 22 |
]) |
| 23 |
def test_diff_command(self, name1, name2, expected_in_stdout, expected_exit_code): |
| 24 |
t1 = os.path.join(FIXTURES_DIR, name1) |
| 25 |
t2 = os.path.join(FIXTURES_DIR, name2) |
| 26 |
|
| 27 |
runner = CliRunner() |
| 28 |
result = runner.invoke(diff, [t1, t2]) |
| 29 |
assert result.exit_code == expected_exit_code, f"test_diff_command failed for {name1}, {name2}" |
| 30 |
assert expected_in_stdout in result.output, f"test_diff_command failed for {name1}, {name2}" |
| 31 |
|
| 32 |
def test_cli_cant_find_file(self): |
| 33 |
runner = CliRunner() |
| 34 |
result = runner.invoke(diff, ['phantom_file1', 'phantom_file2']) |
| 35 |
assert result.exit_code == 2 |
| 36 |
assert "Path 'phantom_file1' does not exist" in result.output |
| 37 |
|
| 38 |
@pytest.mark.parametrize('t1, t2, args, expected_exit_code', [ |
| 39 |
('t1.json', 't2.json', {}, 0), |
| 40 |
('t1_corrupt.json', 't2.json', {}, 1), |
| 41 |
('t1.json', 't2_json.csv', {}, 0), |
| 42 |
('t2_json.csv', 't1.json', {}, 0), |
| 43 |
('t1.csv', 't2.csv', ["--ignore-order", "--report-repetition"], 0), |
| 44 |
('t1.toml', 't2.toml', {}, 0), |
| 45 |
('t1.pickle', 't2.pickle', {}, 0), |
| 46 |
('t1.yaml', 't2.yaml', {}, 0), |
| 47 |
]) |
| 48 |
def test_deeppatch_command(self, t1, t2, args, expected_exit_code, tmp_path): |
| 49 |
t1_copy_path = os.path.join(tmp_path, t1) |
| 50 |
t1 = os.path.join(FIXTURES_DIR, t1) |
| 51 |
t2 = os.path.join(FIXTURES_DIR, t2) |
| 52 |
copyfile(t1, t1_copy_path) |
| 53 |
runner = CliRunner() |
| 54 |
delta_pickled = runner.invoke(diff, [t1, t2, '--create-patch', *args]) |
| 55 |
assert delta_pickled.exit_code == expected_exit_code |
| 56 |
|
| 57 |
if expected_exit_code == 0: |
| 58 |
delta_path = os.path.join(tmp_path, 'delta.pickle') |
| 59 |
with open(delta_path, 'wb') as the_file: |
| 60 |
the_file.write(delta_pickled.stdout_bytes) |
| 61 |
|
| 62 |
runner = CliRunner() |
| 63 |
deeppatched = runner.invoke(patch, [t1_copy_path, delta_path]) |
| 64 |
assert deeppatched.exit_code == expected_exit_code |
| 65 |
|
| 66 |
runner = CliRunner() |
| 67 |
final_diff = runner.invoke(diff, [t1_copy_path, t2, *args]) |
| 68 |
assert final_diff.exit_code == expected_exit_code |
| 69 |
assert final_diff.output == '{}\n' |
| 70 |
|
| 71 |
def test_command_group_by(self): |
| 72 |
t1 = os.path.join(FIXTURES_DIR, 'c_t1.csv') |
| 73 |
t2 = os.path.join(FIXTURES_DIR, 'c_t2.csv') |
| 74 |
runner = CliRunner() |
| 75 |
diffed = runner.invoke(diff, [t1, t2, '--group-by', 'id']) |
| 76 |
assert 0 == diffed.exit_code |
| 77 |
assert 'values_changed' in diffed.output |
| 78 |
assert '"new_value": "Chicken"' in diffed.output |
| 79 |
|
| 80 |
def test_command_math_epsilon(self): |
| 81 |
t1 = os.path.join(FIXTURES_DIR, 'd_t1.yaml') |
| 82 |
t2 = os.path.join(FIXTURES_DIR, 'd_t2.yaml') |
| 83 |
runner = CliRunner() |
| 84 |
diffed = runner.invoke(diff, [t1, t2, '--math-epsilon', '0.1']) |
| 85 |
assert 0 == diffed.exit_code |
| 86 |
assert '{}\n' == diffed.output |
| 87 |
|
| 88 |
diffed2 = runner.invoke(diff, [t1, t2, '--math-epsilon', '0.001']) |
| 89 |
assert 0 == diffed2.exit_code |
| 90 |
assert '{\n "values_changed": {\n "root[2][2]": {\n "new_value": 0.289,\n "old_value": 0.288\n }\n }\n}\n' == diffed2.output |
| 91 |
|
| 92 |
def test_command_grep(self): |
| 93 |
path = os.path.join(FIXTURES_DIR, 'd_t1.yaml') |
| 94 |
runner = CliRunner() |
| 95 |
diffed = runner.invoke(grep, ['Sammy', path]) |
| 96 |
assert 0 == diffed.exit_code |
| 97 |
assert "{'matched_values': ['root[2][0]']}\n" == diffed.output |
| 98 |
|
| 99 |
def test_command_err_grep1(self): |
| 100 |
path = os.path.join(FIXTURES_DIR, 'd_t1.yaml') |
| 101 |
runner = CliRunner() |
| 102 |
diffed = runner.invoke(grep, [path, 'Sammy']) |
| 103 |
assert "Path 'Sammy' does not exist" in diffed.output |
| 104 |
assert 2 == diffed.exit_code |
| 105 |
|
| 106 |
def test_command_err_grep2(self): |
| 107 |
path = os.path.join(FIXTURES_DIR, 'invalid_yaml.yaml') |
| 108 |
runner = CliRunner() |
| 109 |
diffed = runner.invoke(grep, ['invalid', path]) |
| 110 |
assert "mapping keys are not allowed here" in diffed.output |
| 111 |
assert 1 == diffed.exit_code |
| 112 |
|
| 113 |
def test_command_extract(self): |
| 114 |
path = os.path.join(FIXTURES_DIR, 'd_t1.yaml') |
| 115 |
runner = CliRunner() |
| 116 |
diffed = runner.invoke(extract, ['root[2][2]', path]) |
| 117 |
assert 0 == diffed.exit_code |
| 118 |
assert '0.288\n' == diffed.output |