test_position_helper.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from textwrap import dedent
  2. import pytest
  3. from core.helper.position_helper import get_position_map, is_filtered, pin_position_map, sort_by_position_map
  4. @pytest.fixture
  5. def prepare_example_positions_yaml(tmp_path, monkeypatch) -> str:
  6. monkeypatch.chdir(tmp_path)
  7. tmp_path.joinpath("example_positions.yaml").write_text(
  8. dedent(
  9. """\
  10. - first
  11. - second
  12. # - commented
  13. - third
  14. - 9999999999999
  15. - forth
  16. """
  17. )
  18. )
  19. return str(tmp_path)
  20. @pytest.fixture
  21. def prepare_empty_commented_positions_yaml(tmp_path, monkeypatch) -> str:
  22. monkeypatch.chdir(tmp_path)
  23. tmp_path.joinpath("example_positions_all_commented.yaml").write_text(
  24. dedent(
  25. """\
  26. # - commented1
  27. # - commented2
  28. -
  29. -
  30. """
  31. )
  32. )
  33. return str(tmp_path)
  34. def test_position_helper(prepare_example_positions_yaml):
  35. position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
  36. assert len(position_map) == 4
  37. assert position_map == {
  38. "first": 0,
  39. "second": 1,
  40. "third": 2,
  41. "forth": 3,
  42. }
  43. def test_position_helper_with_all_commented(prepare_empty_commented_positions_yaml):
  44. position_map = get_position_map(
  45. folder_path=prepare_empty_commented_positions_yaml, file_name="example_positions_all_commented.yaml"
  46. )
  47. assert position_map == {}
  48. def test_excluded_position_data(prepare_example_positions_yaml):
  49. position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
  50. pin_list = ["forth", "first"]
  51. include_set = set()
  52. exclude_set = {"9999999999999"}
  53. position_map = pin_position_map(original_position_map=position_map, pin_list=pin_list)
  54. data = [
  55. "forth",
  56. "first",
  57. "second",
  58. "third",
  59. "9999999999999",
  60. "extra1",
  61. "extra2",
  62. ]
  63. # filter out the data
  64. data = [item for item in data if not is_filtered(include_set, exclude_set, item, lambda x: x)]
  65. # sort data by position map
  66. sorted_data = sort_by_position_map(
  67. position_map=position_map,
  68. data=data,
  69. name_func=lambda x: x,
  70. )
  71. # assert the result in the correct order
  72. assert sorted_data == ["forth", "first", "second", "third", "extra1", "extra2"]
  73. def test_included_position_data(prepare_example_positions_yaml):
  74. position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
  75. pin_list = ["forth", "first"]
  76. include_set = {"forth", "first"}
  77. exclude_set = {}
  78. position_map = pin_position_map(original_position_map=position_map, pin_list=pin_list)
  79. data = [
  80. "forth",
  81. "first",
  82. "second",
  83. "third",
  84. "9999999999999",
  85. "extra1",
  86. "extra2",
  87. ]
  88. # filter out the data
  89. data = [item for item in data if not is_filtered(include_set, exclude_set, item, lambda x: x)]
  90. # sort data by position map
  91. sorted_data = sort_by_position_map(
  92. position_map=position_map,
  93. data=data,
  94. name_func=lambda x: x,
  95. )
  96. # assert the result in the correct order
  97. assert sorted_data == ["forth", "first"]