def max_length_constraint(self, fixture): max_allowed_length = 5 max_length_constraint = MaxLengthConstraint(max_length=max_allowed_length) #max length is returned as parameter vassert( max_length_constraint.parameters == '%s' % max_allowed_length ) #case: just enough characters, exact number of with expected(NoException): max_length_constraint.validate_input('⁵'*max_allowed_length) #case: less than max with expected(NoException): max_length_constraint.validate_input('⁵'*(max_allowed_length-1)) #case: nothing with expected(NoException): max_length_constraint.validate_input('') #case: just too much with expected(MaxLengthConstraint): max_length_constraint.validate_input('ś'*(max_allowed_length+1))
def test_max_length_constraint_js(web_fixture, constraint_rendering_fixture): fixture = constraint_rendering_fixture max_allowed_length = 5 constraint = MaxLengthConstraint(max_length=max_allowed_length) 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"]', '123456') accepted_value = web_fixture.driver_browser.get_value('//input[@type="text"]') assert accepted_value == '12345'