Esempio n. 1
0
def test_getting_modified_copy(fixture):
    """It is possible to get a modified copy of an existing field if you want to link it with
       different constraints on a different input"""

    other_constraint = ValidationConstraint('Other error')
    other_constraint.name = 'other'
    field = Field()
    field.add_validation_constraint(other_constraint)
    model_object = EmptyStub()
    field.bind('field_name', model_object)

    # Getting a copy
    new_field = field.copy()
    assert new_field is not field
    assert new_field.name == field.name
    assert new_field.storage_object == field.storage_object
    assert new_field.default == field.default
    assert new_field.label == field.label
    copied_other_constraint = new_field.get_validation_constraint_named(other_constraint.name)
    assert copied_other_constraint.field is new_field
    new_validation_constraints = [i.__class__ for i in new_field.validation_constraints]
    old_validation_constraints = [i.__class__ for i in field.validation_constraints]
    assert new_validation_constraints == old_validation_constraints
    assert new_field.validation_constraints != field.validation_constraints
    assert new_field.validation_constraints is not field.validation_constraints

    assert new_field.access_rights is not field.access_rights
    assert new_field.access_rights.readable is field.access_rights.readable
    assert new_field.access_rights.writable is field.access_rights.writable

    # Getting a required copy
    assert not field.required
    required_field = field.as_required(required_message='new required message')
    assert required_field.required
    required = required_field.get_validation_constraint_named(RequiredConstraint.name)
    assert required.error_message.template == 'new required message'

    # Getting copy that's not required
    field.make_required('')
    assert field.required
    optional_field = field.as_optional()
    assert not optional_field.required

    # Getting copy with a ValidationConstraint of certain type removed
    assert field.required 
    more_lax_field = field.without_validation_constraint(RequiredConstraint)
    assert not more_lax_field.required 

    # Getting copy with a new ValidationConstraint added
    field.make_optional()
    assert not field.required 
    more_strict_field = field.with_validation_constraint(RequiredConstraint())
    assert more_strict_field.required 

    # Getting copy with a new label
    assert field.label != 'new label'
    differently_labelled_field = field.with_label('new label')
    assert differently_labelled_field.label == 'new label'
Esempio n. 2
0
    def getting_modified_copy(self, fixture):
        """It is possible to get a modified copy of an existing field if you want to link it with 
           different constraints on a different input"""
        other_constraint = ValidationConstraint('Other error')
        other_constraint.name = 'other'
        field = Field()
        field.add_validation_constraint(other_constraint)

        # Getting a copy
        new_field = field.copy()
        vassert( new_field is not field )
        vassert( new_field.name == field.name )
        vassert( new_field.storage_object == field.storage_object )
        vassert( new_field.default == field.default )
        vassert( new_field.label == field.label )
        copied_other_constraint = new_field.get_validation_constraint_named(other_constraint.name)
        vassert( copied_other_constraint.field is new_field )
        new_validation_constraints = [i.__class__ for i in new_field.validation_constraints]
        old_validation_constraints = [i.__class__ for i in field.validation_constraints]
        vassert( new_validation_constraints == old_validation_constraints )
        vassert( new_field.validation_constraints != field.validation_constraints )
        vassert( new_field.validation_constraints is not field.validation_constraints )

        vassert( new_field.access_rights is not field.access_rights )
        vassert( new_field.access_rights.readable is field.access_rights.readable )
        vassert( new_field.access_rights.writable is field.access_rights.writable )

        # Getting a required copy
        vassert( not field.required )
        required_field = field.as_required(required_message='new required message')
        vassert( required_field.required )
        required = required_field.get_validation_constraint_named(RequiredConstraint.name)
        vassert( required.error_message.template == 'new required message' )

        # Getting copy that's not required
        field.make_required('')
        vassert( field.required )
        optional_field = field.as_optional()
        vassert( not optional_field.required )

        # Getting copy with a ValidationConstraint of certain type removed
        vassert( field.required )
        more_lax_field = field.as_without_validation_constraint(RequiredConstraint)
        vassert( not more_lax_field.required )
        
        # Getting copy with a new ValidationConstraint added
        field.make_optional()
        vassert( not field.required )
        more_strict_field = field.as_with_validation_constraint(RequiredConstraint())
        vassert( more_strict_field.required )