Skip to content

Commit

Permalink
Updated unit tests post changes to weather, maps and directions
Browse files Browse the repository at this point in the history
  • Loading branch information
keiffster committed Feb 8, 2018
1 parent dd21e10 commit bfdbc23
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/programy/clients/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _process_status_question(self, userid, text):
logging.debug(status)

if logging.getLogger().isEnabledFor(logging.DEBUG):
logging.debug("Sending status response [@%s] [%s]", (user.screen_name, response))
logging.debug("Sending status response [@%s] [%s]",user.screen_name, response)

self._api.update_status(status)

Expand Down
5 changes: 4 additions & 1 deletion src/programy/extensions/maps/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

class GoogleMapsExtension(Extension):

def get_geo_locator(self):
return GoogleMaps()

# execute() is the interface that is called from the <extension> tag in the AIML
def execute(self, bot, clientid, data):
if logging.getLogger().isEnabledFor(logging.DEBUG):
Expand All @@ -32,7 +35,7 @@ def execute(self, bot, clientid, data):
from_place = splits[1]
to_place = splits[2]

googlemaps = GoogleMaps(bot.license_keys)
googlemaps = self.get_geo_locator()

if command == "DISTANCE":
distance = googlemaps.get_distance_between_addresses(from_place, to_place)
Expand Down
19 changes: 14 additions & 5 deletions src/programy/parser/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def _is_chinese_char(c):
(0x31C0, 0x31EF)]
return any(s <= ord(c) <= e for s, e in r)

def _is_wildchar(self, ch):
MATCH_CHARS = ['^', '#', '', '*']
return bool(ch in MATCH_CHARS)

def texts_to_words(self, texts):
if not texts:
return []
Expand All @@ -63,16 +67,21 @@ def texts_to_words(self, texts):
if CjkTokenizer._is_chinese_char(ch):
if len(last_word) > 0:
words.append(last_word)
last_word = ch
else:
words.append(ch)
last_word = ''
words.append(ch)
else:
if ch == self.split_chars:
if self._is_wildchar(ch):
if len(last_word) > 0:
words.append(last_word)
last_word = ''
words.append(ch)
else:
last_word += ch
if ch == self.split_chars:
if len(last_word) > 0:
words.append(last_word)
last_word = ''
else:
last_word += ch

if len(last_word) > 0:
words.append(last_word)
Expand Down
4 changes: 2 additions & 2 deletions test/programytest/extensions/maps/maps.aiml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<template>
<srai>
SHOW_DISTANCE
<extension path="programy.extensions.maps.maps.GoogleMapsExtension">
<extension path="programytest.extensions.maps.test_aiml.MockGoogleMapsExtension">
DISTANCE <star index="1"/> <star index="2"/>
</extension>
</srai>
Expand Down Expand Up @@ -115,7 +115,7 @@
<template>
<srai>
SHOW_DIRECTIONS
<extension path="programy.extensions.maps.maps.GoogleMapsExtension">
<extension path="programytest.extensions.maps.test_aiml.MockGoogleMapsExtension">
DIRECTIONS <star index="1"/> <star index="2"/>
</extension>
</srai>
Expand Down
36 changes: 25 additions & 11 deletions test/programytest/extensions/maps/test_aiml.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import unittest
import os
import json

from programy.extensions.maps.maps import GoogleMapsExtension
from programy.utils.geo.google import GoogleMaps
from programytest.aiml_tests.client import TestClient
from programy.config.sections.brain.file import BrainFileConfiguration

class MockGoogleMaps(GoogleMaps):

def __init__(self, response_file):
self._response_file = response_file

def _get_response_as_json(self, url):
with open(self._response_file) as response_data:
return json.load(response_data)


class MockGoogleMapsExtension(GoogleMapsExtension):

response_file = None

def get_geo_locator(self):
return MockGoogleMaps(MockGoogleMapsExtension.response_file)

class MapsTestsClient(TestClient):

