-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
ecb51a8
commit 396e40c
Showing
11 changed files
with
172 additions
and
54 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
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import logging | ||
import os | ||
|
||
import yaml | ||
from yaml import YAMLError | ||
|
||
|
||
def load_yaml_file(file_path: str, ignore_error: bool = False) -> dict: | ||
""" | ||
Safe loading a YAML file to a dict | ||
:param file_path: the path of the YAML file | ||
:param ignore_error: | ||
if True, return empty dict if error occurs and the error will be logged in warning level | ||
if False, raise error if error occurs | ||
:return: a dict of the YAML content | ||
""" | ||
try: | ||
if not file_path or not os.path.exists(file_path): | ||
raise FileNotFoundError(f'Failed to load YAML file {file_path}: file not found') | ||
|
||
with open(file_path, encoding='utf-8') as file: | ||
try: | ||
return yaml.safe_load(file) | ||
except Exception as e: | ||
raise YAMLError(f'Failed to load YAML file {file_path}: {e}') | ||
except Exception as e: | ||
if ignore_error: | ||
logging.warning(f'Failed to load YAML file {file_path}: {e}') | ||
return {} | ||
else: | ||
raise e |
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
Empty file.
29 changes: 29 additions & 0 deletions
29
api/tests/unit_tests/utils/position_helper/test_position_helper.py
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
|
||
from core.utils.position_helper import get_position_map | ||
|
||
|
||
@pytest.fixture | ||
def prepare_example_positions_yaml(tmp_path, monkeypatch) -> str: | ||
monkeypatch.chdir(tmp_path) | ||
tmp_path.joinpath("example_positions.yaml").write_text(""" | ||
- first | ||
- second | ||
# - commented | ||
- third | ||
- forth | ||
""") | ||
return str(tmp_path) | ||
|
||
|
||
def test_position_helper(prepare_example_positions_yaml): | ||
position_map = get_position_map( | ||
folder_path=prepare_example_positions_yaml, | ||
file_name='example_positions.yaml') | ||
assert position_map == { | ||
'first': 0, | ||
'second': 1, | ||
'third': 2, | ||
'forth': 3, | ||
} |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pytest | ||
from yaml import YAMLError | ||
|
||
from core.tools.utils.yaml_utils import load_yaml_file | ||
|
||
EXAMPLE_YAML_FILE = 'example_yaml.yaml' | ||
INVALID_YAML_FILE = 'invalid_yaml.yaml' | ||
NON_EXISTING_YAML_FILE = 'non_existing_file.yaml' | ||
|
||
|
||
@pytest.fixture | ||
def prepare_example_yaml_file(tmp_path, monkeypatch) -> str: | ||
monkeypatch.chdir(tmp_path) | ||
file_path = tmp_path.joinpath(EXAMPLE_YAML_FILE) | ||
file_path.write_text(""" | ||
address: | ||
city: Example City | ||
country: Example Country | ||
age: 30 | ||
gender: male | ||
languages: | ||
- Python | ||
- Java | ||
- C++ | ||
""") | ||
return str(file_path) | ||
|
||
|
||
@pytest.fixture | ||
def prepare_invalid_yaml_file(tmp_path, monkeypatch) -> str: | ||
monkeypatch.chdir(tmp_path) | ||
file_path = tmp_path.joinpath(INVALID_YAML_FILE) | ||
file_path.write_text(""" | ||
address: | ||
city: Example City | ||
country: Example Country | ||
age: 30 | ||
empty_key: | ||
gender: male | ||
languages: | ||
- Python | ||
- Java | ||
- C++ | ||
""") | ||
return str(file_path) | ||
|
||
|
||
def test_load_yaml_non_existing_file(): | ||
assert load_yaml_file(file_path=NON_EXISTING_YAML_FILE, ignore_error=True) == {} | ||
assert load_yaml_file(file_path='', ignore_error=True) == {} | ||
|
||
with pytest.raises(FileNotFoundError, match=f'Failed to load YAML file {NON_EXISTING_YAML_FILE}: file not found'): | ||
load_yaml_file(file_path=NON_EXISTING_YAML_FILE) | ||
|
||
|
||
def test_load_valid_yaml_file(prepare_example_yaml_file): | ||
yaml_data = load_yaml_file(file_path=prepare_example_yaml_file) | ||
assert len(yaml_data) > 0 | ||
assert yaml_data['age'] == 30 | ||
assert yaml_data['gender'] == 'male' | ||
assert yaml_data.get('empty_key') is None | ||
assert yaml_data['address']['city'] == 'Example City' | ||
assert set(yaml_data['languages']) == {'Python', 'Java', 'C++'} | ||
|
||
|
||
def test_load_invalid_yaml_file(prepare_invalid_yaml_file): | ||
# yaml syntax error | ||
with pytest.raises(YAMLError): | ||
load_yaml_file(file_path=prepare_invalid_yaml_file) | ||
|
||
# ignore error | ||
assert load_yaml_file(file_path=prepare_invalid_yaml_file, ignore_error=True) == {} |