Skip to content

Commit cdf6446

Browse files
committed
Add tests
1 parent bd67e40 commit cdf6446

File tree

6 files changed

+163
-21
lines changed

6 files changed

+163
-21
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ end_of_line = lf
66
indent_style = space
77
insert_final_newline = true
88
trim_trailing_whitespace = true
9-
indent_size = 2
9+
indent_size = 4
1010

1111
[*.md]
1212
trim_trailing_whitespace = false

src/opensky/opensky.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
import asyncio
5-
import json
65
import socket
76
from dataclasses import dataclass
87
from importlib import metadata
@@ -90,19 +89,6 @@ async def _request(
9089
raise OpenSkyConnectionError(msg) from exception
9190

9291
content_type = response.headers.get("Content-Type", "")
93-
if (response.status // 100) in [4, 5]:
94-
contents = await response.read()
95-
response.close()
96-
97-
if content_type == "application/json":
98-
raise OpenSkyError(
99-
response.status,
100-
json.loads(contents.decode("utf8")),
101-
)
102-
raise OpenSkyError(
103-
response.status,
104-
{"message": contents.decode("utf8")},
105-
)
10692

10793
if "application/json" not in content_type:
10894
text = await response.text()

tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
"""Tests for the OpenSky Library."""
2+
from pathlib import Path
3+
4+
5+
def load_fixture(filename: str) -> str:
6+
"""Load a fixture."""
7+
path = Path(__package__) / "fixtures" / filename
8+
return path.read_text(encoding="utf-8")

tests/fixtures/states.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"states": [
3+
["ab1644", "UAL421 ", "United States", 1683488743, 1683488743, -71.1656, 42.5372, 2217.42, false, 137.8, 342.17, 13, null, 2194.56, null, false, 0, 4],
4+
["e8027e", "LPE2264 ", "Chile", 1683488479, 1683488722, -77.1296, -12.0115, null, true, 83.29, 149.16, 9.1, null, null, null, false, 0, 1],
5+
["e8027d", "LPE2392 ", "Chile", 1683488743, 1683488743, -76.9275, -11.905, 4335.78, false, 187.32, 332.17, 10.4, null, null, null, false, 0, 1],
6+
["a2cbe1", "N28CN ", "United States", 1683488743, 1683488743, -93.9953, 44.9762, 701.04, false, 73.29, 293.15, -4.55, null, 685.8, null, false, 0, 9]
7+
],
8+
"time": 1683488744
9+
}

tests/test.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/test_states.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"""Tests for the OpenSky Library."""
2+
# pylint: disable=protected-access
3+
import asyncio
4+
5+
import aiohttp
6+
import pytest
7+
from aiohttp import ClientError
8+
from aresponses import Response, ResponsesMockServer
9+
10+
from opensky import (
11+
AircraftCategory,
12+
OpenSky,
13+
OpenSkyConnectionError,
14+
OpenSkyError,
15+
PositionSource,
16+
StatesResponse,
17+
)
18+
19+
from . import load_fixture
20+
21+
22+
async def test_states(
23+
aresponses: ResponsesMockServer,
24+
) -> None:
25+
"""Test retrieving states."""
26+
aresponses.add(
27+
"opensky-network.org",
28+
"/api/states/all",
29+
"GET",
30+
aresponses.Response(
31+
status=200,
32+
headers={"Content-Type": "application/json"},
33+
text=load_fixture("states.json"),
34+
),
35+
)
36+
async with aiohttp.ClientSession() as session:
37+
opensky = OpenSky(session=session)
38+
response: StatesResponse = await opensky.states()
39+
assert len(response.states) == 4
40+
assert response.time == 1683488744
41+
first_aircraft = response.states[0]
42+
assert first_aircraft.icao24 == "ab1644"
43+
assert first_aircraft.callsign == "UAL421 "
44+
assert first_aircraft.origin_country == "United States"
45+
assert first_aircraft.time_position == 1683488743
46+
assert first_aircraft.last_contact == 1683488743
47+
assert first_aircraft.longitude == -71.1656
48+
assert first_aircraft.latitude == 42.5372
49+
assert first_aircraft.barometric_altitude == 2217.42
50+
assert not first_aircraft.on_ground
51+
assert first_aircraft.velocity == 137.8
52+
assert first_aircraft.true_track == 342.17
53+
assert first_aircraft.vertical_rate == 13
54+
assert first_aircraft.sensors is None
55+
assert first_aircraft.geo_altitude == 2194.56
56+
assert first_aircraft.transponder_code is None
57+
assert not first_aircraft.special_purpose_indicator
58+
assert first_aircraft.position_source == PositionSource.ADSB
59+
assert first_aircraft.category == AircraftCategory.LARGE
60+
await opensky.close()
61+
62+
63+
async def test_new_session(
64+
aresponses: ResponsesMockServer,
65+
) -> None:
66+
"""Test that it creates a new session if not given one."""
67+
aresponses.add(
68+
"opensky-network.org",
69+
"/api/states/all",
70+
"GET",
71+
aresponses.Response(
72+
status=200,
73+
headers={"Content-Type": "application/json"},
74+
text=load_fixture("states.json"),
75+
),
76+
)
77+
async with OpenSky() as opensky:
78+
assert not opensky.session
79+
await opensky.states()
80+
assert opensky.session
81+
82+
83+
async def test_timeout(aresponses: ResponsesMockServer) -> None:
84+
"""Test request timeout."""
85+
86+
# Faking a timeout by sleeping
87+
async def response_handler(_: aiohttp.ClientResponse) -> Response:
88+
"""Response handler for this test."""
89+
await asyncio.sleep(2)
90+
return aresponses.Response(body="Goodmorning!")
91+
92+
aresponses.add(
93+
"opensky-network.org",
94+
"/api/states/all",
95+
"GET",
96+
response_handler,
97+
)
98+
99+
async with aiohttp.ClientSession() as session:
100+
opensky = OpenSky(session=session, request_timeout=1)
101+
with pytest.raises(OpenSkyConnectionError):
102+
assert await opensky.states()
103+
await opensky.close()
104+
105+
106+
async def test_request_error(aresponses: ResponsesMockServer) -> None:
107+
"""Test request error."""
108+
109+
async def response_handler(_: aiohttp.ClientResponse) -> Response:
110+
"""Response handler for this test."""
111+
raise ClientError
112+
113+
aresponses.add(
114+
"opensky-network.org",
115+
"/api/states/all",
116+
"GET",
117+
response_handler,
118+
)
119+
120+
async with aiohttp.ClientSession() as session:
121+
opensky = OpenSky(session=session)
122+
with pytest.raises(OpenSkyConnectionError):
123+
assert await opensky.states()
124+
await opensky.close()
125+
126+
127+
async def test_unexpected_server_response(
128+
aresponses: ResponsesMockServer,
129+
) -> None:
130+
"""Test handling a server error."""
131+
aresponses.add(
132+
"opensky-network.org",
133+
"/api/states/all",
134+
"GET",
135+
aresponses.Response(
136+
status=200,
137+
headers={"Content-Type": "plain/text"},
138+
text="Yes",
139+
),
140+
)
141+
142+
async with aiohttp.ClientSession() as session:
143+
opensky = OpenSky(session=session)
144+
with pytest.raises(OpenSkyError):
145+
assert await opensky.states()
146+
await opensky.close()

0 commit comments

Comments
 (0)