test_station.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import pytest
  2. from copy import deepcopy
  3. from plib import Station, StationError, StationMap
  4. @pytest.fixture
  5. def js1():
  6. js = {
  7. "code": "abkn",
  8. "height": 219.23325751908123,
  9. "status": "new",
  10. "location": {
  11. "lat": 0.0,
  12. "lon": 15.5
  13. },
  14. "xyz": [
  15. -90768.799,
  16. 3777267.336,
  17. 5121588.168
  18. ]
  19. }
  20. return js
  21. @pytest.fixture
  22. def js2():
  23. js = {
  24. "code": "abkn",
  25. "height": 219.23325751908123,
  26. "status": "new",
  27. "location": {
  28. "lat": 0.0,
  29. "lon": 0.0
  30. },
  31. "xyz": [
  32. -90768.799,
  33. 3777267.336,
  34. 5121588.168
  35. ]
  36. }
  37. return js
  38. @pytest.fixture
  39. def json_data():
  40. text = [
  41. {
  42. "code": "akya",
  43. "height": 330.27589765004814,
  44. "status": "new",
  45. "location": {
  46. "lat": 0.0,
  47. "lon": 0.0
  48. },
  49. "xyz": [
  50. 2078592.977,
  51. 3354438.194,
  52. 4994390.812
  53. ]
  54. },
  55. {
  56. "code": "alek",
  57. "height": 174.47401913441718,
  58. "location": {
  59. "lat": 10.0,
  60. "lon": 0.0
  61. },
  62. "status": "new",
  63. "xyz": [
  64. 2959560.1322,
  65. 2236904.702,
  66. 5171064.6737
  67. ]
  68. },
  69. {
  70. "code": "abdl",
  71. "height": 86.10842919163406,
  72. "status": "new",
  73. "location": {
  74. "lat": 15.5,
  75. "lon": 15.5
  76. },
  77. "xyz": [
  78. 3025653.429,
  79. 2742460.808,
  80. 4883170.747
  81. ]
  82. }
  83. ]
  84. return text
  85. class TestStation:
  86. def test_creation(self, js1):
  87. try:
  88. Station(**js1)
  89. except Exception as e:
  90. pytest.fail(f"Failed with exception '{type(e)} - {e}'")
  91. for field in ["code", "height", "status", "location"]:
  92. js_copy = deepcopy(js1)
  93. del js1["location"]
  94. with pytest.raises(StationError):
  95. Station(**js1)
  96. def test_manual_creator(self):
  97. st = Station()
  98. def test_distance_to(self, js1, js2):
  99. s1 = Station(**js1)
  100. s2 = Station(**js2)
  101. assert s1.distance_to(s2) == 15.5
  102. class TestStationMap:
  103. def test_creation(self, js1, js2):
  104. st1 = Station(**js1)
  105. st2 = Station(**js2)
  106. stations = StationMap([st1, st2])
  107. assert stations["abdl"] == st1
  108. def test_load_from_json(self, json_data):
  109. stations = StationMap.from_json(json_data)
  110. assert len(stations) == 3
  111. names = list(stations)
  112. import json
  113. js = json.loads(json_data)
  114. names_js = [item["code"] for item in js]
  115. assert names == names_js
  116. def test_save_to_json(self, json_data):
  117. text = StationMap.from_json(json_data).to_json()
  118. assert text == json_data
  119. def test_nearest_station(self, json_data):
  120. stations = StationMap.from_json(json_data)
  121. assert stations.nearest("abdl").code == "alek"