Projet

Général

Profil

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

Valentin Deniaud, 29 novembre 2022 17:23

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
95 95
    with pytest.raises(TestError) as excinfo:
96 96
        testdef.run(formdef)
97 97
    assert str(excinfo.value) == 'Failed to evaluate page 1 post condition.'
98

  
99

  
100
def test_field_conditions(pub):
101
    formdef = FormDef()
102
    formdef.name = 'test title'
103
    formdef.fields = [
104
        fields.StringField(id='1', label='Text', varname='text'),
105
        fields.StringField(
106
            id='2',
107
            label='Text with condition',
108
            varname='text_cond',
109
            required=False,
110
            condition={'type': 'django', 'value': 'form_var_text == "a"'},
111
        ),
112
    ]
113
    formdef.store()
114

  
115
    formdata = formdef.data_class()()
116
    formdata.just_created()
117
    formdata.receipt_time = datetime.datetime(2021, 1, 1, 0, 0).timetuple()
118
    formdata.data['1'] = 'a'
119
    formdata.data['2'] = 'xxx'
120

  
121
    testdef = TestDef.create_from_formdata(formdef, formdata)
122
    testdef.run(formdef)
123

  
124
    formdata.data['1'] = 'b'
125
    testdef = TestDef.create_from_formdata(formdef, formdata)
126
    with pytest.raises(TestError) as excinfo:
127
        testdef.run(formdef)
128
    assert str(excinfo.value) == 'Tried to fill field "Text with condition" on page 0 but it is hidden.'
129

  
130
    del formdata.data['2']
131
    testdef = TestDef.create_from_formdata(formdef, formdata)
132
    testdef.run(formdef)
133

  
134
    formdata.data['1'] = 'a'
135
    testdef = TestDef.create_from_formdata(formdef, formdata)
136
    testdef.run(formdef)
wcs/testdef.py
83 83
            if field.type in ('subtitle', 'title', 'comment', 'computed'):
84 84
                continue
85 85

  
86
            if not field.is_visible(formdata.data, objectdef):
87
                if field.id in self.data['fields']:
88
                    raise TestError(
89
                        _('Tried to fill field "%(label)s" on page %(no)d but it is hidden.')
90
                        % {'label': field.label, 'no': page_no}
91
                    )
92
                continue
93

  
86 94
            formdata.data[field.id] = self.data['fields'].get(field.id)
87 95
            get_publisher().substitutions.invalidate_cache()
88 96

  
89
-