|
1 | 1 | import os |
2 | 2 |
|
3 | | -from gendiff.gendiff import generate_diff, read_file |
| 3 | +from gendiff.gendiff import generate_diff |
| 4 | +from gendiff.io_utils import read_file |
4 | 5 |
|
5 | 6 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
6 | 7 | dir_with_data = os.path.join(current_dir, 'test_data') |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | def test_read_file(): |
23 | | - file1_json = os.path.join(dir_with_data, 'file1.json') |
24 | | - file2_json = os.path.join(dir_with_data, 'file2.json') |
25 | | - file1_yaml = os.path.join(dir_with_data, 'file1.yaml') |
26 | | - file2_yaml = os.path.join(dir_with_data, 'file2.yaml') |
27 | | - assert read_file(file1_json) == right_data1 |
28 | | - assert read_file(file1_json) != right_data2 |
29 | | - assert read_file(file2_json) == right_data2 |
30 | | - assert read_file(file2_json) != right_data1 |
31 | | - assert read_file(file1_yaml) == right_data1 |
32 | | - assert read_file(file1_yaml) != right_data2 |
33 | | - assert read_file(file2_yaml) == right_data2 |
34 | | - assert read_file(file2_yaml) != right_data1 |
| 24 | + file1_json_path = os.path.join(dir_with_data, 'file1.json') |
| 25 | + file2_json_path = os.path.join(dir_with_data, 'file2.json') |
| 26 | + file1_yaml_path = os.path.join(dir_with_data, 'file1.yaml') |
| 27 | + file2_yaml_path = os.path.join(dir_with_data, 'file2.yaml') |
35 | 28 |
|
| 29 | + assert read_file(file1_json_path) == right_data1 |
| 30 | + assert read_file(file1_json_path) != right_data2 |
| 31 | + assert read_file(file2_json_path) == right_data2 |
| 32 | + assert read_file(file2_json_path) != right_data1 |
| 33 | + assert read_file(file1_yaml_path) == right_data1 |
| 34 | + assert read_file(file1_yaml_path) != right_data2 |
| 35 | + assert read_file(file2_yaml_path) == right_data2 |
| 36 | + assert read_file(file2_yaml_path) != right_data1 |
36 | 37 |
|
37 | | -def test_generate_diff(): |
38 | | - right_str = ("{\n" |
39 | | - " - follow: false\n" |
40 | | - " host: hexlet.io\n" |
41 | | - " - proxy: 123.234.53.22\n" |
42 | | - " - timeout: 50\n" |
43 | | - " + timeout: 20\n" |
44 | | - " + verbose: true\n" |
45 | | - "}") |
46 | | - test_file_path = os.path.join(dir_with_data, 'test_result.txt') |
47 | | - try: |
48 | | - with open(test_file_path, 'r', encoding='utf-8') as file: |
49 | | - right_data = file.read() |
50 | | - except OSError: |
51 | | - print('Can not open test_file.') |
52 | 38 |
|
53 | | - test_file_path = os.path.join(dir_with_data, 'test_result_plain.txt') |
| 39 | +def open_file(file_name: str) -> dict: |
| 40 | + test_file_path = os.path.join(dir_with_data, file_name) |
54 | 41 | try: |
55 | 42 | with open(test_file_path, 'r', encoding='utf-8') as file: |
56 | | - right_data_plain = file.read() |
| 43 | + return file.read() |
57 | 44 | except OSError: |
58 | 45 | print('Can not open test_file.') |
59 | 46 |
|
60 | | - wrong_str = '' |
61 | | - file1_json = os.path.join(dir_with_data, 'file1.json') |
62 | | - file2_json = os.path.join(dir_with_data, 'file2.json') |
63 | | - file1_yaml = os.path.join(dir_with_data, 'file1.yaml') |
64 | | - file2_yaml = os.path.join(dir_with_data, 'file2.yaml') |
65 | 47 |
|
| 48 | +def test_generate_diff(): |
| 49 | + right_data_stylish = open_file('test_result_stylish.txt') |
| 50 | + right_data_plain = open_file('test_result_plain.txt') |
| 51 | + # right_data_json = open_file('test_result_json.txt') |
| 52 | + wrong_str = '' |
| 53 | + |
66 | 54 | file3_json = os.path.join(dir_with_data, 'file3.json') |
67 | 55 | file4_json = os.path.join(dir_with_data, 'file4.json') |
68 | 56 | file3_yaml = os.path.join(dir_with_data, 'file3.yaml') |
69 | 57 | file4_yaml = os.path.join(dir_with_data, 'file4.yaml') |
70 | 58 |
|
71 | | - assert generate_diff(file1_json, file2_json) == right_str |
72 | | - assert generate_diff(file1_json, file2_json) != wrong_str |
73 | | - assert generate_diff(file1_yaml, file2_yaml) == right_str |
74 | | - assert generate_diff(file1_yaml, file2_yaml) != wrong_str |
75 | | - |
76 | | - assert generate_diff(file3_json, file4_json) == right_data |
| 59 | + assert generate_diff(file3_json, file4_json) == right_data_stylish |
77 | 60 | assert generate_diff(file3_json, file4_json) != wrong_str |
78 | | - assert generate_diff(file3_yaml, file4_yaml) == right_data |
| 61 | + assert generate_diff(file3_yaml, file4_yaml) == right_data_stylish |
79 | 62 | assert generate_diff(file3_yaml, file4_yaml) != wrong_str |
80 | 63 |
|
81 | 64 | assert generate_diff(file3_json, file4_json, 'plain') == right_data_plain |
82 | 65 | assert generate_diff(file3_json, file4_json, 'plain') != wrong_str |
83 | 66 | assert generate_diff(file3_yaml, file4_yaml, 'plain') == right_data_plain |
84 | 67 | assert generate_diff(file3_yaml, file4_yaml, 'plain') != wrong_str |
85 | 68 |
|
| 69 | + # assert generate_diff(file3_json, file4_json, 'json') == right_data_json |
| 70 | + # assert generate_diff(file3_json, file4_json, 'json') != wrong_str |
| 71 | + # assert generate_diff(file3_yaml, file4_yaml, 'json') == right_data_json |
| 72 | + # assert generate_diff(file3_yaml, file4_yaml, 'json') != wrong_str |
0 commit comments