def test_gendiff_empty_file(): with pytest.raises(ValueError): assert generate_diff(path_current(PLAIN_JSON_DIR, 'original.json'), path_current('clean_file.json')) with pytest.raises(ValueError): assert generate_diff(path_current(PLAIN_JSON_DIR, 'original.json'), path_current('clean_file.yml'))
def test_generate_diff(path_to_first, path_to_second, form, expected): with open(expected, 'r') as file: if form == 'json': assert json.load(file) == json.loads( generate_diff(path_to_first, path_to_second, form)) else: assert file.read() == generate_diff(path_to_first, path_to_second, form)
def test_gen_diff(file_format): file1 = 'tests/fixtures/first_file.{0}'.format(file_format) file2 = 'tests/fixtures/second_file.{0}'.format(file_format) json_diff = generate_diff(file1, file2, 'json') stylish_diff = generate_diff(file1, file2, 'stylish') plain_diff = generate_diff(file1, file2, 'plain') default_diff = generate_diff(file1, file2) assert default_diff == _read_data(stylish_result) assert stylish_diff == _read_data(stylish_result) assert plain_diff == _read_data(plain_result) assert json.loads(json_diff) == json.loads(_read_data(json_result)) assert isinstance(json.loads(json_diff), dict)
def test_generate_diff_yml(): before = "tests/fixtures/before.yaml" after = "tests/fixtures/after.yml" data = generate_diff(before, after) with open("tests/fixtures/yml_diff.txt", "r") as yml_diff: expected = yml_diff.read().strip("\n") assert data == expected
def test_generate_diff_json(): before = "tests/fixtures/before.json" after = "tests/fixtures/after.json" data = generate_diff(before, after) with open("tests/fixtures/json_diff.txt", "r") as json_diff: expected = json_diff.read().strip("\n") assert data == expected
def test_gendiff(): json1 = "tests/fixtures/flat_json_1.json" json2 = "tests/fixtures/flat_json_2.json" right_answer = open( "tests/fixtures/expectations/right_flat.txt" ) assert gendiff.generate_diff(json1, json2) == right_answer.read()
def test_gendiff(): json1 = "tests/fixtures/tree_json_1.json" json2 = "tests/fixtures/tree_json_2.json" right_answer = json.load( open("tests/fixtures/expectations/right_json.json")) assert json.loads(gendiff.generate_diff(json1, json2, 'json')) == right_answer
def test_0_plain(): file2 = './tests/fixtures/test_01.json' file1 = './tests/fixtures/test_00.json' with open('./tests/fixtures/test_plain.txt', 'r') as answer: expected = answer.read() result = gendiff.generate_diff(file1, file2, 'plain') assert result == expected
def test_split_json(): file2 = './tests/fixtures/after.json' file1 = './tests/fixtures/before.json' with open('./tests/fixtures/split_diff.txt', 'r') as answer: expected = answer.read() result = gendiff.generate_diff(file1, file2) assert result == expected
def test_gendiff(): json1 = "tests/fixtures/flat_json_1.json" json2 = "tests/fixtures/flat_yaml.yml" right_answer = open("tests/fixtures/expectations/right_flat.txt", mode="r", encoding="UTF-8") assert gendiff.generate_diff(json1, json2) == right_answer.read()
def test_json(): file1 = 'tests/fixtures/first2.json' file2 = 'tests/fixtures/second2.json' with open('tests/fixtures/answer_json.json', 'r') as answer: expected = loads(answer.read()) result = loads(generate_diff(file1, file2, 'json')) assert result == expected
def test_plain_yaml(): file1 = 'tests/fixtures/first2.yaml' file2 = 'tests/fixtures/second2.yaml' with open('tests/fixtures/answer_plain.txt', 'r') as answer: expected = answer.read() result = generate_diff(file1, file2, 'plain') assert result == expected
def test_diff_json(): file1 = 'tests/fixtures/first.json' file2 = 'tests/fixtures/second.json' with open('tests/fixtures/answer_diff.txt', 'r') as answer: expected = answer.read() result = generate_diff(file1, file2) assert result == expected
def test_complex_yaml_stylish(): file2 = './tests/fixtures/complex_2.yaml' file1 = './tests/fixtures/complex_1.yaml' with open('./tests/fixtures/complex_stylish.txt', 'r') as answer: expected = answer.read() result = gendiff.generate_diff(file1, file2) assert result == expected
def test_gendiff(): json1 = "tests/fixtures/tree_json_1.json" yaml2 = "tests/fixtures/tree_yaml.yml" right_answer = open( "tests/fixtures/expectations/right_tree.txt" ) assert gendiff.generate_diff(json1, yaml2) == right_answer.read()
def main(): """Return difference of 2 json or yaml. Returns: str: difference between given 2 files. """ print(gendiff.generate_diff(args.first_file, args.second_file, args.format))
def test_nested_json_file(file_type): first_file_path = os.path.join(TEST_FILES, f'first_nested.{file_type}') second_file_path = os.path.join(TEST_FILES, f'second_nested.{file_type}') diff = generate_diff(first_file_path, second_file_path) diff = sort_dict(diff) with open(os.path.join(RESULT_FILE, "json_nested")) as f: data_file = f.read() assert data_file == get_json_diff(diff), "Данные не совпадают"
def test_flat_text_diff(file_type): first_file_path = os.path.join(TEST_FILES, f'first.{file_type}') second_file_path = os.path.join(TEST_FILES, f'second.{file_type}') diff = generate_diff(first_file_path, second_file_path) diff = sort_dict(diff) with open(os.path.join(RESULT_FILE, "flat_text_diff.txt")) as f: data_file = f.read() assert data_file == get_plain_diff(diff), "Данные не совпадают"
def test_complex_json_json(): file2 = './tests/fixtures/complex_2.json' file1 = './tests/fixtures/complex_1.json' # expected = loads('./tests/fixtures/json.json').read() with open('./tests/fixtures/json.json') as answer: expected = loads(answer.read()) result = loads(gendiff.generate_diff(file1, file2, 'json')) assert result == expected
def main(): """Главная функция.""" cli_args = parse_cli_args() diff = generate_diff( cli_args.first_file, cli_args.second_file, cli_args.format, ) print(diff)
def main(): args = cli.parse_args() print( gendiff.generate_diff( args.first_file, args.second_file, args.format ) )
def test_plain_nested_yaml_diff(): """Tests of generate_diff function. Returns answer of assert. """ first_path = 'tests/fixtures/nested1.yml' second_path = 'tests/fixtures/nested2.yml' diff = generate_diff(first_path, second_path, 'plain') exp_diff = extract_exp_value('tests/fixtures/exp_plain_nested_diff.txt') assert diff == exp_diff
def test_stylish_nested_json_diff(): """Tests of generate_diff function. Returns answer of assert. """ first_path = 'tests/fixtures/nested1.json' second_path = 'tests/fixtures/nested2.json' diff = generate_diff(first_path, second_path) exp_diff = extract_exp_value('tests/fixtures/exp_stylish_nested_diff.txt') assert diff == exp_diff
def test_stylish_yaml_diff(): """Tests of generate_diff function. Returns answer of assert. """ first_path = 'tests/fixtures/file1.yml' second_path = 'tests/fixtures/file2.yml' diff = generate_diff(first_path, second_path) exp_diff = extract_exp_value('tests/fixtures/exp_stylish_diff.txt') assert diff == exp_diff
def test_plain_json_diff(): """Tests of generate_diff function. Returns answer of assert. """ first_path = 'tests/fixtures/file1.json' second_path = 'tests/fixtures/file2.json' diff = generate_diff(first_path, second_path, 'plain') exp_diff = extract_exp_value('tests/fixtures/exp_plain_diff.txt') assert diff == exp_diff
def test_formats( path_to_previous, path_to_current, path_to_expected, output_format, ): expected = get_expected(path_to_expected) previous = '{0}{1}'.format(PATH_TO_FIXTURES, path_to_previous) current = '{0}{1}'.format(PATH_TO_FIXTURES, path_to_current) assert expected == generate_diff(previous, current, _format=output_format)
def main(): parser = argparse.ArgumentParser(description='Generate diff') parser.add_argument('first_file', type=str) parser.add_argument('second_file', type=str) parser.add_argument('-f', '--format', help='set format of output') args = parser.parse_args() diff = generate_diff(args.first_file, args.second_file) _type = args.format if _type == 'plain': print(get_plain_diff(diff)) elif _type == 'json': print(get_json_diff(diff)) elif _type is None: print(get_dict_diff(diff)) else: raise Exception("Invalid format, run --help!")
def main(): """Run this script.""" parser = argparse.ArgumentParser(description='Generate diff', ) parser.add_argument( 'path_to_first_file', help='path to first config', ) parser.add_argument( 'path_to_second_file', help='path to second config', ) parser.add_argument( '-f', '--format', help='set format of output', choices=['plain', 'json'], default='json', ) args = vars(parser.parse_args()) print(generate_diff(**args))
def main(): data1, data2, formatter = get_args() return print(generate_diff(data1, data2, formatter))
def test_json_nested(answer_plain_nested, fixtures_path): file1_path = os.path.join(fixtures_path, 'file1_nested.json') file2_path = os.path.join(fixtures_path, 'file2_nested.json') formatted_diff = generate_diff(file1_path, file2_path, 'plain') assert formatted_diff == answer_plain_nested