Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -8,8 +8,9 @@ from core.helper.position_helper import get_position_map, is_filtered, pin_posit
|
||||
@pytest.fixture
|
||||
def prepare_example_positions_yaml(tmp_path, monkeypatch) -> str:
|
||||
monkeypatch.chdir(tmp_path)
|
||||
tmp_path.joinpath("example_positions.yaml").write_text(dedent(
|
||||
"""\
|
||||
tmp_path.joinpath("example_positions.yaml").write_text(
|
||||
dedent(
|
||||
"""\
|
||||
- first
|
||||
- second
|
||||
# - commented
|
||||
@@ -17,57 +18,54 @@ def prepare_example_positions_yaml(tmp_path, monkeypatch) -> str:
|
||||
|
||||
- 9999999999999
|
||||
- forth
|
||||
"""))
|
||||
"""
|
||||
)
|
||||
)
|
||||
return str(tmp_path)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def prepare_empty_commented_positions_yaml(tmp_path, monkeypatch) -> str:
|
||||
monkeypatch.chdir(tmp_path)
|
||||
tmp_path.joinpath("example_positions_all_commented.yaml").write_text(dedent(
|
||||
"""\
|
||||
tmp_path.joinpath("example_positions_all_commented.yaml").write_text(
|
||||
dedent(
|
||||
"""\
|
||||
# - commented1
|
||||
# - commented2
|
||||
-
|
||||
-
|
||||
|
||||
"""))
|
||||
"""
|
||||
)
|
||||
)
|
||||
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')
|
||||
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
|
||||
assert len(position_map) == 4
|
||||
assert position_map == {
|
||||
'first': 0,
|
||||
'second': 1,
|
||||
'third': 2,
|
||||
'forth': 3,
|
||||
"first": 0,
|
||||
"second": 1,
|
||||
"third": 2,
|
||||
"forth": 3,
|
||||
}
|
||||
|
||||
|
||||
def test_position_helper_with_all_commented(prepare_empty_commented_positions_yaml):
|
||||
position_map = get_position_map(
|
||||
folder_path=prepare_empty_commented_positions_yaml,
|
||||
file_name='example_positions_all_commented.yaml')
|
||||
folder_path=prepare_empty_commented_positions_yaml, file_name="example_positions_all_commented.yaml"
|
||||
)
|
||||
assert position_map == {}
|
||||
|
||||
|
||||
def test_excluded_position_data(prepare_example_positions_yaml):
|
||||
position_map = get_position_map(
|
||||
folder_path=prepare_example_positions_yaml,
|
||||
file_name='example_positions.yaml'
|
||||
)
|
||||
pin_list = ['forth', 'first']
|
||||
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
|
||||
pin_list = ["forth", "first"]
|
||||
include_set = set()
|
||||
exclude_set = {'9999999999999'}
|
||||
exclude_set = {"9999999999999"}
|
||||
|
||||
position_map = pin_position_map(
|
||||
original_position_map=position_map,
|
||||
pin_list=pin_list
|
||||
)
|
||||
position_map = pin_position_map(original_position_map=position_map, pin_list=pin_list)
|
||||
|
||||
data = [
|
||||
"forth",
|
||||
@@ -90,22 +88,16 @@ def test_excluded_position_data(prepare_example_positions_yaml):
|
||||
)
|
||||
|
||||
# assert the result in the correct order
|
||||
assert sorted_data == ['forth', 'first', 'second', 'third', 'extra1', 'extra2']
|
||||
assert sorted_data == ["forth", "first", "second", "third", "extra1", "extra2"]
|
||||
|
||||
|
||||
def test_included_position_data(prepare_example_positions_yaml):
|
||||
position_map = get_position_map(
|
||||
folder_path=prepare_example_positions_yaml,
|
||||
file_name='example_positions.yaml'
|
||||
)
|
||||
pin_list = ['forth', 'first']
|
||||
include_set = {'forth', 'first'}
|
||||
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
|
||||
pin_list = ["forth", "first"]
|
||||
include_set = {"forth", "first"}
|
||||
exclude_set = {}
|
||||
|
||||
position_map = pin_position_map(
|
||||
original_position_map=position_map,
|
||||
pin_list=pin_list
|
||||
)
|
||||
position_map = pin_position_map(original_position_map=position_map, pin_list=pin_list)
|
||||
|
||||
data = [
|
||||
"forth",
|
||||
@@ -128,4 +120,4 @@ def test_included_position_data(prepare_example_positions_yaml):
|
||||
)
|
||||
|
||||
# assert the result in the correct order
|
||||
assert sorted_data == ['forth', 'first']
|
||||
assert sorted_data == ["forth", "first"]
|
||||
|
||||
@@ -5,17 +5,18 @@ 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'
|
||||
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(dedent(
|
||||
"""\
|
||||
file_path.write_text(
|
||||
dedent(
|
||||
"""\
|
||||
address:
|
||||
city: Example City
|
||||
country: Example Country
|
||||
@@ -26,7 +27,9 @@ def prepare_example_yaml_file(tmp_path, monkeypatch) -> str:
|
||||
- Java
|
||||
- C++
|
||||
empty_key:
|
||||
"""))
|
||||
"""
|
||||
)
|
||||
)
|
||||
return str(file_path)
|
||||
|
||||
|
||||
@@ -34,8 +37,9 @@ def prepare_example_yaml_file(tmp_path, monkeypatch) -> str:
|
||||
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(dedent(
|
||||
"""\
|
||||
file_path.write_text(
|
||||
dedent(
|
||||
"""\
|
||||
address:
|
||||
city: Example City
|
||||
country: Example Country
|
||||
@@ -45,13 +49,15 @@ def prepare_invalid_yaml_file(tmp_path, monkeypatch) -> str:
|
||||
- Python
|
||||
- Java
|
||||
- C++
|
||||
"""))
|
||||
"""
|
||||
)
|
||||
)
|
||||
return str(file_path)
|
||||
|
||||
|
||||
def test_load_yaml_non_existing_file():
|
||||
assert load_yaml_file(file_path=NON_EXISTING_YAML_FILE) == {}
|
||||
assert load_yaml_file(file_path='') == {}
|
||||
assert load_yaml_file(file_path="") == {}
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_yaml_file(file_path=NON_EXISTING_YAML_FILE, ignore_error=False)
|
||||
@@ -60,12 +66,12 @@ def test_load_yaml_non_existing_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['address']['city'] == 'Example City'
|
||||
assert set(yaml_data['languages']) == {'Python', 'Java', 'C++'}
|
||||
assert yaml_data.get('empty_key') is None
|
||||
assert yaml_data.get('non_existed_key') is None
|
||||
assert yaml_data["age"] == 30
|
||||
assert yaml_data["gender"] == "male"
|
||||
assert yaml_data["address"]["city"] == "Example City"
|
||||
assert set(yaml_data["languages"]) == {"Python", "Java", "C++"}
|
||||
assert yaml_data.get("empty_key") is None
|
||||
assert yaml_data.get("non_existed_key") is None
|
||||
|
||||
|
||||
def test_load_invalid_yaml_file(prepare_invalid_yaml_file):
|
||||
|
||||
Reference in New Issue
Block a user