test_crf_reader.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from pathlib import Path
  2. from unfccc_ghg_data.helper import downloaded_data_path_UNFCCC
  3. from unfccc_ghg_data.unfccc_crf_reader.unfccc_crf_reader_core import (
  4. filter_category,
  5. find_latest_version,
  6. get_country_folders,
  7. get_info_from_crf_filename,
  8. get_latest_date_for_country,
  9. get_latest_version_for_country,
  10. )
  11. def test_get_latest_date_for_country():
  12. # RUS CRF
  13. expected = "22082023"
  14. date = get_latest_date_for_country("RUS", 2023, submission_type="CRF")
  15. assert date == expected
  16. # AUS CRT
  17. expected = "12042024"
  18. date = get_latest_date_for_country("AUS", 1, submission_type="CRT")
  19. assert date == expected
  20. # RUS CRT
  21. expected = "20241220"
  22. date = get_latest_date_for_country("RUS", 1, submission_type="CRT")
  23. assert date == expected
  24. def test_get_latest_version_for_country():
  25. # AUS CRT
  26. expected = "V0.0"
  27. date = get_latest_version_for_country("AUS", 1)
  28. assert date == expected
  29. # RUS CRT
  30. expected = "V1.0"
  31. date = get_latest_version_for_country("RUS", 1)
  32. assert date == expected
  33. def test_find_latest():
  34. test_list = ["V0.01", "V0.3", "V0.2"]
  35. expected = "V0.3"
  36. version = find_latest_version(test_list)
  37. assert version == expected
  38. def test_get_info_from_crf_filename():
  39. # crf
  40. filename = "BLR_2021_1990_30032021_192048.xlsx"
  41. expected = {
  42. "party": "BLR",
  43. "submission_year": 2021,
  44. "data_year": 1990,
  45. "date": "30032021",
  46. "extra": "192048",
  47. }
  48. assert expected == get_info_from_crf_filename(filename)
  49. # crt
  50. filename = "GUY-CRT-2024-V0.3-1992-20240927-191031_started.xlsx"
  51. expected = {
  52. "party": "GUY",
  53. "submission_year": 2024,
  54. "data_year": 1992,
  55. "date": "20240927",
  56. "extra": "191031_started",
  57. "version": "V0.3",
  58. }
  59. assert expected == get_info_from_crf_filename(filename)
  60. def test_filter_category():
  61. # general
  62. map_gen = ["Option C (country-specific):", ["\\IGNORE"], 4]
  63. assert filter_category(map_gen, "MOZ") == map_gen
  64. # country specific
  65. expected = [
  66. "Other (as specified in table 3(I).A)",
  67. ["3.A.1.C"],
  68. 5,
  69. ]
  70. expected_remove = ["\\REMOVE", ["3.A.1.C"], 5]
  71. # exclude multiple
  72. map_excl_multiple = [
  73. "\\C!-AUS-MLT-LUX-POL-SVN-USA\\ Other (as specified in table 3(I).A)",
  74. ["3.A.1.C"],
  75. 5,
  76. ]
  77. assert filter_category(map_excl_multiple, "MOZ") == expected
  78. assert filter_category(map_excl_multiple, "MLT") == expected_remove
  79. # exclude single
  80. map_excl_single = [
  81. "\\C!-AUS\\ Other (as specified in table 3(I).A)",
  82. ["3.A.1.C"],
  83. 5,
  84. ]
  85. expected = [
  86. "Other (as specified in table 3(I).A)",
  87. ["3.A.1.C"],
  88. 5,
  89. ]
  90. expected_remove = ["\\REMOVE", ["3.A.1.C"], 5]
  91. assert filter_category(map_excl_single, "MOZ") == expected
  92. assert filter_category(map_excl_single, "AUS") == expected_remove
  93. # include multiple
  94. map_incl_multiple = [
  95. "\\C-AUS-NLD\\ Other (as specified in table 3(I).A)",
  96. ["3.A.1.C"],
  97. 5,
  98. ]
  99. assert filter_category(map_incl_multiple, "MOZ") == expected_remove
  100. assert filter_category(map_incl_multiple, "AUS") == expected
  101. assert filter_category(map_incl_multiple, "NLD") == expected
  102. # include single
  103. map_incl_single = ["\\C-AUS\\ Other (as specified in table 3(I).A)", ["3.A.1.C"], 5]
  104. assert filter_category(map_incl_single, "MOZ") == expected_remove
  105. assert filter_category(map_incl_single, "AUS") == expected
  106. def test_get_country_folders():
  107. # BTR1
  108. expected = [
  109. Path("Russian_Federation/BTR1"),
  110. Path("Australia/BTR1"),
  111. Path("Guyana/BTR1"),
  112. ]
  113. folders = get_country_folders(
  114. country_codes=["RUS", "AUS", "GUY"],
  115. submission_year=1,
  116. submission_type="CRT",
  117. )
  118. folders = [folder.relative_to(downloaded_data_path_UNFCCC) for folder in folders]
  119. assert expected == folders
  120. # CRF 2023
  121. expected = [
  122. Path("Russian_Federation/CRF2023"),
  123. Path("Australia/CRF2023"),
  124. Path("Germany/CRF2023"),
  125. ]
  126. folders = get_country_folders(
  127. country_codes=["RUS", "AUS", "DEU"],
  128. submission_year=2023,
  129. submission_type="CRF",
  130. )
  131. folders = [folder.relative_to(downloaded_data_path_UNFCCC) for folder in folders]
  132. assert expected == folders