Projet

Général

Profil

0002-api-accept-int-on-recurrence_days-serialiser-60351.patch

Nicolas Roche, 02 février 2022 19:10

Télécharger (2,63 ko)

Voir les différences:

Subject: [PATCH 2/2] api: accept int on recurrence_days serialiser (#60351)

 chrono/api/serializers.py | 3 +++
 tests/api/test_event.py   | 9 +++++++++
 2 files changed, 12 insertions(+)
chrono/api/serializers.py
18 18
    return objects
19 19

  
20 20

  
21 21
class StringOrListField(serializers.ListField):
22 22
    def to_internal_value(self, data):
23 23
        if isinstance(data, list) and len(data) == 1 and isinstance(data[0], str):
24 24
            # accept comma separated string passed as a list of only one string item
25 25
            data = data[0]
26
        elif isinstance(data, int):
27
            # accept int as the first element of a list
28
            data = [data]
26 29
        if isinstance(data, str):
27 30
            data = [s.strip() for s in data.split(',') if s.strip()]
28 31
        return super().to_internal_value(data)
29 32

  
30 33

  
31 34
class CommaSeparatedStringField(serializers.ListField):
32 35
    def get_value(self, dictionary):
33 36
        return super(serializers.ListField, self).get_value(dictionary)
tests/api/test_event.py
1 1
import datetime
2
import json
2 3

  
3 4
import pytest
4 5
from django.utils.timezone import localtime, now
5 6

  
6 7
from chrono.agendas.models import Agenda, Booking, Event
7 8

  
8 9
pytestmark = pytest.mark.django_db
9 10

  
......
351 352
        'places': 12,
352 353
        'recurrence_days': recurrence_days,
353 354
    }
354 355
    assert Event.objects.filter(agenda=agenda).count() == 0
355 356
    resp = app.post(api_url, params=params)
356 357
    assert Event.objects.filter(agenda=agenda).count() == 1
357 358
    assert not resp.json['err']
358 359

  
360
    headers = {'Content-type': 'application/json'}
361
    data = '{"start_datetime": "2022-01-20 16:00", "places": "3", "recurrence_days": %s}' % json.dumps(
362
        recurrence_days
363
    )
364
    resp = app.post(api_url, headers=headers, params=data)
365
    assert Event.objects.filter(agenda=agenda).count() == 2
366
    assert not resp.json['err']
367

  
359 368

  
360 369
@pytest.mark.freeze_time('2021-11-01')
361 370
def test_update_event(app, user):
362 371
    api_url = '/api/agenda/%s/event/%s/' % ('nop', 'nop')
363 372

  
364 373
    # no authentication
365 374
    resp = app.patch(api_url, status=401)
366 375
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
367
-