Expand All @@ -13,27 +32,22 @@ def load_configuration(self, arguments):
super(MapsTestsClient, self).load_configuration(arguments)
self.configuration.brain_configuration.files.aiml_files._files=[os.path.dirname(__file__)]


class MapsAIMLTests(unittest.TestCase):

def setUp (self):
MapsAIMLTests.test_client = MapsTestsClient()

latlong = os.path.dirname(__file__) + os.sep + "google_latlong.json"
distance = os.path.dirname(__file__) + os.sep + "distance.json"
directions = os.path.dirname(__file__) + os.sep + "directions.json"

MapsAIMLTests.test_client.bot.license_keys.load_license_key_data("""
GOOGLE_LATLONG=%s
GOOGLE_MAPS_DISTANCE=%s
GOOGLE_MAPS_DIRECTIONS=%s
"""%(latlong, distance, directions))

def test_distance(self):
MockGoogleMapsExtension.response_file = os.path.dirname(__file__) + os.sep + "distance.json"

response = MapsAIMLTests.test_client.bot.ask_question("testif", "DISTANCE EDINBURGH KINGHORN")
self.assertIsNotNone(response)
self.assertEqual(response, 'It is 25 . 1 miles')

def test_directions(self):
MockGoogleMapsExtension.response_file = os.path.dirname(__file__) + os.sep + "directions.json"

response = MapsAIMLTests.test_client.bot.ask_question("testif", "DIRECTIONS EDINBURGH KINGHORN")
self.assertIsNotNone(response)
self.assertTrue(response.startswith("To get there Head west on Leith St"))
52 changes: 37 additions & 15 deletions test/programytest/extensions/maps/test_maps.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,71 @@
import unittest
import unittest.mock
import os
import json

from programy.extensions.maps.maps import GoogleMapsExtension
from programy.utils.geo.google import GoogleDistance
from programytest.aiml_tests.client import TestClient
import os

from programy.utils.geo.google import GoogleMaps

class MockGoogleMaps(GoogleMaps):

def __init__(self, response_file):
self._response_file = response_file

def _get_response_as_json(self, url):
with open(self._response_file) as response_data:
return json.load(response_data)


class MockGoogleMapsExtension(GoogleMapsExtension):

def __init__(self, response_file):
self._response_file = response_file

def get_geo_locator(self):
return MockGoogleMaps(self._response_file)


class MapsExtensionTests(unittest.TestCase):

def setUp(self):
self.test_client = TestClient()

latlong = os.path.dirname(__file__) + os.sep + "google_latlong.json"
def test_maps_distance(self):
distance = os.path.dirname(__file__) + os.sep + "distance.json"
directions = os.path.dirname(__file__) + os.sep + "directions.json"

self.test_client.bot.license_keys.load_license_key_data("""
GOOGLE_LATLONG=%s
GOOGLE_MAPS_DISTANCE=%s
GOOGLE_MAPS_DIRECTIONS=%s
"""%(latlong, distance, directions))

def test_maps_distance(self):
googlemaps = GoogleMapsExtension()
googlemaps = MockGoogleMapsExtension(distance)
self.assertIsNotNone(googlemaps)

result = googlemaps.execute(self.test_client.bot, "testid", "DISTANCE EDINBURGH KINGHORN")
self.assertIsNotNone(result)
self.assertEquals("DISTANCE DEC 25 FRAC 1 UNITS miles", result)

def test_maps_direction(self):
googlemaps = GoogleMapsExtension()
directions = os.path.dirname(__file__) + os.sep + "directions.json"

googlemaps = MockGoogleMapsExtension(directions)
self.assertIsNotNone(googlemaps)

result = googlemaps.execute(self.test_client.bot, "testid", "DIRECTIONS EDINBURGH KINGHORN")
self.assertIsNotNone(result)
self.assertTrue(result.startswith("DIRECTIONS Head west on Leith St/A900 toward Leith"))

def test_maps_unknown(self):
googlemaps = GoogleMapsExtension()
latlong = os.path.dirname(__file__) + os.sep + "google_latlong.json"

googlemaps = MockGoogleMapsExtension(latlong)
self.assertIsNotNone(googlemaps)

