87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
import datetime
|
|
import pytest
|
|
from webtest import TestApp
|
|
|
|
from django.test import override_settings
|
|
from django.utils.timezone import now
|
|
|
|
from chrono.agendas.models import Agenda, Event, Booking
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
@pytest.fixture
|
|
def some_data():
|
|
agenda = Agenda(label=u'Foo bar')
|
|
agenda.save()
|
|
first_date = datetime.datetime.now().replace(hour=17, minute=0, second=0)
|
|
first_date += datetime.timedelta(days=1)
|
|
for i in range(3):
|
|
event = Event(start_datetime=first_date + datetime.timedelta(days=i),
|
|
places=20, agenda=agenda)
|
|
event.save()
|
|
|
|
agenda2 = Agenda(label=u'Foo bar2')
|
|
agenda2.save()
|
|
first_date = datetime.datetime.now().replace(hour=20, minute=0, second=0)
|
|
first_date += datetime.timedelta(days=1)
|
|
for i in range(2):
|
|
event = Event(start_datetime=first_date + datetime.timedelta(days=i),
|
|
places=20, agenda=agenda2)
|
|
event.save()
|
|
|
|
# a date in the past
|
|
event = Event(start_datetime=first_date - datetime.timedelta(days=10),
|
|
places=10, agenda=agenda)
|
|
event.save()
|
|
|
|
|
|
def test_datetimes_api(app, some_data):
|
|
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
|
|
resp = app.get('/api/agenda/%s/datetimes/' % agenda_id)
|
|
assert 'data' in resp.json
|
|
assert len(resp.json['data']) == 3
|
|
|
|
|
|
def test_datetime_api_fr(app, some_data):
|
|
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
|
|
with override_settings(LANGUAGE_CODE='fr-fr'):
|
|
resp = app.get('/api/agenda/%s/datetimes/' % agenda_id)
|
|
# no seconds, hh:mm in 24-hour formats
|
|
assert resp.json['data'][0]['text'].endswith(' 17:00')
|
|
assert 'data' in resp.json
|
|
|
|
|
|
def test_booking_api(app, some_data):
|
|
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
|
|
event = Event.objects.filter(agenda_id=agenda_id)[0]
|
|
resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda_id, event.id))
|
|
Booking.objects.get(id=resp.json['booking_id'])
|
|
assert Booking.objects.count() == 1
|
|
|
|
def test_booking_api_with_data(app, some_data):
|
|
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
|
|
event = Event.objects.filter(agenda_id=agenda_id)[0]
|
|
resp = app.post_json('/api/agenda/%s/fillslot/%s/' % (agenda_id, event.id),
|
|
params={'hello': 'world'})
|
|
assert Booking.objects.count() == 1
|
|
assert Booking.objects.all()[0].extra_data == {'hello': 'world'}
|
|
|
|
def test_soldout(app, some_data):
|
|
agenda_id = Agenda.objects.filter(label=u'Foo bar')[0].id
|
|
event = Event.objects.filter(agenda_id=agenda_id).exclude(start_datetime__lt=now())[0]
|
|
|
|
resp = app.get('/api/agenda/%s/datetimes/' % agenda_id)
|
|
assert len(resp.json['data']) == 3
|
|
assert event.id in [x['id'] for x in resp.json['data']]
|
|
|
|
for i in range(event.places):
|
|
Booking(event=event).save()
|
|
|
|
resp = app.get('/api/agenda/%s/datetimes/' % agenda_id)
|
|
assert len(resp.json['data']) == 2
|
|
assert not event.id in [x['id'] for x in resp.json['data']]
|
|
|
|
resp = app.post('/api/agenda/%s/fillslot/%s/' % (agenda_id, event.id), status=400)
|
|
assert resp.json['err'] == 1
|
|
assert resp.json['reason'] == 'sold out'
|