"""Test Splitio model.""" import json from contextlib import nullcontext as does_not_raise from unittest.mock import call import pytest from owsresponse import response from pythonfeatures import pythonfeatures from product_review.models import splitio as splitio_model @pytest.mark.parametrize( ( "test_description", "get_single_feature_by_attributes_result", "expected_raise", "expected_result", ), [ ( "success enabled", response.Response("enabled"), does_not_raise(), True, ), ( "success disabled", response.Response("control"), does_not_raise(), False, ), ( "error splitio", response.Response(status=500, message="Splitio error message"), pytest.raises( Exception, match=json.dumps( { "status": 500, "code": "splitio_error", "message": "Splitio error message", } ), ), None, ), ], ) def test__is_enabled( mocker, test_description, get_single_feature_by_attributes_result, expected_raise, expected_result, ): """Test _is_enabled.""" mocker.patch.object( pythonfeatures, "get_single_feature_by_attributes", return_value=get_single_feature_by_attributes_result, ) feature_name = "some_feature" feature_attributes = {"some_key": "some_value"} result = None with expected_raise: result = splitio_model._is_enabled(feature_name, feature_attributes) assert pythonfeatures.get_single_feature_by_attributes.mock_calls == [ call(feature_name, feature_attributes) ] assert result == expected_result @pytest.mark.parametrize("_is_enabled_result", [True, False]) def test_is_enabled_prevent_approval_with_blockers( app_context, mocker, _is_enabled_result ): """Test is_enabled_prevent_approval_with_blockers.""" app_context.g.request_context.identity_id = "some-identity-id" mocker.patch.object(splitio_model, "_is_enabled", return_value=_is_enabled_result) result = splitio_model.is_enabled_prevent_approval_with_blockers() assert splitio_model._is_enabled.mock_calls == [ call( "content_app_prevent_product_approval_with_blockers", {"identity_id": "some-identity-id"}, ) ] assert result == _is_enabled_result