"""Test feature flagging helpers.""" from unittest.mock import MagicMock from ddex_ingester_common.constants.feature_flag import ( ENABLED ) from ddex_ingester_common.graphql.feature_flag_queries import ( FEATURE_FLAG_LABEL_QUERY, FEATURE_FLAG_QUERY ) from ddex_ingester_common.helpers.feature_flag import ( is_flag_enabled, is_flag_enabled_for_label ) import pytest @pytest.mark.parametrize('flag_value, expected', [ ('control', False), (ENABLED, True), ]) def test_is_flag_enabled(flag_value, expected): """Test is_flag_enabled.""" execute = MagicMock(return_value={ 'data': {'account': {'features': [ { 'value': 'control', 'feature': 'another_flag' }, { 'value': f'{flag_value}', 'feature': 'cool_flag' }, ]}} }) connector = MagicMock() connector.execute = execute flag_name = 'cool_flag' output = is_flag_enabled(connector, flag_name) assert output == expected execute.assert_called_once_with( FEATURE_FLAG_QUERY, {'prefix': flag_name} ) def test_is_flag_enabled_non_existant_flag(): """Test is_flag_enabled with a flag that does not exist.""" execute = MagicMock(return_value={ 'data': {'account': {'features': [ { 'value': ENABLED, 'feature': 'another_flag' }, { 'value': 'control', 'feature': 'something_else' }, ]}} }) connector = MagicMock() connector.execute = execute flag_name = 'cool_flag' output = is_flag_enabled(connector, flag_name) assert not output execute.assert_called_once_with( FEATURE_FLAG_QUERY, {'prefix': flag_name} ) @pytest.mark.parametrize('flag_value, expected', [ ('control', False), (ENABLED, True), ]) def test_is_flag_enabled_for_label(flag_value, expected): """Test is_flag_enabled_for_label.""" vendor_id = 123 subaccount_id = 1234 execute = MagicMock(return_value={ 'data': {'orchardLabel': {'features': [ { 'value': 'control', 'feature': 'another_flag' }, { 'value': f'{flag_value}', 'feature': 'cool_flag' }, ]}} }) connector = MagicMock() connector.execute = execute flag_name = 'cool_flag' output = is_flag_enabled_for_label(connector, flag_name, vendor_id, subaccount_id) assert output == expected execute.assert_called_once_with( FEATURE_FLAG_LABEL_QUERY, { 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'prefix': flag_name } ) def test_is_flag_enabled_for_label_non_existant_flag(): """Test is_flag_enabled_for_label with a flag that does not exist.""" vendor_id = 123 subaccount_id = 1234 execute = MagicMock(return_value={ 'data': {'orchardLabel': {'features': [ { 'value': ENABLED, 'feature': 'another_flag' }, { 'value': 'control', 'feature': 'something_else' }, ]}} }) connector = MagicMock() connector.execute = execute flag_name = 'cool_flag' output = is_flag_enabled_for_label(connector, flag_name, vendor_id, subaccount_id) assert not output execute.assert_called_once_with( FEATURE_FLAG_LABEL_QUERY, { 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'prefix': flag_name } ) def test_is_flag_enabled_for_label_handles_nonexistent_label(): """Test is_flag_enabled_for_label handling a nonexistent label.""" vendor_id = 123 subaccount_id = 1234 execute = MagicMock(return_value={ 'data': {'orchardLabel': None} }) connector = MagicMock() connector.execute = execute flag_name = 'cool_flag' output = is_flag_enabled_for_label(connector, flag_name, vendor_id, subaccount_id) assert not output execute.assert_called_once_with( FEATURE_FLAG_LABEL_QUERY, { 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'prefix': flag_name } )