From 4ad676c34114d9c6edd0c0b83f65c099ef92a6d2 Mon Sep 17 00:00:00 2001 From: Lexi Stelter Date: Mon, 28 Nov 2022 19:00:45 +0100 Subject: [PATCH] Add unit tests for AnyOfValidator with empty allowed values/types --- tests/validators/any_of_validator_test.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/validators/any_of_validator_test.py b/tests/validators/any_of_validator_test.py index 274ef53..0c79b3c 100644 --- a/tests/validators/any_of_validator_test.py +++ b/tests/validators/any_of_validator_test.py @@ -181,6 +181,19 @@ def test_with_specified_allowed_type(allowed_types, expected_type_dict, valid_in **expected_type_dict, } + @staticmethod + def test_empty_allowed_values_with_allowed_types(): + """ Test AnyOfValidator with an empty list of allowed values (requires explicit allowed_types). """ + validator = AnyOfValidator([], allowed_types=[str]) + + # There can't be any valid input for this validator (in that way it acts like a RejectValidator with a different error) + with pytest.raises(ValueNotAllowedError) as exception_info: + validator.validate('banana') + assert exception_info.value.to_dict() == { + 'code': 'value_not_allowed', + 'allowed_values': [], + } + # Test AnyOfValidator with case-insensitive option @staticmethod @@ -248,9 +261,16 @@ def test_value_not_allowed_error_with_too_many_allowed_values(): # Invalid validator parameters + @staticmethod + def test_empty_allowed_values_requires_allowed_types(): + """ Test that AnyOfValidator raises exception when allowed_values is empty and no allowed_types are specified. """ + with pytest.raises(InvalidValidatorOptionException) as exception_info: + AnyOfValidator([]) + assert str(exception_info.value) == 'Parameter "allowed_types" is an empty list (or types could not be autodetermined).' + @staticmethod def test_empty_allowed_types(): - """ Check that AnyOfValidator raises exception when allowed_types is empty. """ + """ Test that AnyOfValidator raises exception when allowed_types is empty. """ with pytest.raises(InvalidValidatorOptionException) as exception_info: AnyOfValidator([1, 2, 3], allowed_types=[]) assert str(exception_info.value) == 'Parameter "allowed_types" is an empty list (or types could not be autodetermined).'