"""Test context util functions.""" import pytest from feed_ingestion.util import context_util from tests.testing_utils import raises_optionally def test_get_context_values(): """Test get_context_values if values_from_context ii not specified.""" possible_values = ['1', '2', '3'] result = context_util.get_context_values( values_from_context=None, possible_values=possible_values) assert result == possible_values def test_get_context_values_with_correct_values(): """Test get_context_values if values_from_context is correct.""" possible_values = ['1', '2', '3'] result = context_util.get_context_values( values_from_context='1,2', possible_values=possible_values) assert result == ['1', '2'] result = context_util.get_context_values( values_from_context='1', possible_values=possible_values) assert result == ['1'] def test_get_context_values_with_not_correct_values(): """Test get_context_values if values_from_context is correct.""" possible_values = ['1', '2', '3'] with pytest.raises(ValueError): context_util.get_context_values( values_from_context='1,4', possible_values=possible_values) @pytest.mark.parametrize( 'value, expected, raises', [ pytest.param('True', context_util.Reload.TRUE, None), pytest.param('true', context_util.Reload.TRUE, None), pytest.param('Full', context_util.Reload.TRUE, None), pytest.param('FULL', context_util.Reload.TRUE, None), pytest.param('full', context_util.Reload.TRUE, None), pytest.param('soft', context_util.Reload.SOFT, None), pytest.param('Soft', context_util.Reload.SOFT, None), pytest.param('SOFT', context_util.Reload.SOFT, None), pytest.param('', context_util.Reload.FALSE, None), pytest.param('false', context_util.Reload.FALSE, None), pytest.param('False', context_util.Reload.FALSE, None), pytest.param('none', context_util.Reload.FALSE, None), pytest.param('1', None, ValueError), pytest.param('sof1', None, ValueError), pytest.param('N', None, ValueError), ], ) def test_reload_from_value(value, expected, raises): """Test Reload.from_value.""" with raises_optionally(raises): result = context_util.Reload.from_value(value) assert result == expected @pytest.mark.parametrize( 'value, expected, raises', [ pytest.param('y', True, None), pytest.param('Y', True, None), pytest.param('yes', True, None), pytest.param('YES', True, None), pytest.param('Yes', True, None), pytest.param('true', True, None), pytest.param('True', True, None), pytest.param('TRUE', True, None), pytest.param('on', True, None), pytest.param('On', True, None), pytest.param('1', True, None), pytest.param('n', False, None), pytest.param('N', False, None), pytest.param('no', False, None), pytest.param('No', False, None), pytest.param('NO', False, None), pytest.param('false', False, None), pytest.param('False', False, None), pytest.param('FALSE', False, None), pytest.param('off', False, None), pytest.param('Off', False, None), pytest.param('0', False, None), pytest.param('', False, None), pytest.param(None, None, ValueError), pytest.param(0, None, ValueError), pytest.param(True, None, ValueError), pytest.param(False, None, ValueError), pytest.param('t', None, ValueError), pytest.param('T', None, ValueError), pytest.param('f', None, ValueError), pytest.param('F', None, ValueError), pytest.param('2', None, ValueError), pytest.param('truthy', None, ValueError), pytest.param('maybe', None, ValueError), ], ) def test_strtobool(value, expected, raises): """Test strtobool.""" with raises_optionally(raises): result = context_util.strtobool(value) assert result is expected