Example #1
0
    def rendering_of_constraints(self, fixture):
        """The constraints of the Field of an PrimitiveInput are rendered in html as html attributes of an Input
           which corresponds with the name of each validation_constraint and has value the parameters of the validation_constraint.
           The error message of each validation_constraint is also put in a json object inside the class attribute.
           These measures make it possible to write constraints that are checked on the browser either by
           browser support or by a jquery validate add-on, and in case of failure to display the exact
           corresponding error message."""

        fixture.field.bind('an_attribute', fixture.model_object)
        fixture.model_object.an_attribute = 'field value'

        constraint1 = ValidationConstraint('validation_constraint 1 message')
        constraint1.name = 'one'

        @stubclass(ValidationConstraint)
        class ConstraintWithParams(ValidationConstraint):
            @property
            def parameters(self):
                return 'a parameter'

        constraint2 = ConstraintWithParams(
            'validation_constraint 2 message with apostrophe\'s')
        constraint2.name = 'two'

        fixture.field.add_validation_constraint(constraint1)
        fixture.field.add_validation_constraint(constraint2)

        tester = WidgetTester(fixture.input)

        actual = tester.render_html()
        escaped_json = html_escape(
            '{"validate": {"messages": {"data-one": "validation_constraint 1 message", "data-two": "validation_constraint 2 message with apostrophe\\\\\'s"}}}'
        )
        expected_html = '''<input name="an_attribute" data-one="true" data-two="a parameter" form="test" type="inputtype" value="field value" class="%s">''' % escaped_json
        vassert(actual == expected_html)
Example #2
0
def test_rendering_of_constraints(web_fixture, constraint_rendering_fixture):
    """The constraints of the Field of an PrimitiveInput are rendered in html as html attributes of an Input
       which corresponds with the name of each validation_constraint and has value the parameters of the validation_constraint.
       The error message of each validation_constraint is also put in a json object inside the class attribute.
       These measures make it possible to write constraints that are checked on the browser either by
       browser support or by a jquery validate add-on, and in case of failure to display the exact
       corresponding error message."""

    fixture = constraint_rendering_fixture

    fixture.field.bind('an_attribute', fixture.model_object)
    fixture.model_object.an_attribute = 'field value'

    constraint1 = ValidationConstraint('validation_constraint 1 message')
    constraint1.name = 'one'
    @stubclass(ValidationConstraint)
    class ConstraintWithParams(ValidationConstraint):
        @property
        def parameters(self):
            return 'a parameter'

    constraint2 = ConstraintWithParams('validation_constraint 2 message with apostrophe\'s and quotes"')
    constraint2.name = 'two'

    fixture.field.add_validation_constraint(constraint1)
    fixture.field.add_validation_constraint(constraint2)

    tester = WidgetTester(fixture.input)

    actual = tester.render_html()
    expected_html = '''<input name="test-an_attribute" id="id-test-an_attribute" data-msg-one="validation_constraint 1 message" data-msg-two="validation_constraint 2 message with apostrophe&#x27;s and quotes&quot;" data-rule-one="true" data-rule-two="a parameter" form="test" type="inputtype" value="field value" class="reahl-primitiveinput">'''
    assert actual == expected_html
Example #3
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'
Example #4
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 )