Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
pguyot committed Dec 16, 2019
2 parents abd18e1 + fd1457e commit ebbf94f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 32 deletions.
10 changes: 4 additions & 6 deletions nabairqualityd/nabairqualityd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dateutil.parser
from asgiref.sync import sync_to_async
from nabcommon.nabservice import NabInfoCachedService
import logging
from . import aqicn


Expand Down Expand Up @@ -120,13 +119,12 @@ async def process_nabd_packet(self, packet):
packet["type"] == "asr_event"
and packet["nlu"]["intent"] == "airquality_forecast"
):
next_date, next_args, config = self.get_config()
next_date, next_args, config_t = await sync_to_async(
self.get_config
)()
now = datetime.datetime.now(datetime.timezone.utc)
expiration = now + datetime.timedelta(minutes=1)
info_data = self.fetch_info_data(config)
await self.perform_additional(
expiration, "today", info_data, config
)
await self.perform(expiration, "today", config_t)


if __name__ == "__main__":
Expand Down
3 changes: 0 additions & 3 deletions nabairqualityd/tests.py

This file was deleted.

19 changes: 19 additions & 0 deletions nabairqualityd/tests/aqicn_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from nabairqualityd import aqicn


class TestAQICN(unittest.TestCase):
def do_test_index(self, index):
client = aqicn.aqicnClient(index)
client.update()
airquality = client.get_data()
self.assertTrue(isinstance(airquality, int))
self.assertTrue(airquality >= 0)
self.assertTrue(airquality <= 2)
city = client.get_city()
self.assertTrue(isinstance(city, str))

def test_indexes(self):
for index in range(0, 2):
self.do_test_index(index)
87 changes: 87 additions & 0 deletions nabairqualityd/tests/nabairqualityd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import unittest
import json
import django
import time
import datetime
import pytest
from asgiref.sync import async_to_sync
from nabairqualityd.nabairqualityd import NabAirqualityd
from nabairqualityd import models


class MockWriter(object):
def __init__(self):
self.written = []

def write(self, packet):
self.written.append(packet)

async def drain(self):
pass


@pytest.mark.django_db(transaction=True)
class TestNabAirqualityd(unittest.TestCase):
def test_fetch_info_data(self):
config = models.Config.load()
config.index_airquality = "aqi"
config.localisation = None
config.save()
service = NabAirqualityd()
data = async_to_sync(service.fetch_info_data)("aqi")
config = models.Config.load()
self.assertIsNotNone(data)
self.assertTrue(data < 3)
self.assertTrue(data >= 0)
self.assertIsNotNone(config.localisation)

def test_perform(self):
config = models.Config.load()
config.index_airquality = "aqi"
config.localisation = None
config.save()
service = NabAirqualityd()
writer = MockWriter()
service.writer = writer
config_t = "aqi"
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
async_to_sync(service.perform)(expiration, "today", config_t)
self.assertEqual(len(writer.written), 2)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabairqualityd")
self.assertTrue("animation" in packet_json)
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)

def test_asr(self):
config = models.Config.load()
config.index_airquality = "aqi"
config.localisation = None
config.save()
service = NabAirqualityd()
writer = MockWriter()
service.writer = writer
config_t = "aqi"
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
packet = {
"type": "asr_event",
"nlu": {"intent": "airquality_forecast"},
}
async_to_sync(service.process_nabd_packet)(packet)
print(writer.written)
self.assertEqual(len(writer.written), 2)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabairqualityd")
self.assertTrue("animation" in packet_json)
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)
9 changes: 5 additions & 4 deletions nabcommon/nabservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,12 @@ def compute_next(self, saved_date, saved_args, config, reason):
next_date = self.next_info_update(config)
return next_date, "info"

def _do_fetch_info_data(self, config):
async def _do_fetch_info_data(self, config):
"""
Invokes fetch_info_data, used by NabInfoCachedService subclass.
"""
return self.fetch_info_data(config)
info_data = await self.fetch_info_data(config)
return info_data


