Exemplo n.º 1
0
def test_base_field_write_or_read_only_not_both():

    assert BaseField("Read only", read_only=True).write_only is False
    assert BaseField("Write only", write_only=True).read_only is False

    with pytest.raises(ValueError):
        BaseField("Both", read_only=True, write_only=True)
Exemplo n.º 2
0
def test_base_field_implementation_hooks():
    field = BaseField(None)

    with pytest.raises(NotImplementedError):
        field.to_representation(None)

    with pytest.raises(NotImplementedError):
        field.from_representation(None)
Exemplo n.º 3
0
def test_base_field_validate():
    def always_pass_validator(value):
        pass

    def always_raise_validator(value):
        raise ValidationError("Just because!")

    # here there is no chance to validate
    field_with_picky_validation = BaseField(
        "test base field validators",
        validators=[always_pass_validator, always_raise_validator]
    )
    with pytest.raises(ValidationError):
        field_with_picky_validation.validate("foo")

    # here will validate because of very indulgent validation
    field_with_lenient_validation = BaseField(
        "test validate",
        validators=[always_pass_validator]
    )
    field_with_lenient_validation.validate("foo")

    # here will also validate because there are no vaidators
    field_with_lenient_validation = BaseField("test validate")
    field_with_lenient_validation.validate("foo")
Exemplo n.º 4
0
def test_base_field_implementation_hooks():
    field = BaseField(None)

    with pytest.raises(NotImplementedError):
        field.to_representation(None)

    with pytest.raises(NotImplementedError):
        field.from_representation(None)
Exemplo n.º 5
0
def test_base_field_validate():
    def always_pass_validator(value):
        pass

    def always_raise_validator(value):
        raise ValidationError("Just because!")

    # here there is no chance to validate
    field_with_picky_validation = BaseField(
        "test base field validators",
        validators=[always_pass_validator, always_raise_validator])
    with pytest.raises(ValidationError):
        field_with_picky_validation.validate("foo")

    # here will validate because of very indulgent validation
    field_with_lenient_validation = BaseField(
        "test validate", validators=[always_pass_validator])
    field_with_lenient_validation.validate("foo")

    # here will also validate because there are no vaidators
    field_with_lenient_validation = BaseField("test validate")
    field_with_lenient_validation.validate("foo")