Projet

Général

Profil

0003-testdef-test-field-conditions-71296.patch

Valentin Deniaud, 14 décembre 2022 18:09

Télécharger (2,73 ko)

Voir les différences:

Subject: [PATCH 3/3] testdef: test field conditions (#71296)

 tests/test_testdef.py | 39 +++++++++++++++++++++++++++++++++++++++
 wcs/testdef.py        |  8 ++++++++
 2 files changed, 47 insertions(+)
tests/test_testdef.py
115 115
    with pytest.raises(TestError) as excinfo:
116 116
        testdef.run(formdef)
117 117
    assert str(excinfo.value) == 'Failed to evaluate page 1 post condition.'
118

  
119

  
120
def test_field_conditions(pub):
121
    formdef = FormDef()
122
    formdef.name = 'test title'
123
    formdef.fields = [
124
        fields.StringField(id='1', label='Text', varname='text'),
125
        fields.StringField(
126
            id='2',
127
            label='Text with condition',
128
            varname='text_cond',
129
            required=False,
130
            condition={'type': 'django', 'value': 'form_var_text == "a"'},
131
        ),
132
    ]
133
    formdef.store()
134

  
135
    formdata = formdef.data_class()()
136
    formdata.just_created()
137
    formdata.receipt_time = datetime.datetime(2021, 1, 1, 0, 0).timetuple()
138
    formdata.data['1'] = 'a'
139
    formdata.data['2'] = 'xxx'
140

  
141
    testdef = TestDef.create_from_formdata(formdef, formdata)
142
    testdef.run(formdef)
143

  
144
    formdata.data['1'] = 'b'
145
    testdef = TestDef.create_from_formdata(formdef, formdata)
146
    with pytest.raises(TestError) as excinfo:
147
        testdef.run(formdef)
148
    assert str(excinfo.value) == 'Tried to fill field "Text with condition" on page 0 but it is hidden.'
149

  
150
    del formdata.data['2']
151
    testdef = TestDef.create_from_formdata(formdef, formdata)
152
    testdef.run(formdef)
153

  
154
    formdata.data['1'] = 'a'
155
    testdef = TestDef.create_from_formdata(formdef, formdata)
156
    testdef.run(formdef)
wcs/testdef.py
90 90
            if field.type in ('subtitle', 'title', 'comment', 'computed'):
91 91
                continue
92 92

  
93
            if not field.is_visible(formdata.data, objectdef):
94
                if field.id in self.data['fields']:
95
                    raise TestError(
96
                        _('Tried to fill field "%(label)s" on page %(no)d but it is hidden.')
97
                        % {'label': field.label, 'no': page_no}
98
                    )
99
                continue
100

  
93 101
            formdata.data[field.id] = self.data['fields'].get(field.id)
94 102
            get_publisher().substitutions.invalidate_cache()
95 103

  
96
-