123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import pytest
- from copy import deepcopy
- from plib import Station, StationError, StationMap
- @pytest.fixture
- def js1():
- js = {
- "code": "abkn",
- "height": 219.23325751908123,
- "status": "new",
- "location": {
- "lat": 0.0,
- "lon": 15.5
- },
- "xyz": [
- -90768.799,
- 3777267.336,
- 5121588.168
- ]
- }
- return js
- @pytest.fixture
- def js2():
- js = {
- "code": "abkn",
- "height": 219.23325751908123,
- "status": "new",
- "location": {
- "lat": 0.0,
- "lon": 0.0
- },
- "xyz": [
- -90768.799,
- 3777267.336,
- 5121588.168
- ]
- }
- return js
- @pytest.fixture
- def json_data():
- text = [
- {
- "code": "akya",
- "height": 330.27589765004814,
- "status": "new",
- "location": {
- "lat": 0.0,
- "lon": 0.0
- },
- "xyz": [
- 2078592.977,
- 3354438.194,
- 4994390.812
- ]
- },
- {
- "code": "alek",
- "height": 174.47401913441718,
- "location": {
- "lat": 10.0,
- "lon": 0.0
- },
- "status": "new",
- "xyz": [
- 2959560.1322,
- 2236904.702,
- 5171064.6737
- ]
- },
- {
- "code": "abdl",
- "height": 86.10842919163406,
- "status": "new",
- "location": {
- "lat": 15.5,
- "lon": 15.5
- },
- "xyz": [
- 3025653.429,
- 2742460.808,
- 4883170.747
- ]
- }
- ]
- return text
- class TestStation:
- def test_creation(self, js1):
- try:
- Station(**js1)
- except Exception as e:
- pytest.fail(f"Failed with exception '{type(e)} - {e}'")
- for field in ["code", "height", "status", "location"]:
- js_copy = deepcopy(js1)
- del js1["location"]
- with pytest.raises(StationError):
- Station(**js1)
- def test_manual_creator(self):
- st = Station()
- def test_distance_to(self, js1, js2):
- s1 = Station(**js1)
- s2 = Station(**js2)
- assert s1.distance_to(s2) == 15.5
- class TestStationMap:
- def test_creation(self, js1, js2):
- st1 = Station(**js1)
- st2 = Station(**js2)
- stations = StationMap([st1, st2])
- assert stations["abdl"] == st1
- def test_load_from_json(self, json_data):
- stations = StationMap.from_json(json_data)
- assert len(stations) == 3
- names = list(stations)
- import json
- js = json.loads(json_data)
- names_js = [item["code"] for item in js]
- assert names == names_js
- def test_save_to_json(self, json_data):
- text = StationMap.from_json(json_data).to_json()
- assert text == json_data
- def test_nearest_station(self, json_data):
- stations = StationMap.from_json(json_data)
- assert stations.nearest("abdl").code == "alek"
|