-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters