"""Tests for main pythonfeatures module.""" from unittest.mock import MagicMock from owsrequest import error_response from owsrequest.constants import headers from owsrequest.context import RequestContext import pytest from requests.structures import CaseInsensitiveDict from pythonfeatures import pythonfeatures from pythonfeatures.logic import features as features_logic @pytest.mark.parametrize(( 'request_headers', 'expected_response', 'profile_context_called', 'account_context_called', 'call_args_str'), [ # Error request headers (missing profile id) ( { headers.ORCHARD_PROFILE_TYPE: headers.PROFILE_TYPE_INSIGHTS }, error_response.create_error_incomplete_profile_headers(), False, False, None ), # Success profile type headers ( { headers.ORCHARD_PROFILE_TYPE: headers.PROFILE_TYPE_INSIGHTS, headers.ORCHARD_PROFILE_ID: '1111' }, None, True, False, "call('InsightsProfile', '1111', None)" ), # Success account type headers ( { headers.ORCHARD_USER_ID: 'oa:456' }, None, False, True, "call('oa:456', None, None, None)" ), # None type headers ( {}, None, False, True, 'call(None, None, None)' ), ]) def test_get_all_features( monkeypatch, request_headers, expected_response, profile_context_called, account_context_called, call_args_str ): """Test get all features.""" monkeypatch.setattr( features_logic, 'get_all_features_with_profile_context', MagicMock()) monkeypatch.setattr( features_logic, 'get_all_features_with_account_context', MagicMock()) result = pythonfeatures.get_all_features(RequestContext( CaseInsensitiveDict(request_headers))) if expected_response is not None: assert result.status == expected_response.status assert result.errors == expected_response.errors if profile_context_called: assert str( features_logic.get_all_features_with_profile_context.call_args ) == call_args_str assert features_logic.get_all_features_with_profile_context.called if account_context_called: assert str( features_logic.get_all_features_with_account_context.call_args ) == call_args_str assert features_logic.get_all_features_with_account_context.called def test_get_all_features_typeerror(): """Test Exception thrown if request_context is not ReqestContext.""" try: pythonfeatures.get_all_features('pizza') except TypeError as er: assert str(er) == 'request_context must be class RequestContext' @pytest.mark.parametrize(( 'request_headers', 'expected_response', 'profile_context_called', 'account_context_called', 'call_args_str'), [ # Error request headers (missing profile id) ( { headers.ORCHARD_PROFILE_TYPE: headers.PROFILE_TYPE_INSIGHTS }, error_response.create_error_incomplete_profile_headers(), False, False, None ), # Success profile type headers ( { headers.ORCHARD_PROFILE_TYPE: headers.PROFILE_TYPE_INSIGHTS, headers.ORCHARD_PROFILE_ID: '1111' }, None, True, False, "call('test_feature', 'InsightsProfile', '1111', None)" ), # Success account type headers ( { headers.ORCHARD_USER_ID: 'oa:456' }, None, False, True, "call('test_feature', 'oa:456', None, None, None)" ), # None type headers ( {}, None, False, True, "call('test_feature', None, None, None)" ), ]) def test_get_single_feature( monkeypatch, request_headers, expected_response, profile_context_called, account_context_called, call_args_str ): """Test get single feature.""" monkeypatch.setattr( features_logic, 'get_single_feature_with_profile_context', MagicMock()) monkeypatch.setattr( features_logic, 'get_single_feature_with_account_context', MagicMock()) result = pythonfeatures.get_single_feature( 'test_feature', RequestContext( CaseInsensitiveDict(request_headers))) if expected_response is not None: assert result.status == expected_response.status assert result.errors == expected_response.errors if profile_context_called: assert str( features_logic.get_single_feature_with_profile_context.call_args ) == call_args_str assert features_logic.get_single_feature_with_profile_context.called if account_context_called: assert str( features_logic.get_single_feature_with_account_context.call_args ) == call_args_str assert features_logic.get_single_feature_with_account_context.called def test_get_single_feature_typeerror(): """Test Exception thrown if request_context is not ReqestContext.""" try: pythonfeatures.get_single_feature('pizza', 'party') except TypeError as er: assert str(er) == 'request_context must be class RequestContext' def test_get_single_feature_by_attributes(monkeypatch): """Test get single feature by attributes.""" monkeypatch.setattr( features_logic, 'get_single_feature_by_attributes', MagicMock()) pythonfeatures.get_single_feature_by_attributes( 'test_feature', {'user_id': 'alw:789'}) assert str( features_logic.get_single_feature_by_attributes.call_args ) == "call('test_feature', {'user_id': 'alw:789'})" assert features_logic.get_single_feature_by_attributes.called