GeezerVison is showing github.com — adding more test coverage · adjust/deepdiff@2e59739
This repository was archived by the owner on Dec 11, 2023. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deepdiff/commands.py +8-11Lines changed: 8 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | @click.group() | 14 | 14 | def deepdiff_cli(): | 15 | 15 | """A simple command line tool.""" | 16 | | - pass | | 16 | + pass # pragma: no cover. | 17 | 17 |
| 18 | 18 |
| 19 | 19 | # cutoff_intersection_for_pairs=CUTOFF_INTERSECTION_FOR_PAIRS_DEFAULT, | @@ -54,15 +54,12 @@ def diff( | 54 | 54 | t2_path = kwargs.pop("t2") | 55 | 55 | t1_extension = t1_path.split('.')[-1] | 56 | 56 | t2_extension = t2_path.split('.')[-1] | 57 | | - try: | 58 | | - kwargs['t1'] = load_path_content(t1_path, file_type=t1_extension) | 59 | | - except Exception as e: | 60 | | - sys.exit(str(f"Error when loading t1: {e}")) | 61 | 57 |
| 62 | | - try: | 63 | | - kwargs['t2'] = load_path_content(t2_path, file_type=t2_extension) | 64 | | - except Exception as e: | 65 | | - sys.exit(str(f"Error when loading t2: {e}")) | | 58 | + for name, t_path, t_extension in [('t1', t1_path, t1_extension), ('t2', t2_path, t2_extension)]: | | 59 | + try: | | 60 | + kwargs[name] = load_path_content(t_path, file_type=t_extension) | | 61 | + except Exception as e: | | 62 | + sys.exit(str(f"Error when loading {name}: {e}")) | 66 | 63 |
| 67 | 64 | if (t1_extension != t2_extension): | 68 | 65 | if t1_extension in {'csv', 'tsv'}: | @@ -72,6 +69,6 @@ def diff( | 72 | 69 |
| 73 | 70 | try: | 74 | 71 | diff = DeepDiff(**kwargs) | 75 | | - except Exception as e: | 76 | | - sys.exit(str(e)) | | 72 | + except Exception as e: # pragma: no cover. No need to test this. | | 73 | + sys.exit(str(e)) # pragma: no cover. No need to test this. | 77 | 74 | pprint(diff, indent=2) |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/serialization.py +12-12Lines changed: 12 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | import collections # NOQA | 12 | 12 | try: | 13 | 13 | import yaml | 14 | | -except ImportError: | 15 | | - yaml = None | | 14 | +except ImportError: # pragma: no cover. | | 15 | + yaml = None # pragma: no cover. | 16 | 16 | try: | 17 | 17 | import toml | 18 | | -except ImportError: | 19 | | - toml = None | | 18 | +except ImportError: # pragma: no cover. | | 19 | + toml = None # pragma: no cover. | 20 | 20 | try: | 21 | 21 | import clevercsv | 22 | | -except ImportError: | 23 | | - clevercsv = None | | 22 | +except ImportError: # pragma: no cover. | | 23 | + clevercsv = None # pragma: no cover. | 24 | 24 | from copy import deepcopy | 25 | 25 | from collections.abc import Mapping | 26 | 26 | from deepdiff.helper import (strings, json_convertor_default, get_type, TEXT_VIEW) | @@ -327,22 +327,22 @@ def load_path_content(path, file_type=None): | 327 | 327 | with open(path, 'r') as the_file: | 328 | 328 | content = json.load(the_file) | 329 | 329 | elif file_type in {'yaml', '.yml'}: | 330 | | - if yaml is None: | 331 | | - raise ImportError('Pyyaml needs to be installed.') | | 330 | + if yaml is None: # pragma: no cover. | | 331 | + raise ImportError('Pyyaml needs to be installed.') # pragma: no cover. | 332 | 332 | with open(path, 'r') as the_file: | 333 | 333 | content = yaml.safe_load(the_file) | 334 | 334 | elif file_type == 'toml': | 335 | | - if toml is None: | 336 | | - raise ImportError('Toml needs to be installed.') | | 335 | + if toml is None: # pragma: no cover. | | 336 | + raise ImportError('Toml needs to be installed.') # pragma: no cover. | 337 | 337 | with open(path, 'r') as the_file: | 338 | 338 | content = toml.load(the_file) | 339 | 339 | elif file_type == 'pickle': | 340 | 340 | with open(path, 'rb') as the_file: | 341 | 341 | content = the_file.read() | 342 | 342 | content = pickle_load(content) | 343 | 343 | elif file_type in {'csv', 'tsv'}: | 344 | | - if clevercsv is None: | 345 | | - raise ImportError('CleverCSV needs to be installed.') | | 344 | + if clevercsv is None: # pragma: no cover. | | 345 | + raise ImportError('CleverCSV needs to be installed.') # pragma: no cover. | 346 | 346 | content = clevercsv.wrappers.read_dicts(path) | 347 | 347 | else: | 348 | 348 | raise UnsupportedFormatErr('Only json, yaml, toml, csv, tsv and pickle are supported.') | |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +[{ | | 2 | + "key1": "value1", | | 3 | + "key2": "value2", | | 4 | +}] |
Collapse file Copy file name to clipboardExpand all lines: tests/test_command.py +17-9Lines changed: 17 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | @pytest.mark.skipif(pypy3, reason='clevercsv is not supported in pypy3') | 10 | 10 | class TestCommands: | 11 | 11 |
| 12 | | - @pytest.mark.parametrize('t1, t2, kwargs, expected_in_stdout', [ | 13 | | - ('t1.json', 't2.json', {}, "'dictionary_item_added\': [root[0]"), | 14 | | - ('t1.json', 't2_json.csv', {}, "'old_value\': \'value2\'"), | 15 | | - ('t1.csv', 't2.csv', {}, "\'new_value\': \'James\'"), | 16 | | - ('t1.toml', 't2.toml', {}, "10.0.0.2"), | 17 | | - ('t1.pickle', 't2.pickle', {}, "'new_value': 5, 'old_value': 1"), | 18 | | - ('t1.yaml', 't2.yaml', {}, "'new_value': 61, 'old_value': 65"), | | 12 | + @pytest.mark.parametrize('t1, t2, kwargs, expected_in_stdout, expected_exit_code', [ | | 13 | + ('t1.json', 't2.json', {}, "'dictionary_item_added\': [root[0]", 0), | | 14 | + ('t1_corrupt.json', 't2.json', {}, "Expecting property name enclosed in double quotes", 1), | | 15 | + ('t1.json', 't2_json.csv', {}, "'old_value\': \'value2\'", 0), | | 16 | + ('t2_json.csv', 't1.json', {}, "'old_value\': \'value3\'", 0), | | 17 | + ('t1.csv', 't2.csv', {}, "\'new_value\': \'James\'", 0), | | 18 | + ('t1.toml', 't2.toml', {}, "10.0.0.2", 0), | | 19 | + ('t1.pickle', 't2.pickle', {}, "'new_value': 5, 'old_value': 1", 0), | | 20 | + ('t1.yaml', 't2.yaml', {}, "'new_value': 61, 'old_value': 65", 0), | 19 | 21 | ]) | 20 | | - def test_diff_command(self, t1, t2, kwargs, expected_in_stdout): | | 22 | + def test_diff_command(self, t1, t2, kwargs, expected_in_stdout, expected_exit_code): | 21 | 23 | t1 = os.path.join(FIXTURES_DIR, t1) | 22 | 24 | t2 = os.path.join(FIXTURES_DIR, t2) | 23 | 25 | runner = CliRunner() | 24 | 26 | result = runner.invoke(diff, [t1, t2], **kwargs) | 25 | | - assert result.exit_code == 0 | | 27 | + assert result.exit_code == expected_exit_code | 26 | 28 | assert expected_in_stdout in result.output | | 29 | + | | 30 | + def test_cli_cant_find_file(self): | | 31 | + runner = CliRunner() | | 32 | + result = runner.invoke(diff, ['phantom_file1', 'phantom_file2']) | | 33 | + assert result.exit_code == 2 | | 34 | + assert "Path 'phantom_file1' does not exist" in result.output |
|