Projet

Général

Profil

0001-api-count-correctly-requested-places-on-multiple-boo.patch

Emmanuel Cazenave, 20 décembre 2019 10:45

Télécharger (3,5 ko)

Voir les différences:

Subject: [PATCH] api: count correctly requested places on multiple booking
 (#38306)

 chrono/api/views.py |  5 ++++-
 tests/test_api.py   | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
chrono/api/views.py
496 496
                if to_cancel_booking.cancellation_datetime:
497 497
                    cancel_error = gettext_noop('cancel booking: booking already cancelled')
498 498
                else:
499
                    to_cancel_places_count = to_cancel_booking.secondary_booking_set.count() + 1
499
                    to_cancel_places_count = (
500
                        to_cancel_booking.secondary_booking_set.filter(event=to_cancel_booking.event).count()
501
                        + 1
502
                    )
500 503
                    if places_count != to_cancel_places_count:
501 504
                        cancel_error = gettext_noop('cancel booking: count is different')
502 505
            except Booking.DoesNotExist:
tests/test_api.py
1467 1467
        assert Event.objects.get(id=event.id).waiting_list == 2
1468 1468

  
1469 1469

  
1470
def test_multiple_booking_move_booking(app, user):
1471
    agenda = Agenda(label=u'Foo bar')
1472
    agenda.save()
1473
    first_date = localtime(now()).replace(hour=17, minute=0, second=0, microsecond=0)
1474
    first_date += datetime.timedelta(days=1)
1475
    events = []
1476
    for i in range(10):
1477
        event = Event(start_datetime=first_date + datetime.timedelta(days=i), places=20, agenda=agenda)
1478
        event.save()
1479
        events.append(event)
1480

  
1481
    first_two_events = events[:2]
1482
    events_ids = [x.id for x in first_two_events]
1483
    resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id)
1484
    slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids]
1485

  
1486
    app.authorization = ('Basic', ('john.doe', 'password'))
1487

  
1488
    # get 1 place on 2 slots
1489
    resp = app.post('/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots})
1490
    booking = Booking.objects.get(id=resp.json['booking_id'])
1491
    assert Booking.objects.filter(primary_booking=booking).count() == 1
1492
    for event in first_two_events:
1493
        assert Event.objects.get(id=event.id).booked_places == 1
1494

  
1495
    # change, 1 place on 2 other slots
1496
    last_two_events = events[-2:]
1497
    events_ids = [x.id for x in last_two_events]
1498
    resp_datetimes = app.get('/api/agenda/%s/datetimes/' % agenda.id)
1499
    slots = [x['id'] for x in resp_datetimes.json['data'] if x['id'] in events_ids]
1500

  
1501
    resp = app.post(
1502
        '/api/agenda/%s/fillslots/' % agenda.slug, params={'slots': slots, 'cancel_booking_id': booking.pk}
1503
    )
1504
    booking = Booking.objects.get(id=resp.json['booking_id'])
1505
    assert Booking.objects.filter(primary_booking=booking).count() == 1
1506
    for event in first_two_events:
1507
        assert Event.objects.get(id=event.id).booked_places == 0
1508
    for event in last_two_events:
1509
        assert Event.objects.get(id=event.id).booked_places == 1
1510

  
1511

  
1470 1512
def test_agenda_detail_api(app, some_data):
1471 1513
    agenda = Agenda.objects.get(slug='foo-bar')
1472 1514
    resp = app.get('/api/agenda/%s/' % agenda.slug)
1473
-