Ejemplo n.º 1
0
def test_allowed_values_constraint_js(web_fixture,
                                      constraint_rendering_fixture):
    fixture = constraint_rendering_fixture

    allowed_values = ['a', 'b']
    constraint = AllowedValuesConstraint(allowed_values=allowed_values)

    class MyForm(Form):
        def __init__(self, view, name):
            super().__init__(view, name)
            field = fixture.model_object.fields.an_attribute.with_validation_constraint(
                constraint)
            self.add_child(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('//input[@type="text"]',
                                    'ba',
                                    trigger_blur=False,
                                    wait_for_ajax=False)
    web_fixture.driver_browser.press_tab()
    web_fixture.driver_browser.wait_for_element_visible(fixture.error_xpath)
Ejemplo n.º 2
0
def test_validation(fixture):
    """Field input is validated by means of Constraints added to the field. Such validation
       happens in two places: validation of the string input, and validation based on the
       parsed python object."""

    # For testing, we just use a simple one-way marshalling field from string to int
    class MyField(Field):
        def parse_input(self, unparsed_input):
            return int(unparsed_input)

    field = fixture.new_field(cls=MyField)

    # How a validation_constraint is coded that checks the raw string input
    class StringLevelConstraint(ValidationConstraint):
        name = 'stringlevel'

        def validate_input(self, unparsed_input):
            if unparsed_input not in ('1', '2'):
                raise self

    # How a validation_constraint is coded that checks the final python object
    class PythonLevelConstraint(ValidationConstraint):
        name = 'pythonlevel'

        def validate_parsed_value(self, parsed_value):
            if parsed_value > 1:
                raise self

    # How constraints are added to the field
    field.add_validation_constraint(StringLevelConstraint(''))
    field.add_validation_constraint(PythonLevelConstraint(''))

    # Input passing both constraints work as expected
    with expected(NoException):
        field.from_input('1')
        assert fixture.model_object.an_attribute == 1

        # Input failing either validation_constraint result in the appropriate validation_constraint being raised
    with expected(StringLevelConstraint):
        field.from_input('unparsable string')
    with expected(PythonLevelConstraint):
        field.from_input('2')

    # If multiple constraints fail, the first one (in order added to the field) is the one reported
    field.add_validation_constraint(AllowedValuesConstraint([]))
    with expected(StringLevelConstraint):
        field.from_input('unparsable string')
Ejemplo n.º 3
0
    def allowed_values_constraint_js(self, fixture):
        allowed_values = ['a', 'b']
        constraint = AllowedValuesConstraint(allowed_values=allowed_values)

        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"]', 'ba')
        fixture.driver_browser.press_tab('//input')
        fixture.driver_browser.wait_for_element_visible(fixture.error_xpath)
Ejemplo n.º 4
0
    def allowed_values_constraint(self, fixture):
        allowed_values=['a','b']
        allowed_values_constraint = AllowedValuesConstraint(allowed_values=allowed_values)

        #case: valid input
        valid_input = allowed_values[1]
        with expected(NoException):
            allowed_values_constraint.validate_input(valid_input)

        #case: invalid input
        invalid_input = 'ba'
        assert invalid_input not in allowed_values
        with expected(AllowedValuesConstraint):
            allowed_values_constraint.validate_input(invalid_input)