test_position_helper.py 3.3 KB

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