Example #1
0
    def test_get_parameters(self, mock_slot):
        mock_origin = Station('Train Town', 'TTX')
        mock_destination = Station('Train City', 'TCX')
        mock_slot.side_effect = [mock_origin, mock_destination]
        parameters = intents.get_parameters({}, {})
        expected_parameters = APIParameters(mock_origin, mock_destination, 0)

        self.assertEqual(mock_slot.call_count, 2)
        self.assertTupleEqual(parameters, expected_parameters)
Example #2
0
    def test_get_timetable_client_err(self, mock_date, _):
        mock_date.today.return_value = '2019-03-01'
        test_params = APIParameters(Station('Invalid query', 'XXX'),
                                    Station('_', '_'),
                                    offset=0)

        with self.assertRaises(ApplicationError) as context:
            data.get_timetable(test_params, '19:45')
        self.assertEqual('Request to TransportAPI failed - Not found',
                         str(context.exception))
Example #3
0
    def test_get_timetable_unknown_err(self, mock_date, _):
        mock_date.today.return_value = '2019-03-01'
        test_params = APIParameters(Station('Simulated new API version',
                                            'UPDATED'),
                                    Station('_', '_'),
                                    offset=0)

        with self.assertRaises(TransportAPIError) as context:
            data.get_timetable(test_params, '19:45')
        expected_err = 'TransportAPI responded in an unexpected way - \'departures\' not found in response'
        self.assertEqual(expected_err, str(context.exception))
Example #4
0
    def test_get_timetable_api_err(self, mock_date, _):
        mock_date.today.return_value = '2019-03-01'
        test_params = APIParameters(Station('Simulated API failure', 'BROKEN'),
                                    Station('_', '_'),
                                    offset=0)

        with self.assertRaises(TransportAPIError) as context:
            data.get_timetable(test_params, '19:45')
        self.assertEqual(
            'Request to TransportAPI failed - Internal server error',
            str(context.exception))
Example #5
0
    def test_get_parameters_origin_from_dynamodb(self, mock_db, mock_slot):
        mock_origin = Station('Home Town', 'HTX')
        mock_destination = Station('Train City', 'TCX')
        mock_session = {'user': {'userId': 'TEST_ID'}}
        mock_slot.side_effect = [None, mock_destination]
        mock_db.get_home_station.return_value = HomeStation(mock_origin, 10)

        parameters = intents.get_parameters({}, mock_session)
        expected_parameters = APIParameters(mock_origin, mock_destination, 10)

        mock_db.get_home_station.assert_called_with('TEST_ID')
        self.assertEqual(mock_slot.call_count, 2)
        self.assertTupleEqual(parameters, expected_parameters)
Example #6
0
    def test_update_home_station_err(self, mock_get, mock_boto3):
        mock_table = Mock()
        mock_response = {}

        mock_table.update_item.return_value = mock_response
        mock_boto3.return_value.Table.return_value = mock_table
        mock_get.return_value = HomeStation(
            Station('University (Birmingham', 'UNI'), 15)

        test_user_id = "existing_user"
        test_details = HomeStation(Station('Five Ways', 'FWY'), 10)

        with self.assertRaises(DynamoDBError) as context:
            dynamodb.set_home_station(test_user_id, test_details)

        self.assertEqual('DynamoDB failed to update home station',
                         str(context.exception))
Example #7
0
def get_station_from_slot(intent, slot_name):
    try:
        slot = intent['slots'][slot_name]['resolutions'][
            'resolutionsPerAuthority'][0]['values'][0]['value']
        station = Station(slot['name'], slot['id'])
        logger.debug('Station slot value found ({}): {} '.format(
            slot_name, station))
        return station
    except KeyError:
        logger.warning('Slot value not found: ' + slot_name)
        return None
