def test_assigns_min_and_max(self): form_class = self.init_form(validators=[NumberRange(min=2, max=10)]) form = form_class(MultiDict(test_field=3)) assert str(form.test_field) == ( '<input id="test_field" max="10" min="2" ' 'name="test_field" step="1" type="number" value="3">' )
def test_existing_name_collision_classical_mapping(self): sa.Table( 'user', sa.MetaData(None), sa.Column('name', sa.String(255)), sa.Column('email', sa.String(255)) ) class MyForm(ModelForm): name = TextField( validators=[Unique( [User.name, User.email], get_session=lambda: self.session )] ) email = TextField() self.session.add(User( name=u'someone', email=u'*****@*****.**' )) self.session.commit() form = MyForm(MultiDict({ 'name': u'someone', 'email': u'*****@*****.**' })) form.validate() assert form.errors == {'name': [u'Already exists.']}
def test_phone_number_in_form(self): form = self.UserForm( MultiDict(name=u'Matti Meikalainen', phone_number='+358401231233')) form.validate() assert len(form.errors) == 0 assert form.data['phone_number'] == ( phone_number.PhoneNumber('+358401231233'))
def test_relationship_multiple_no_collision(self): class MyForm(ModelForm): name = TextField(validators=[ Unique([User.name, User.favorite_color], get_session=lambda: self.session) ]) email = TextField() favorite_color = QuerySelectField( query_factory=lambda: self.session.query(Color).all(), allow_blank=True) red_color = Color(name='red') blue_color = Color(name='blue') self.session.add(red_color) self.session.add(blue_color) self.session.add( User(name=u'someone', email=u'*****@*****.**', favorite_color=red_color)) self.session.commit() form = MyForm( MultiDict({ 'name': u'someone', 'email': u'*****@*****.**', 'favorite_color': str(blue_color.id) })) form.validate() assert form.errors == {}
def test_assigns_min_and_max(self): form_class = self.init_form( validators=[TimeRange(min=time(12, 12), max=time(13, 30))]) form = form_class(MultiDict(test_field='13:20')) assert str(form.test_field) == ( '<input id="test_field" max="13:30:00" min="12:12:00"' ' name="test_field" type="time" value="13:20">')
def test_validates_whole_chain(self): class MyForm(Form): email = TextField(validators=[Chain([DataRequired(), Email()])]) form = MyForm(MultiDict({'name': ''})) form.validate() assert 'email' in form.errors
def test_invalid_weekdays(self): form_class = self.init_form() form = form_class(MultiDict([ ('test_field', '8'), ])) form.validate() assert len(form.errors['test_field']) == 1
def test_renders_input_time_at_midnight(self): form_class = self.init_form() form = form_class(MultiDict(test_field='00:00')) assert str(form.test_field) == ( '<input id="test_field" name="test_field"' ' type="time" value="00:00">' )
def test_prevents_value_changing(self): class MyForm(Form): name = TextField(default='') form = MyForm() read_only(form.name) form.process(MultiDict({'name': 'New value'})) assert form.name.data == ''
def test_has_readonly_and_disabled_attributes_in_html(self): form_class = self.init_form() form = form_class(MultiDict(test_field='y')) read_only(form.test_field) assert ( '<input checked disabled id="test_field" ' 'name="test_field" readonly type="checkbox" value="y">' ) in str(form.test_field)
def test_assigns_min_and_max(self): form_class = self.init_form(validators=[ DateRange(min=datetime(2000, 1, 1), max=datetime(2000, 10, 10)) ]) form = form_class(MultiDict(test_field='2000-2-2')) assert str(form.test_field) == ( '<input id="test_field" max="2000-10-10" min="2000-01-01"' ' name="test_field" type="date" value="2000-2-2">')
def test_processes_values(self): form_class = self.init_form() form = form_class(MultiDict({ 'test_field-date': '2000-3-2', 'test_field-time': '19:10', })) assert form.test_field.data['date'] == date(2000, 3, 2) assert form.test_field.data['time'] == time(19, 10)
def test_understands_nested_choices(self): form_class = self.init_form(choices=self.choices) form = form_class( MultiDict([('fruit', 'invalid')]) ) form.validate() assert len(form.errors['fruit']) == 1
def test_understands_callables_as_choices(self): form_class = self.init_form(choices=lambda: []) form = form_class( MultiDict([('fruit', 'invalid')]) ) form.validate() assert len(form.errors['fruit']) == 1
def test_supports_model_query_parameter(self): User.query = self.session.query(User) class MyForm(ModelForm): name = TextField(validators=[Unique(User.name, )]) form = MyForm(MultiDict({'name': u'someone'})) assert form.validate()
def test_custom_none_value(self): self.create_cities() MyForm = self.create_form(allow_blank=True, blank_text='Choose city...', blank_value='') form = MyForm(MultiDict({'city': ''})) assert form.validate(), form.errors assert '<option selected value="">Choose city...</option>' in (str( form.city))
def test_understands_nested_choices(self): form_class = self.init_form(choices=self.choices) form = form_class( MultiDict([('fruits', 'apple'), ('fruits', 'invalid')])) form.validate() assert form.errors == { 'fruits': [u"'invalid' is not a valid choice for this field"] }
def test_preserves_previous_value(self): class MyForm(Form): name = TextField() form = MyForm() form.name.data = 'Previous value' read_only(form.name) form.process(MultiDict({'name': 'New value'})) assert form.name.data == 'Previous value'
def test_populates_object(self): form_class = self.init_form() form = form_class(MultiDict({ 'test_field-date': '2000-3-2', 'test_field-time': '19:10', })) obj = Obj() form.populate_obj(obj) assert obj.test_field == datetime(2000, 3, 2, 19, 10)
def save(self, event=None, data=None): if not data: data = { 'name': u'Some event', 'locations-0-name': u'Some location', 'locations-0-description': u'Some description' } if not event: event = self.Event() self.session.add(event) form = self.EventForm(MultiDict(data)) else: form = self.EventForm(MultiDict(data), obj=event) form.validate() form.populate_obj(event) self.session.commit() return event
def test_assigns_step(self): form_class = self.init_form( widget=NumberInput(step='0.1') ) form = form_class(MultiDict(test_field=3)) assert str(form.test_field) == ( '<input id="test_field" ' 'name="test_field" step="0.1" type="number" value="3">' )
def test_data_coercion(self): choices = ( ('Fruits', ((0, 'Apple'), (1, 'Peach'), (2, 'Pear'))), (3, 'Cucumber'), ) form_class = self.init_form(choices=choices, coerce=int) form = form_class(MultiDict([('fruit', '1')])) form.validate() assert not form.errors
def test_strip_string_fields_set_for_string_field(self): self.init() class ModelTestForm(ModelForm): class Meta: model = self.ModelTest strip_string_fields = True f = ModelTestForm(MultiDict([('test_column', 'strip this ')])) assert f.test_column.data == 'strip this'
def test_understands_none_values(self): class MyForm(Form): choice_field = SelectField( choices=[('', '-- Choose --'), ('choice 1', 'Something')], coerce=null_or_unicode ) form = MyForm(MultiDict({'choice_field': u''})) form.validate() assert form.errors == {}
def test_does_not_trim_fields_when_trim_param_is_false(self): self.init(info={'trim': False}) class ModelTestForm(ModelForm): class Meta: model = self.ModelTest strip_string_fields = True f = ModelTestForm(MultiDict([('test_column', 'strip this ')])) assert f.test_column.data == 'strip this '
def test_invalid_unique_phone_no_500_error(self): class MyForm(ModelForm): phone = PhoneNumberField(validators=[Unique(User.phone)], region='ES') form = MyForm(MultiDict({'phone': u'abc$%·'})) form.validate() assert len(form.errors['phone']) == 2 assert 'Not a valid phone number value' in form.errors['phone'] assert 'Unable to check uniqueness.' in form.errors['phone']
def test_assigment_and_deletion(self): self.save() event = self.session.query(self.Event).first() assert event.locations[0].name == u'Some location' data = {'name': u'Some event'} form = self.EventForm(MultiDict(data)) form.validate() form.populate_obj(event) self.session.commit() event = self.session.query(self.Event).first() assert event.locations == []
def test_without_obj_without_collision(self): class MyForm(ModelForm): name = TextField(validators=[ Unique(User.name, get_session=lambda: self.session) ]) self.session.add(User(name=u'someone else')) self.session.commit() form = MyForm(MultiDict({'name': u'someone'})) assert form.validate()
def test_does_not_populate_obj_values(self): class MyForm(Form): id = PassiveHiddenField() class A(object): id = None form = MyForm(MultiDict({'id': 12})) a = A() form.populate_obj(a) assert a.id is None
def test_existing_name_no_collision(self): class MyForm(ModelForm): name = TextField(validators=[ Unique(User.name, get_session=lambda: self.session) ]) obj = User(name=u'someone') self.session.add(obj) form = MyForm(MultiDict({'name': u'someone'}), obj=obj) assert form.validate()