|
1 | 1 | """Test validation Failure.""" |
2 | 2 |
|
| 3 | +# standard |
| 4 | +from typing import Any, Callable |
| 5 | + |
| 6 | +# third-party |
| 7 | +import pytest |
| 8 | + |
3 | 9 | # local |
4 | | -from validators import between |
| 10 | +from validators import ValidationError, between, slug, uuid |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")]) |
| 14 | +def test_false_raise_validation_error_flag( |
| 15 | + function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch |
| 16 | +) -> None: |
| 17 | + """A false flag returns validation errors instead of raising them.""" |
| 18 | + monkeypatch.delenv("RAISE_VALIDATION_ERROR", raising=False) |
| 19 | + result = function(value, r_ve=False) |
| 20 | + assert isinstance(result, ValidationError) |
| 21 | + assert not result |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")]) |
| 25 | +def test_environment_still_enables_raise_validation_error( |
| 26 | + function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch |
| 27 | +) -> None: |
| 28 | + """The environment option enables raising even when the call flag is false.""" |
| 29 | + monkeypatch.setenv("RAISE_VALIDATION_ERROR", "True") |
| 30 | + with pytest.raises(ValidationError): |
| 31 | + function(value, r_ve=False) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")]) |
| 35 | +def test_true_raise_validation_error_flag( |
| 36 | + function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch |
| 37 | +) -> None: |
| 38 | + """A true call flag continues to raise validation errors.""" |
| 39 | + monkeypatch.delenv("RAISE_VALIDATION_ERROR", raising=False) |
| 40 | + with pytest.raises(ValidationError): |
| 41 | + function(value, r_ve=True) |
| 42 | + |
5 | 43 |
|
6 | 44 | failed_obj_repr = "ValidationError(func=between" |
7 | 45 |
|
|
0 commit comments