result = googlemaps.execute(self.test_client.bot, "testid", "SOMETHINGELSE EDINBURGH KINGHORN")
self.assertIsNone(result)

def test_format_distance_for_programy(self):
googlemaps = GoogleMapsExtension()
distance = os.path.dirname(__file__) + os.sep + "distance.json"

googlemaps = MockGoogleMapsExtension(distance)
self.assertIsNotNone(googlemaps)

distance = GoogleDistance("here", "there")
Expand All @@ -57,7 +77,9 @@ def test_format_distance_for_programy(self):
self.assertEquals("DISTANCE DEC 22 FRAC 45 UNITS km", googlemaps._format_distance_for_programy(distance))

def test_format_directions_for_programy(self):
googlemaps = GoogleMapsExtension()
directions = os.path.dirname(__file__) + os.sep + "directions.json"

googlemaps = MockGoogleMapsExtension(directions)
self.assertIsNotNone(googlemaps)

directions = unittest.mock.Mock()
Expand Down
61 changes: 55 additions & 6 deletions test/programytest/extensions/weather/test_aiml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
import unittest
import os
import json

from programy.extensions.weather.weather import WeatherExtension
from programytest.aiml_tests.client import TestClient
from programy.utils.weather.metoffice import MetOffice
from programy.utils.geo.google import GoogleMaps

class MockGoogleMaps(GoogleMaps):

def __init__(self, response_file):
self._response_file = response_file

def _get_response_as_json(self, url):
with open(self._response_file) as response_data:
return json.load(response_data)


class MockMetOffice(MetOffice):

def __init__(self, response_file):
self._response_file = response_file

def _get_response_as_json(self):
with open(self._response_file) as response_data:
return json.load(response_data)

def get_forecast_data(self, lat, lon, forecast_type):
return self._get_response_as_json()

def get_observation_data(self, lat, lon):
return self._get_response_as_json()


class MockWeatherExtension(WeatherExtension):

maps_file = None
weather_file = None

def get_geo_locator(self, bot):
return MockGoogleMaps(MockWeatherExtension.maps_file)

def get_met_office(self, bot):
return MockMetOffice(MockWeatherExtension.weather_file)


class WeathersTestsClient(TestClient):
Expand All @@ -15,19 +57,26 @@ def load_configuration(self, arguments):

class WeathersAIMLTests(unittest.TestCase):


def setUp (self):
WeathersAIMLTests.test_client = WeathersTestsClient()
WeathersAIMLTests.test_client.bot.license_keys.load_license_key_data("""
METOFFICE_API_KEY=TESTKEY
""")
WeathersAIMLTests.test_client.bot.get_conversation("testid").properties["active"] = "true"

def test_weather(self):
latlong = os.path.dirname(__file__) + os.sep + "google_latlong.json"
observation = os.path.dirname(__file__) + os.sep + "observation.json"
MockWeatherExtension.maps_file = os.path.dirname(__file__) + os.sep + "google_latlong.json"
MockWeatherExtension.weather_file = os.path.dirname(__file__) + os.sep + "observation.json"
threehourly = os.path.dirname(__file__) + os.sep + "forecast_3hourly.json"
daily = os.path.dirname(__file__) + os.sep + "forecast_daily.json"

response = WeathersAIMLTests.test_client.bot.ask_question("testid", "WEATHER LOCATION KY39UR WHEN TODAY")
self.assertIsNotNone(response)
self.assertEqual(response, "Today the weather is Partly cloudy (day) , with a temperature of 12 dot 3 \'C")
self.assertEqual(response, "According to the UK Met Office, this it is partly cloudy (day) , with a temperature of around 12dot3'C , humidty is 57.3% , with pressure at 1017mb and falling .")

"""
WEATHER IN *
WEATHER LOCATION * WHEN *
FORECAST LOCATION * DAY *
FORECAST LOCATION * TIME *
WILL IT RAIN TODAY IN *
WILL IT RAIN TOMORROW IN *
"""
Loading

0 comments on commit bfdbc23

Please sign in to comment.