def test_recast_memory_recast_offline_2(mock_get, mock_put: MagicMock, recast_memory_request): mock_get.return_value = Mock(status_code=500) with pytest.raises(ExternalAPIException): recast_send_request_memory(field=recast_memory_request['field'], user_id=recast_memory_request['user_id'], value=recast_memory_request['value']) assert mock_get.call_count == 1 assert mock_put.call_count == 0
def test_recast_memory_invalid_credentials_2(mock_get, mock_put: MagicMock, recast_memory_request): mock_get.return_value = Mock(status_code=401) with pytest.raises(InvalidCredentialsException): recast_send_request_memory(field=recast_memory_request['field'], user_id=recast_memory_request['user_id'], value=recast_memory_request['value']) assert mock_get.call_count == 1 assert mock_put.call_count == 0
def test_recast_memory_send_request(mock_put, mock_get, recast_memory_request, recast_memory_response): mock_get.return_value = Mock( status_code=200, json=Mock(return_value=recast_memory_response)) mock_put.return_value = Mock( status_code=200, json=Mock(return_value=recast_memory_response)) res = recast_send_request_memory(field=recast_memory_request['field'], user_id=recast_memory_request['user_id'], value=recast_memory_request['value']) assert mock_get.call_count == 1 assert mock_put.call_count == 1 assert res['results']['conversation_id'] == recast_memory_request[ 'user_id'] assert res['results']['memory'][recast_memory_request['field']]
def test_recast_memory_conversation_doesnt_exist( mock_get, mock_put, mock_recast_send_request_dialog, recast_memory_request, recast_memory_response): mock_get.side_effect = [ Mock(status_code=404, json=Mock(return_value=recast_memory_response)), Mock(status_code=200, json=Mock(return_value=recast_memory_response)) ] mock_put.return_value = Mock( status_code=200, json=Mock(return_value=recast_memory_response)) res = recast_send_request_memory(field=recast_memory_request['field'], user_id=recast_memory_request['user_id'], value=recast_memory_request['value']) assert mock_recast_send_request_dialog.call_count == 1 assert mock_get.call_count == 2 assert mock_put.call_count == 1 assert res['results']['conversation_id'] == recast_memory_request[ 'user_id'] assert res['results']['memory'][recast_memory_request['field']]
def test_recast_memory_field_is_null(mock_get, mock_put: MagicMock, recast_memory_request, recast_memory_response): def side_effect(**kwargs): assert kwargs.get('data') data = json.loads(kwargs['data']) assert data.get('memory') is not None and not data['memory'].get( recast_memory_request['field']) return Mock(status_code=200, json=Mock(return_value=recast_memory_response)) mock_get.return_value = Mock( status_code=200, json=Mock(return_value=recast_memory_response)) mock_put.side_effect = side_effect res = recast_send_request_memory(field=recast_memory_request['field'], user_id=recast_memory_request['user_id']) assert mock_get.call_count == 1 assert mock_put.call_count == 1 assert res['results']['conversation_id'] == recast_memory_request[ 'user_id']
def memory(): errors = [] if request.json: if 'field' not in request.json: errors.append(dict(MissingParameterException('field'))) if 'user_id' not in request.json: errors.append(dict(MissingParameterException('user_id'))) if errors: return jsonify({'errors': errors}), 400 field = request.json['field'] if field not in SUPPORTED_FIELDS: return jsonify({ 'errors': [ dict( BadParameterException('field', valid_values=SUPPORTED_FIELDS)) ] }), 400 value = request.json.get('value') user_id = request.json['user_id'] try: res = recast_send_request_memory(field, user_id, value) return jsonify(res['results']), 200 except InvalidCredentialsException as e: return jsonify({'errors': [dict(e)]}), e.status_code except ExternalAPIException as e: return jsonify({'errors': [dict(e)]}), e.status_code except Exception as e: logger.error(e) return jsonify({ 'errors': [dict(APIException('nlp_memory'.format(type(e).__name__, e)))] }), 500 else: errors.append(dict(APIException('no_content'))) return jsonify({'errors': errors}), 400