forked from codeforboston/BrooklineVoiceApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented school district intent codeforboston#15
- Loading branch information
Showing
6 changed files
with
224 additions
and
0 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 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
65 changes: 65 additions & 0 deletions
65
brooklinevoiceapp/mycity/intents/school_district_intent.py
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,65 @@ | ||
"""Alexa intent used to find school district associated with address""" | ||
|
||
import logging | ||
|
||
from mycity.intents import intent_constants | ||
from mycity.mycity_response_data_model import MyCityResponseDataModel | ||
from mycity.utils.address_utils import set_address_in_session | ||
from mycity.utils.brookline_arcgis_api_utils import get_sorted_school_district_json | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
CARD_TITLE_SCHOOL_DISTRICT = "Nearest School District" | ||
|
||
OUTPUT_SPEECH_TEMPLATE = \ | ||
"The nearest school district to you is {}." | ||
|
||
FEATURES_PATH = "features" | ||
ATTRIBUTES_PATH = "attributes" | ||
NAME_PATH = "NAME" | ||
|
||
def find_closest_school_district(mycity_request): | ||
""" | ||
Finds the closest school district in Brookline | ||
to a given address | ||
:param mycity_request: MyCityRequestDataModel object | ||
:return: MyCityResponseDataModel object | ||
""" | ||
logger.debug('Finding closest school district') | ||
|
||
response = MyCityResponseDataModel() | ||
set_address_in_session(mycity_request) | ||
current_address = mycity_request \ | ||
.session_attributes \ | ||
.get(intent_constants.CURRENT_ADDRESS_KEY) | ||
logger.debug(current_address) | ||
if current_address is None: | ||
response.dialog_directive = "Delegate" | ||
else: | ||
response.output_speech = _get_output_speech_for_address(current_address) | ||
response.card_title = CARD_TITLE_SCHOOL_DISTRICT | ||
|
||
return response | ||
|
||
|
||
def _get_output_speech_for_address(address): | ||
""" | ||
Gets the API response and builds an output speech string | ||
:param address: Current address | ||
:return: Output speech string | ||
""" | ||
|
||
logger.debug("Getting response for address " + str(address)) | ||
features = get_sorted_school_district_json(address) | ||
logger.debug("school district response:", features) | ||
|
||
try: | ||
first_feature = features[0] | ||
logger.debug(first_feature) | ||
facility_name = first_feature[ATTRIBUTES_PATH][NAME_PATH] | ||
except IndexError: | ||
return intent_constants.NO_RESULTS_RESPONSE | ||
|
||
return OUTPUT_SPEECH_TEMPLATE.format(facility_name) |
50 changes: 50 additions & 0 deletions
50
brooklinevoiceapp/mycity/test/integration_tests/test_school_district_intent.py
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,50 @@ | ||
import copy | ||
import logging | ||
|
||
from mycity.intents import ( | ||
intent_constants, | ||
school_district_intent as ps_intent, | ||
) | ||
from mycity.test import test_constants | ||
from mycity.test.integration_tests import ( | ||
intent_base_case as base_case, | ||
intent_test_mixins as mix_ins, | ||
) | ||
|
||
############################################ | ||
# TestCase class for school_district_intent # | ||
############################################ | ||
|
||
MOCK_RESPONSE = test_constants.GET_SCHOOL_DISTRICT_API_MOCK | ||
|
||
NO_RESULTS_RESPONSE = intent_constants.NO_RESULTS_RESPONSE | ||
|
||
FEATURES = ps_intent.FEATURES_PATH | ||
ATTRIBUTES = ps_intent.ATTRIBUTES_PATH | ||
NAME = ps_intent.NAME_PATH | ||
|
||
class SchoolDistrictTestCase(mix_ins.RepromptTextTestMixIn, | ||
mix_ins.CardTitleTestMixIn, | ||
base_case.IntentBaseCase): | ||
intent_to_test = "SchoolDistrictIntent" | ||
expected_title = ps_intent.CARD_TITLE_SCHOOL_DISTRICT | ||
returns_reprompt_text = False | ||
|
||
def setUp(self): | ||
""" | ||
Patching out the functions in SchoolDistrictIntent that use requests.get | ||
""" | ||
super().setUp() | ||
self.mock_requests(get_geocode_data=copy.deepcopy(test_constants.GET_ADDRESS_CANDIDATES_API_MOCK), | ||
get_data=copy.deepcopy(test_constants.GET_SCHOOL_DISTRICT_API_MOCK)) | ||
|
||
def testResponseContainsName(self): | ||
response = self.controller.on_intent(self.request) | ||
for feature in MOCK_RESPONSE[FEATURES]: | ||
self.assertIn(feature[ATTRIBUTES][NAME], response.output_speech) | ||
|
||
def testNoFeatureResults(self): | ||
self.mock_requests(get_geocode_data=copy.deepcopy(test_constants.GET_ADDRESS_CANDIDATES_API_MOCK), | ||
get_data=copy.deepcopy(test_constants.NO_RESULTS_GET_SCHOOL_DISTRICT_API_MOCK)) | ||
response = self.controller.on_intent(self.request) | ||
self.assertEqual(response.output_speech, NO_RESULTS_RESPONSE) |
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