Exemplo n.º 1
0
    def test_choice(self):
        """Test choices validations for the string field"""
        class StatusChoices(enum.Enum):
            """Set of choices for the status"""

            PENDING = "Pending"
            SUCCESS = "Success"
            ERROR = "Error"

        status = String(max_length=10, choices=StatusChoices)
        assert status is not None

        # Test loading of values to the status field
        assert status._load("Pending") == "Pending"
        with pytest.raises(ValidationError) as e_info:
            status._load("Failure")
        assert e_info.value.messages == {
            "unlinked": [
                "Value `'Failure'` is not a valid choice. "
                "Must be among ['Pending', 'Success', 'Error']"
            ]
        }
Exemplo n.º 2
0
def test_that_sanitization_can_be_optionally_switched_off():
    str_field = String(sanitize=False)

    value = str_field._load("an <script>evil()</script> example")
    assert value == "an <script>evil()</script> example"
Exemplo n.º 3
0
def test_that_string_values_are_automatically_cleaned():
    str_field = String()

    value = str_field._load("an <script>evil()</script> example")
    assert value == u"an &lt;script&gt;evil()&lt;/script&gt; example"
Exemplo n.º 4
0
    def test_max_length(self):
        """Test maximum length validation for the string field"""

        with pytest.raises(ValidationError):
            name = String(max_length=5)
            name._load("Dummy Dummy")
Exemplo n.º 5
0
    def test_min_length(self):
        """Test minimum length validation for the string field"""

        with pytest.raises(ValidationError):
            name = String(min_length=5, max_length=10)
            name._load("Dum")
Exemplo n.º 6
0
 def test_type_validation(self):
     """Test type checking validation for the Field"""
     name = String(max_length=10)
     assert name._load(1) == "1"