-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4df4fd7
commit d2fd66c
Showing
3 changed files
with
27 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
import json | ||
import yaml | ||
import os | ||
|
||
|
||
def get_file_format(filepath): | ||
file = open(filepath) | ||
if filepath.endswith('.json'): | ||
return file, 'json' | ||
elif filepath.endswith('.yml') or filepath.endswith('.yaml'): | ||
return file, 'yaml' | ||
else: | ||
file.close() | ||
raise TypeError("Invalid file format!") | ||
_, file_format = os.path.splitext(filepath) | ||
return file_format[1:] | ||
|
||
|
||
def read_file_data(filepath): | ||
file, file_format = get_file_format(filepath) | ||
if file_format == 'json': | ||
return json.load(file) | ||
elif file_format == 'yaml': | ||
return yaml.safe_load(file) | ||
else: | ||
raise TypeError("Invalid file format!") | ||
with open(filepath) as file: | ||
file_format = get_file_format(filepath) | ||
if file_format == 'json': | ||
return json.load(file) | ||
elif file_format == 'yaml' or 'yml': | ||
return yaml.load(file, Loader=yaml.Loader) | ||
else: | ||
raise TypeError("Invalid file format!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
import pytest | ||
from gendiff.parser import read_file_data | ||
|
||
|
||
def test_read_json_file_data(): | ||
filepath = 'tests/fixtures/flat_files/file1.json' | ||
expected_data = {'follow': False, 'host': 'hexlet.io', 'proxy': '123.234.53.22', 'timeout': 50} | ||
actual_data = read_file_data(filepath) | ||
assert actual_data == expected_data | ||
|
||
|
||
def test_read_yaml_file(): | ||
filepath = 'tests/fixtures/flat_files/file2.yml' | ||
expected_data = {'host': 'hexlet.io', 'timeout': 20, 'verbose': True} | ||
actual_data = read_file_data(filepath) | ||
assert actual_data == expected_data | ||
|
||
|
||
def test_determine_file_format(): | ||
file1 = read_file_data('tests/fixtures/flat_files/file1.json') | ||
file2 = read_file_data('tests/fixtures/flat_files/file2.yml') | ||
assert file1 == {'follow': False, 'host': 'hexlet.io', 'proxy': '123.234.53.22', 'timeout': 50} | ||
assert file2 == {'host': 'hexlet.io', 'timeout': 20, 'verbose': True} | ||
|
||
|
||
def test_determine_file_format_invalid(): | ||
with pytest.raises(TypeError) as excinfo: | ||
read_file_data('tests/fixtures/comparison_results/correct_flat_comparison.txt') | ||
assert 'Invalid file format!' in str(excinfo.value) |