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))
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)
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')
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')
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']))
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))
def set_home_station(intent, session): user_id = session['user']['userId'] station = get_station_from_slot(intent, 'home') distance = get_slot_value(intent, 'distance') details = HomeStation(station, distance) result = dynamodb.set_home_station(user_id, details) speech = 'Your home station has been {}.'.format(result) session_attributes = {} return build_response( session_attributes, build_speechlet_response(speech, reprompt=None, should_end_session=True))
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)