Example #8
0
    def test_set_home_station(self, mock_db, mock_slot, mock_station):
        mock_session = {'user': {'userId': 'TEST_ID'}}
        mock_station.return_value = Station('Train Town', 'TTX')
        mock_slot.return_value = 10
        mock_db.set_home_station.return_value = 'set'
        expected_speech = 'Your home station has been set.'

        response = intents.set_home_station({}, mock_session)
        _test_response(response,
                       expected_speech,
                       expected_reprompt=None,
                       should_end_session=True)
Example #9
0
    def test_set_home_station_success(self, mock_get, mock_boto3):
        mock_table = Mock()
        mock_response = {'ResponseMetadata': {'HTTPStatusCode': 200}}

        mock_table.put_item.return_value = mock_response
        mock_boto3.return_value.Table.return_value = mock_table
        mock_get.return_value = None

        test_user_id = "new_user"
        test_details = HomeStation(Station('Five Ways', 'FWY'), 10)
        result = dynamodb.set_home_station(test_user_id, test_details)

        self.assertEqual(result, 'set')
Example #10
0
    def test_update_home_station_success(self, mock_get, mock_boto3):
        mock_table = Mock()
        mock_response = {
            'Attributes': {
                'station_crs': 'FWY',
                'station_name': 'Five Ways',
                'distance': '10'
            },
            'ResponseMetadata': {
                'HTTPStatusCode': 200
            }
        }

        mock_table.update_item.return_value = mock_response
        mock_boto3.return_value.Table.return_value = mock_table
        mock_get.return_value = HomeStation(
            Station('University (Birmingham', 'UNI'), 15)

        test_user_id = "existing_user"
        test_details = HomeStation(Station('Five Ways', 'FWY'), 10)
        result = dynamodb.set_home_station(test_user_id, test_details)

        self.assertEqual(result, 'updated')
Example #11
0
def get_home_station(user_id):

    db = boto3.resource('dynamodb', region_name='eu-west-1')
    table = db.Table('RailUK')

    response = table.get_item(Key={'UserID': user_id})
    logger.debug("DynamoDB GET response: \n" + str(response))

    if 'Item' not in response:
        logger.warning('DynamoDB returned no home station')
        return None

    details = response['Item']
    station = Station(details['station_name'], details['station_crs'])
    return HomeStation(station, int(details['distance']))
Example #12
0
    def test_set_home_station_err(self, mock_get, mock_boto3):
        mock_table = Mock()
        mock_response = {'ResponseMetadata': {'HTTPStatusCode': 500}}

        mock_table.put_item.return_value = mock_response
        mock_boto3.return_value.Table.return_value = mock_table
        mock_get.return_value = None

        test_user_id = "existing_user"
        test_details = HomeStation(Station('Five Ways', 'FWY'), 10)

        with self.assertRaises(DynamoDBError) as context:
            dynamodb.set_home_station(test_user_id, test_details)

        self.assertEqual('DynamoDB failed to set home station',
                         str(context.exception))
Example #13
0
    def test_get_home_station_success(self, mock_boto3):
        mock_table = Mock()
        mock_response = {
            'Item': {
                'UserID': 'existing_user',
                'distance': '10',
                'station_crs': 'FWY',
                'station_name': 'Five Ways'
            },
            'ResponseMetadata': {
                'HTTPStatusCode': 200
            }
        }
        mock_table.get_item.return_value = mock_response
        mock_boto3.return_value.Table.return_value = mock_table

        test_user_id = "existing_user"
        result = dynamodb.get_home_station(test_user_id)

        expected_result = HomeStation(Station('Five Ways', 'FWY'), 10)
        self.assertEqual(result, expected_result)
Example #14
0
 def test_get_station_from_slot_ok(self):
     test_intent = helpers.generate_test_intent()
     station = intents.get_station_from_slot(test_intent, 'home')
     expected_station = Station(name='Home Town', crs='HTX')
     self.assertTupleEqual(station, expected_station)
Example #15
0
def generate_test_api_params():
    return APIParameters(origin=Station('Home Town', 'HTX'),
                         destination=Station('Train Town', 'TTX'),
                         offset=0)