Exemple #1
0
    def required_constraint_js(self, fixture):
        constraint = RequiredConstraint()

        class MyForm(Form):
            def __init__(self, view, name):
                super(MyForm, self).__init__(view, name)
                field = fixture.model_object.fields.an_attribute
                field.add_validation_constraint(constraint)
                self.add_child(TextInput(self, field))

        wsgi_app = fixture.new_wsgi_app(child_factory=MyForm.factory('myform'),
                                        enable_js=True)
        fixture.reahl_server.set_app(wsgi_app)

        fixture.driver_browser.open('/')

        fixture.driver_browser.type('//input[@type="text"]', 'something')
        fixture.driver_browser.press_tab('//input')
        fixture.driver_browser.wait_for_element_not_visible(
            fixture.error_xpath)

        fixture.driver_browser.type('//input[@type="text"]', '')
        fixture.driver_browser.press_backspace(
            '//input')  # To trigger validation on the field

        fixture.driver_browser.wait_for_element_visible(fixture.error_xpath)
Exemple #2
0
def test_required_constraint_js(web_fixture, constraint_rendering_fixture):
    fixture = constraint_rendering_fixture

    constraint = RequiredConstraint()

    class MyForm(Form):
        def __init__(self, view, name):
            super().__init__(view, name)
            self.use_layout(FormLayout())
            field = fixture.model_object.fields.an_attribute.with_validation_constraint(constraint)
            self.layout.add_input(TextInput(self, field))
    wsgi_app = web_fixture.new_wsgi_app(child_factory=MyForm.factory('myform'), enable_js=True)
    web_fixture.reahl_server.set_app(wsgi_app)

    web_fixture.driver_browser.open('/')

    web_fixture.driver_browser.type(XPath.input_labelled('an attribute'), 'something', trigger_blur=False, wait_for_ajax=False)
    web_fixture.driver_browser.press_tab()
    web_fixture.driver_browser.wait_for_element_not_visible(fixture.error_xpath)

    web_fixture.driver_browser.type(XPath.input_labelled('an attribute'), '')
    
    web_fixture.driver_browser.press_keys(web_fixture.driver_browser.Keys.BACK_SPACE, locator=XPath.input_labelled('an attribute'))  # To trigger validation on the field

    web_fixture.driver_browser.wait_for_element_visible(fixture.error_xpath)
Exemple #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'
Exemple #4
0
    def multi_choices_required(self):
        self.all_choices = [Choice(1, IntegerField(label='One'))]
        self.groups = []
        self.choices = self.all_choices
        field = self.new_field(MultiChoiceField)
        self.field = field.with_validation_constraint(RequiredConstraint())

        self.valid_inputs = [('1', ), ['1']]
        self.invalid_input = []
        self.input_to_value_map = {('1', ): [1]}
        self.expected_validation_constraint = RequiredConstraint
Exemple #5
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 )
Exemple #6
0
def test_required_constraint(fixture):
    selector = 'find me'
    required_constraint = RequiredConstraint(dependency_expression=selector)

    #selector is returned as parameter
    assert required_constraint.parameters == selector

    #case: no input
    with expected(RequiredConstraint):
        required_constraint.validate_input('')
    with expected(RequiredConstraint):
        required_constraint.validate_input(None)
    #just spaces
    space = ' '
    with expected(RequiredConstraint):
        required_constraint.validate_input(space)
    with expected(RequiredConstraint):
        required_constraint.validate_input(space * 56)

    #case: input
    with expected(NoException):
        required_constraint.validate_input(' something valid    ')
    with expected(NoException):
        required_constraint.validate_input('sômething else that_is_valid')
    with expected(NoException):
        required_constraint.validate_input('.')