Skip to content

Commit

Permalink
Improvement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bieniu committed Mar 21, 2020
1 parent 3bb6e42 commit 1c57f80
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def update(self): # pylint: disable=too-many-branches
# The GIOS server sends a null values for sensors several minutes before
# adding new data from measuring station. If the newest value is null
# we take the earlier value.
if len(sensor_data["values"]) > 0:
if len(sensor_data["values"]) > 0 or len(data) == 1:
if sensor_data["values"][0][ATTR_VALUE]:
data[sensor][ATTR_VALUE] = sensor_data["values"][0][ATTR_VALUE]
elif sensor_data.get("values")[1][ATTR_VALUE]:
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/data/sensor_invalid_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": "PM10",
"values": []
}
30 changes: 28 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,41 @@ async def test_no_sensor_data():


@pytest.mark.asyncio
async def test_invalid_sensor_data():
async def test_invalid_sensor_data_1():
"""Test with invalid sensor data."""
with open("tests/data/stations.json") as file:
stations = json.load(file)

with open("tests/data/station.json") as file:
station = json.load(file)

with open("tests/data/sensor_invalid.json") as file:
with open("tests/data/sensor_invalid_1.json") as file:
sensor = json.load(file)

with patch("gios.Gios._get_stations", return_value=stations), patch(
"gios.Gios._get_station", return_value=station
), patch("gios.Gios._get_sensor", return_value=sensor):

async with ClientSession() as websession:
gios = Gios(VALID_STATION_ID, websession)
await gios.update()

assert gios.station_name == VALID_STATION_NAME
assert gios.latitude == VALID_LATITUDE
assert gios.longitude == VALID_LONGITUDE
assert gios.available == False
assert gios.data == {}

@pytest.mark.asyncio
async def test_invalid_sensor_data_2():
"""Test with invalid sensor data."""
with open("tests/data/stations.json") as file:
stations = json.load(file)

with open("tests/data/station.json") as file:
station = json.load(file)

with open("tests/data/sensor_invalid_2.json") as file:
sensor = json.load(file)

with patch("gios.Gios._get_stations", return_value=stations), patch(
Expand Down

0 comments on commit 1c57f80

Please sign in to comment.