Example #1
0
def test_with_data():
    form = F(DummyPostData(a=["btest"]))
    assert form.a.data == "btest"
    assert form.a() == (
        '<select id="a" name="a"><option value="a">hello</option>'
        '<option selected value="btest">bye</option></select>')
Example #2
0
 def test_failure(self):
     form = self.F(DummyPostData(a=["  foo bar  "], b=["hi"]))
     self.assertEqual(form.a.data, "foo bar")
     self.assertEqual(form.b.data, "hi")
     self.assertEqual(len(form.b.process_errors), 1)
     assert not form.validate()
Example #3
0
def test_choice_shortcut_post():
    F = make_form(a=SelectField(choices=["foo", "bar"]))
    form = F(DummyPostData(a=["foo"]))
    assert form.validate()
    assert form.a.data == "foo"
    assert len(form.a.errors) == 0
Example #4
0
 def test_microseconds(self):
     d = datetime(2011, 5, 7, 3, 23, 14, 424200)
     F = make_form(a=DateTimeField(format='%Y-%m-%d %H:%M:%S.%f'))
     form = F(DummyPostData(a=['2011-05-07 03:23:14.4242']))
     self.assertEqual(d, form.a.data)
Example #5
0
 def test_separator(self):
     form = self.F2(DummyPostData({'a-a': 'fake', 'a::a': 'real'}))
     self.assertEqual(form.a.a.name, 'a::a')
     self.assertEqual(form.a.a.data, 'real')
     self.assertTrue(form.validate())
Example #6
0
def test_separator(F2):
    form = F2(DummyPostData({"a-a": "fake", "a::a": "real"}))
    assert form.a.a.name == "a::a"
    assert form.a.a.data == "real"
    assert form.validate()
Example #7
0
 def test_formdata_and_kwargs(self):
     form = self.F(DummyPostData({'otherField': 1}), a='test')
     self.assertEqual(form.a.data, 'test')
Example #8
0
 def test_with_postdata_and_model(self):
     form = self.BoringForm(DummyPostData(bool1=["y"]), obj=self.obj)
     assert form.bool1.data is True
     assert form.bool2.data is False
Example #9
0
    def test_empty_formdata(self):
        """"If formdata is empty, field.process_formdata should still run to handle empty data."""

        self.assertEqual(
            self.F(DummyPostData({'other': 'other'})).test.data, '')
        self.assertEqual(self.F(DummyPostData()).test.data, '')
Example #10
0
 def test_errors(self):
     F = make_form(a=FieldList(self.t))
     form = F(DummyPostData({"a-0": ["a"], "a-1": ""}))
     assert not form.validate()
     assert form.a.errors == [[], ["This field is required."]]
Example #11
0
    def test_simple(self):
        b = self._build_value
        VALUES = (
            b("search", "search", 'type="search"'),
            b("telephone", "123456789", 'type="tel"'),
            b("url", "http://wtforms.simplecodes.com/", 'type="url"'),
            b("email", "*****@*****.**", 'type="email"'),
            b(
                "datetime",
                "2013-09-05 00:23:42",
                'type="datetime"',
                datetime(2013, 9, 5, 0, 23, 42),
            ),
            b("date", "2013-09-05", 'type="date"', date(2013, 9, 5)),
            b(
                "dt_local",
                "2013-09-05 00:23:42",
                'type="datetime-local"',
                datetime(2013, 9, 5, 0, 23, 42),
            ),
            b(
                "integer",
                "42",
                '<input id="integer" name="integer" type="number" value="42">',
                42,
            ),
            b(
                "decimal",
                "43.5",
                '<input id="decimal" name="decimal" '
                'step="any" type="number" value="43.5">',
                Decimal("43.5"),
            ),
            b(
                "int_range",
                "4",
                '<input id="int_range" name="int_range" type="range" value="4">',
                4,
            ),
            b(
                "decimal_range",
                "58",
                '<input id="decimal_range" name="decimal_range" '
                'step="any" type="range" value="58">',
                58,
            ),
        )
        formdata = DummyPostData()
        kw = {}
        for item in VALUES:
            formdata[item["key"]] = item["form_input"]
            kw[item["key"]] = item["data"]

        form = self.F(formdata)
        for item in VALUES:
            field = form[item["key"]]
            render_value = field()
            if render_value != item["expected_html"]:
                tmpl = (
                    "Field {key} render mismatch: {render_value!r} != {expected_html!r}"
                )
                raise AssertionError(
                    tmpl.format(render_value=render_value, **item))
            if field.data != item["data"]:
                tmpl = "Field {key} data mismatch: {field.data!r} != {data!r}"
                raise AssertionError(tmpl.format(field=field, **item))
