test_position_helper.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from textwrap import dedent
  2. import pytest
  3. from core.helper.position_helper import get_position_map, sort_and_filter_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_map(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_list = []
  51. exclude_list = ['9999999999999']
  52. sorted_filtered_position_map = sort_and_filter_position_map(
  53. original_position_map=position_map,
  54. pin_list=pin_list,
  55. include_list=include_list,
  56. exclude_list=exclude_list
  57. )
  58. assert sorted_filtered_position_map == {
  59. 'forth': 0,
  60. 'first': 1,
  61. 'second': 2,
  62. 'third': 3,
  63. }
  64. def test_included_position_map(prepare_example_positions_yaml):
  65. position_map = get_position_map(
  66. folder_path=prepare_example_positions_yaml,
  67. file_name='example_positions.yaml'
  68. )
  69. pin_list = ['second', 'first']
  70. include_list = ['first', 'second', 'third', 'forth']
  71. exclude_list = []
  72. sorted_filtered_position_map = sort_and_filter_position_map(
  73. original_position_map=position_map,
  74. pin_list=pin_list,
  75. include_list=include_list,
  76. exclude_list=exclude_list
  77. )
  78. assert sorted_filtered_position_map == {
  79. 'second': 0,
  80. 'first': 1,
  81. 'third': 2,
  82. 'forth': 3,
  83. }