def test_does_not_fail_when_value_is_one_of_multiple_types(self) -> None: validator = KeysTypeValidator(types=(str, int)) validator({"5": "5"}) validator({5: "5"})
def test_fails_when_value_is_not_of_multiple_type(self) -> None: value = {("5", 5): "5"} with pytest.raises(ValidationError, match="must be of type str, int"): KeysTypeValidator(types=(str, int))(value)
def test_does_not_fail_when_value_is_of_single_type(self) -> None: value = {"5": "5"} assert KeysTypeValidator(types=str)(value) is None
def test_fails_when_value_is_not_of_single_type(self) -> None: value = {5: "5"} with pytest.raises(ValidationError, match="must be of type str"): KeysTypeValidator(types=str)(value)
def test_value_must_be_a_map(self) -> None: value = "not a map" with pytest.raises(ValidationError, match="must be of type Mapping"): # noinspection PyTypeChecker KeysTypeValidator(types=str)(value) # type: ignore
def test_can_receive_multiple_types(self) -> None: validator = KeysTypeValidator(types={int, str}) assert validator.types == (int, str)
def test_can_receive_a_single_type(self) -> None: validator = KeysTypeValidator(types=str) assert validator.types == (str, )
def test_raises_error_if_types_is_not_an_iterable_of_types(self) -> None: with pytest.raises(ValueError, match="Must provide an iterable of types"): KeysTypeValidator(types=(str, "this is not a type", int)) # type: ignore