Example #12
0
 def test_separator(self):
     form = self.F2(DummyPostData({"a-a": "fake", "a::a": "real"}))
     self.assertEqual(form.a.a.name, "a::a")
     self.assertEqual(form.a.a.data, "real")
     self.assertTrue(form.validate())
Example #13
0
 def test_formdata(self):
     form = self.F1(DummyPostData({"a-a": ["moo"]}))
     self.assertEqual(form.a.form.a.name, "a-a")
     self.assertEqual(form.a["a"].data, "moo")
     self.assertEqual(form.a["b"].data, None)
     self.assertTrue(form.validate())
Example #14
0
 def test_dont_validate_choices(self):
     F = make_form(a=SelectField(choices=[("a", "Foo")], validate_choice=False))
     form = F(DummyPostData(a=["b"]))
     assert form.validate()
     self.assertEqual(form.a.data, "b")
     self.assertEqual(len(form.a.errors), 0)
Example #15
0
def test_microseconds():
    d = datetime(2011, 5, 7, 3, 23, 14, 424200)
    F = make_form(a=DateTimeField(format="%Y-%m-%d %H:%M:%S.%f"))
    form = F(DummyPostData(a=["2011-05-07 03:23:14.4242"]))
    assert d == form.a.data
Example #16
0
def test_failure():
    form = F(DummyPostData(a=["  foo bar  "], b=["hi"]))
    assert form.a.data == "foo bar"
    assert form.b.data == "hi"
    assert len(form.b.process_errors) == 1
    assert not form.validate()
Example #17
0
def test_formdata(F1):
    form = F1(DummyPostData({"a-a": ["moo"]}))
    assert form.a.form.a.name == "a-a"
    assert form.a["a"].data == "moo"
    assert form.a["b"].data is None
    assert form.validate()
Example #18
0
 def test_with_data(self):
     form = self.F(DummyPostData(a=['btest']))
     self.assertEqual(form.a.data, 'btest')
     self.assertEqual(str(form.a), """<select id="a" name="a"><option value="a">hello</option><option selected value="btest">bye</option></select>""")
Example #19
0
 def test_failure(self):
     form = self.F(DummyPostData(a=['  foo bar  '], b=['hi']))
     self.assertEqual(form.a.data, 'foo bar')
     self.assertEqual(form.b.data, 'hi')
     self.assertEqual(len(form.b.process_errors), 1)
     assert not form.validate()
Example #20
0
 def test_rendering(self):
     form = self.BoringForm(DummyPostData(bool2="x"))
     self.assertEqual(str(form.bool1), '<input id="bool1" name="bool1" type="checkbox" value="y"/>')
     self.assertEqual(str(form.bool2), '<input checked="checked" id="bool2" name="bool2" type="checkbox" value="x"/>')
     self.assertEqual(form.bool2.raw_data, ['x'])
Example #21
0
 def test_with_postdata_and_model(self):
     form = self.BoringForm(DummyPostData(bool1=['y']), obj=self.obj)
     self.assertEqual(form.bool1.data, True)
     self.assertEqual(form.bool2.data, False)
Example #22
0
def test_validate_choices_when_none():
    F = make_form(a=SelectField())
    form = F(DummyPostData(a="b"))
    with pytest.raises(TypeError, match="Choices cannot be None"):
        form.validate()
Example #23
0
 def test_formdata(self):
     form = self.F1(DummyPostData({'a-a': ['moo']}))
     self.assertEqual(form.a.form.a.name, 'a-a')
     self.assertEqual(form.a['a'].data, 'moo')
     self.assertEqual(form.a['b'].data, '')
     self.assertTrue(form.validate())
Example #24
0
def test_dont_validate_choices():
    F = make_form(a=SelectField(choices=[("a", "Foo")], validate_choice=False))
    form = F(DummyPostData(a=["b"]))
    assert form.validate()
    assert form.a.data == "b"
    assert len(form.a.errors) == 0
Example #25
0
 def _test_phase2(self, form_class, session, token, must_validate=True):
     form = form_class(formdata=DummyPostData(csrf_token=token),
                       meta={'csrf_context': session})
     if must_validate:
         assert form.validate()
     return form
Example #26
0
 def test_errors(self):
     F = make_form(a=FieldList(self.t))
     form = F(DummyPostData({'a-0': ['a'], 'a-1': ''}))
     assert not form.validate()
     self.assertEqual(form.a.errors, [[], ['This field is required.']])