File: test_read_only_function.py

package info (click to toggle)
wtforms-components 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 1,582; makefile: 135; sh: 11
file content (41 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from wtforms import Form
from wtforms.fields import StringField
from wtforms_test import FormTestCase

from tests import MultiDict
from wtforms_components import read_only


class TestReadOnlyFunction(FormTestCase):
    def test_prevents_value_changing(self):
        class MyForm(Form):
            name = StringField(default="")

        form = MyForm()
        read_only(form.name)
        form.process(MultiDict({"name": "New value"}))
        assert form.name.data == ""

    def test_preserves_previous_value(self):
        class MyForm(Form):
            name = StringField()

        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_prevents_value_population(self):
        class MyForm(Form):
            name = StringField()

        class MyModel:
            pass

        form = MyForm()
        model = MyModel()
        form.name.data = "Existing value"
        read_only(form.name)
        form.populate_obj(model)
        assert not hasattr(model, "name")