class NabInfoCachedService(NabInfoService, ABC):
Expand All @@ -481,7 +482,7 @@ def __init__(self):
self.cached_info_config = None
self.cached_info_expdate = None

def _do_fetch_info_data(self, config):
async def _do_fetch_info_data(self, config):
"""
Fetch the info data from whatever source, using config, caching it
locally.
Expand All @@ -495,7 +496,7 @@ def _do_fetch_info_data(self, config):
):
return self.cached_info
next_hour = now + datetime.timedelta(seconds=3600)
new_info = self.fetch_info_data(config)
new_info = await self.fetch_info_data(config)
self.cached_info = new_info
self.cached_info_config = config
self.cached_info_expdate = next_hour
Expand Down
26 changes: 11 additions & 15 deletions nabweatherd/nabweatherd.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,13 @@ async def perform_additional(self, expiration, type, info_data, config_t):
location, unit = config_t
if location is None:
logging.debug(f"location is None (service is unconfigured)")
if type != "info":
packet = (
'{"type":"message",'
'"signature":{"audio":['
'"nabweatherd/signature.mp3"]},'
'"body":[{"audio":["nabweatherd/no-location-error.mp3"]}],'
'"expiration":"' + expiration.isoformat() + '"}\r\n'
)
self.writer.write(packet.encode("utf8"))
packet = '{"type":"info","info_id":"weather"}\r\n'
packet = (
'{"type":"message",'
'"signature":{"audio":['
'"nabweatherd/signature.mp3"]},'
'"body":[{"audio":["nabweatherd/no-location-error.mp3"]}],'
'"expiration":"' + expiration.isoformat() + '"}\r\n'
)
self.writer.write(packet.encode("utf8"))
else:
if type == "today":
Expand Down Expand Up @@ -471,14 +468,13 @@ async def process_nabd_packet(self, packet):
packet["type"] == "asr_event"
and packet["nlu"]["intent"] == "weather_forecast"
):
next_date, next_args, config = self.get_config()
next_date, next_args, config_t = await sync_to_async(
self.get_config
)()
# todo : detect today/tomorrow
now = datetime.datetime.now(datetime.timezone.utc)
expiration = now + datetime.timedelta(minutes=1)
info_data = self.fetch_info_data(config)
await self.perform_additional(
expiration, "today", info_data, config
)
await self.perform(expiration, "today", config_t)


if __name__ == "__main__":
Expand Down
30 changes: 26 additions & 4 deletions nabweatherd/tests/nabweatherd_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import unittest
import asyncio
import threading
import json
import django
import time
import datetime
import signal
import pytest
from asgiref.sync import async_to_sync
from nabweatherd.nabweatherd import NabWeatherd
from nabweatherd import models


class MockWriter(object):
Expand All @@ -22,7 +20,6 @@ async def drain(self):
pass


@pytest.mark.django_db
class TestNabWeatherd(unittest.TestCase):
def test_fetch_info_data(self):
service = NabWeatherd()
Expand Down Expand Up @@ -62,3 +59,28 @@ def test_aliases(self):
self.assertEqual(weather_class, "J_W1_0-N_1")
weather_class = service.normalize_weather_class("J_W2_4-N_1")
self.assertEqual(weather_class, "J_W1_3-N_0")


@pytest.mark.django_db(transaction=True)
class TestNabWeatherdDB(unittest.TestCase):
def test_asr(self):
config = models.Config.load()
config.location = "75005"
config.unit = NabWeatherd.UNIT_CELSIUS
config.save()
service = NabWeatherd()
writer = MockWriter()
service.writer = writer
packet = {"type": "asr_event", "nlu": {"intent": "weather_forecast"}}
async_to_sync(service.process_nabd_packet)(packet)
self.assertEqual(len(writer.written), 2)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd")
self.assertTrue("animation" in packet_json)
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)

0 comments on commit ebbf94f

Please sign in to comment.