Exemplo n.º 1
0
    def test_get_geolocation_address(self, mock_googlemaps):
        client = mock_googlemaps.Client.return_value

        client.geocode.return_value = [
            {"geometry": {"location": {"lat": 45, "lng": -75}}}
        ]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertEqual(location, Point(45.0, -75.0))
        client.geocode.assert_called_once_with(address=ADDRESS)
Exemplo n.º 2
0
    def test_get_geolocation_postal_code(self, mock_googlemaps):
        client = mock_googlemaps.Client.return_value

        client.geocode.return_value = [
            {"geometry": {"location": {"lat": 45, "lng": -75}}}
        ]
        location = Geocoding().get_from_postal_code(postal_code=POSTAL_CODE)
        self.assertEqual(location, Point(45.0, -75.0))
        client.geocode.assert_called_once_with(
            components={"postal_code": "H0H0H0", "country": "CA"}
        )
Exemplo n.º 3
0
    def test_get_geolocation_missing_keys(self, mock_googlemaps):
        client = mock_googlemaps.Client.return_value

        client.geocode.return_value = [{}]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)

        client.geocode.return_value = [{"geometry": {}}]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)

        client.geocode.return_value = [{"geometry": {"location": {}}}]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)

        client.geocode.return_value = [{"geometry": {"location": {"lng": 45}}}]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)

        client.geocode.return_value = [{"geometry": {"location": {"lat": 45}}}]
        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)
Exemplo n.º 4
0
    def __init__(self):
        self.geocoding_client = Geocoding()

        super().__init__()
Exemplo n.º 5
0
async def validate_test_navigation_form_postal_code(
    value: Text,
    geocoding_client: Geocoding,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
) -> List[EventType]:
    postal_code = _get_postal_code(value)
    if postal_code is None:
        return _check_postal_code_error_counter(tracker, dispatcher)

    try:
        coordinates = geocoding_client.get_from_postal_code(postal_code)
        if coordinates is None:
            return _check_postal_code_error_counter(tracker, dispatcher)

        testing_locations = (
            _get_stub_testing_locations(tracker)
            if _must_stub_testing_locations(tracker)
            else await get_testing_locations(coordinates)
        )

    except Exception:
        logger.exception("Failed to fetch testing locations")
        dispatcher.utter_message(
            template="utter_test_navigation_form_locations_not_fetched_1"
        )
        dispatcher.utter_message(
            template="utter_test_navigation_form_locations_not_fetched_2"
        )
        return [
            SlotSet(POSTAL_CODE_SLOT, postal_code),
            SlotSet(REQUESTED_SLOT, None),
            SlotSet(TRY_DIFFERENT_ADDRESS_SLOT, SKIP_SLOT_PLACEHOLDER),
        ]

    if len(testing_locations) == 0:
        dispatcher.utter_message(template="utter_test_navigation_form_locations_none")
        return [SlotSet(POSTAL_CODE_SLOT, postal_code)]

    elif len(testing_locations) == 1:
        dispatcher.utter_message(template="utter_test_navigation_form_locations_one")
        dispatcher.utter_message(
            attachment=_locations_carousel(testing_locations, coordinates, domain)
        )

    else:
        dispatcher.utter_message(
            template="utter_test_navigation_form_locations_many_1",
            nb_testing_sites=len(testing_locations),
        )
        dispatcher.utter_message(template="utter_test_navigation_form_locations_many_2")
        dispatcher.utter_message(template="utter_test_navigation_form_locations_many_3")
        dispatcher.utter_message(
            attachment=_locations_carousel(testing_locations, coordinates, domain)
        )

    return [
        SlotSet(POSTAL_CODE_SLOT, postal_code),
        SlotSet(REQUESTED_SLOT, None),
        SlotSet(TRY_DIFFERENT_ADDRESS_SLOT, SKIP_SLOT_PLACEHOLDER),
    ]
Exemplo n.º 6
0
    def test_get_geolocation_empty(self, mock_googlemaps):
        client = mock_googlemaps.Client.return_value
        client.geocode.return_value = []

        location = Geocoding().get_from_address(ADDRESS)
        self.assertIsNone(location)
Exemplo n.º 7
0
 def test_missing_environment_key(self):
     with self.assertRaises(expected_exception=KeyError):
         Geocoding()
Exemplo n.º 8
0
class TestNavigationForm(FormAction):
    def __init__(self):
        self.geocoding_client = Geocoding()

        super().__init__()

    def name(self) -> Text:
        return FORM_NAME

    async def run(
        self,
        dispatcher,
        tracker,
        domain,
    ):
        bind_logger(tracker)
        return await super().run(dispatcher, tracker, domain)

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        if tracker.get_slot(END_FORM_SLOT) is True:
            return []

        if tracker.get_slot(LOCATIONS_SLOT):
            return [POSTAL_CODE_SLOT]

        return [POSTAL_CODE_SLOT, TRY_DIFFERENT_ADDRESS_SLOT]

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        return {
            POSTAL_CODE_SLOT: self.from_text(),
            TRY_DIFFERENT_ADDRESS_SLOT: yes_no_nlu_mapping(self),
        }

    def request_next_slot(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Optional[List[EventType]]:
        return request_next_slot(self, dispatcher, tracker, domain)

    async def validate_test_navigation__postal_code(
        self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        postal_code = _get_postal_code(value)
        if postal_code is None:
            return _check_postal_code_error_counter(tracker, dispatcher)

        try:
            coordinates = self.geocoding_client.get_from_postal_code(
                postal_code)
            if coordinates is None:
                return _check_postal_code_error_counter(tracker, dispatcher)

            testing_locations = (_get_stub_testing_locations(tracker)
                                 if _must_stub_testing_locations(tracker) else
                                 await get_testing_locations(coordinates))

        except Exception:
            logger.exception("Failed to fetch testing locations")
            dispatcher.utter_message(
                template="utter_test_navigation__could_not_fetch_1")
            dispatcher.utter_message(
                template="utter_test_navigation__could_not_fetch_2")
            return {POSTAL_CODE_SLOT: postal_code, END_FORM_SLOT: True}

        if len(testing_locations) == 0:
            dispatcher.utter_message(
                template="utter_test_navigation__no_locations")

        elif len(testing_locations) == 1:
            dispatcher.utter_message(
                template="utter_test_navigation__one_location")
            dispatcher.utter_message(attachment=_locations_carousel(
                testing_locations, coordinates, domain))

        else:
            dispatcher.utter_message(
                template="utter_test_navigation__many_locations_1",
                nb_testing_sites=len(testing_locations),
            )
            dispatcher.utter_message(
                template="utter_test_navigation__many_locations_2")
            dispatcher.utter_message(
                template="utter_test_navigation__many_locations_3")
            dispatcher.utter_message(attachment=_locations_carousel(
                testing_locations, coordinates, domain))

        return {
            POSTAL_CODE_SLOT: postal_code,
            LOCATIONS_SLOT:
            [location.raw_data for location in testing_locations],
        }

    @validate_boolean_slot
    def validate_test_navigation__try_different_address(
        self,
        value: Union[bool, str],
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        if value is True:
            return {POSTAL_CODE_SLOT: None, LOCATIONS_SLOT: None}

        dispatcher.utter_message(template="utter_test_navigation__acknowledge")
        return {}

    def submit(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict]:
        return CLEARED_SLOTS