Projet

Général

Profil

0002-api-use-StringOrListField-for-recurrence_days-serial.patch

Nicolas Roche, 14 février 2022 17:38

Télécharger (3,04 ko)

Voir les différences:

Subject: [PATCH 2/2] api: use StringOrListField for recurrence_days serialiser
 (#60351)

 chrono/api/serializers.py |  2 +-
 tests/api/test_event.py   | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
chrono/api/serializers.py
265 265

  
266 266
class AgendaSlugsSerializer(serializers.Serializer):
267 267
    agendas = CommaSeparatedStringField(
268 268
        required=True, child=serializers.SlugField(max_length=160, allow_blank=False)
269 269
    )
270 270

  
271 271

  
272 272
class EventSerializer(serializers.ModelSerializer):
273
    recurrence_days = CommaSeparatedStringField(
273
    recurrence_days = StringOrListField(
274 274
        required=False, child=serializers.IntegerField(min_value=0, max_value=6)
275 275
    )
276 276

  
277 277
    class Meta:
278 278
        model = Event
279 279
        fields = [
280 280
            'start_datetime',
281 281
            'recurrence_days',
tests/api/test_event.py
157 157
    agenda.kind = 'meetings'
158 158
    agenda.save()
159 159
    app.post('/api/agenda/%s/check/%s/' % (agenda.slug, event.slug), status=404)
160 160
    agenda.kind = 'virtual'
161 161
    agenda.save()
162 162
    app.post('/api/agenda/%s/check/%s/' % (agenda.slug, event.slug), status=404)
163 163

  
164 164

  
165
@pytest.mark.parametrize(
166
    'days_in, days_out, err_msg',
167
    [
168
        (1, None, 'Expected a list of items but got type "int".'),
169
        ('2', [2], None),
170
        ([3], [3], None),
171
        (['4'], [4], None),
172
        ([1, 2], [1, 2], None),
173
        (['2', '3'], [2, 3], None),
174
        ('4, 5', [4, 5], None),
175
    ],
176
)
177
def test_string_or_list_serialiser(app, user, days_in, days_out, err_msg):
178
    app.authorization = ('Basic', ('john.doe', 'password'))
179
    agenda = Agenda(label='Foo bar')
180
    agenda.maximal_booking_delay = 0
181
    agenda.save()
182
    api_url = '/api/agenda/%s/event/' % (agenda.slug)
183

  
184
    params = {
185
        'start_datetime': '2022-02-03 16:00',
186
        'places': '1',
187
        'recurrence_days': days_in,
188
    }
189
    if not err_msg:
190
        resp = app.post_json(api_url, params=params)
191
        assert not resp.json['err']
192
        assert Event.objects.get().recurrence_days == days_out
193
    else:
194
        resp = app.post_json(api_url, params=params, status=400)
195
        assert resp.json['err_desc'] == 'invalid payload'
196
        assert resp.json['errors']['recurrence_days'][0] == err_msg
197

  
198

  
165 199
@pytest.mark.freeze_time('2021-11-01')
166 200
def test_add_event(app, user):
167 201
    api_url = '/api/agenda/%s/event/' % ('999')
168 202

  
169 203
    # no authentication
170 204
    resp = app.post(api_url, status=401)
171 205
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
172 206

  
173
-