tests: update to use django_webtest and not to rely on hardcoded object ids
This commit is contained in:
parent
05876a7b12
commit
f80afdd268
|
@ -8,7 +8,7 @@ rm -f test_results.xml
|
|||
export PIP_USE_WHEEL=no
|
||||
pip install --upgrade 'pip<8'
|
||||
pip install --upgrade pylint pylint-django
|
||||
pip install --upgrade pytest pytest-django pytest-cov WebTest
|
||||
pip install --upgrade pytest pytest-django pytest-cov WebTest django-webtest
|
||||
pip install --upgrade -r requirements.txt
|
||||
|
||||
PYTHONPATH=$(pwd):$PYTHONPATH DJANGO_SETTINGS_MODULE=chrono.settings CHRONO_SETTINGS_FILE=tests/settings.py \
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
import pytest
|
||||
|
||||
import django_webtest
|
||||
|
||||
@pytest.fixture
|
||||
def app(request):
|
||||
wtm = django_webtest.WebTestMixin()
|
||||
wtm._patch_settings()
|
||||
request.addfinalizer(wtm._unpatch_settings)
|
||||
return django_webtest.DjangoTestApp()
|
|
@ -5,7 +5,6 @@ from webtest import TestApp
|
|||
from django.test import override_settings
|
||||
from django.utils.timezone import now
|
||||
|
||||
from chrono.wsgi import application
|
||||
from chrono.agendas.models import Agenda, Event, Booking
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
@ -36,52 +35,52 @@ def some_data():
|
|||
event.save()
|
||||
|
||||
|
||||
def test_datetimes_api(some_data):
|
||||
app = TestApp(application)
|
||||
resp = app.get('/api/agenda/1/datetimes/')
|
||||
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(some_data):
|
||||
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'):
|
||||
app = TestApp(application)
|
||||
resp = app.get('/api/agenda/1/datetimes/')
|
||||
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(some_data):
|
||||
app = TestApp(application)
|
||||
event = Event.objects.filter(agenda_id=1)[0]
|
||||
resp = app.post('/api/agenda/1/fillslot/%s/' % event.id)
|
||||
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(some_data):
|
||||
app = TestApp(application)
|
||||
event = Event.objects.filter(agenda_id=1)[0]
|
||||
resp = app.post_json('/api/agenda/1/fillslot/%s/' % event.id,
|
||||
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(some_data):
|
||||
app = TestApp(application)
|
||||
event = Event.objects.filter(agenda_id=1).exclude(start_datetime__lt=now())[0]
|
||||
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/1/datetimes/')
|
||||
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/1/datetimes/')
|
||||
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/1/fillslot/%s/' % event.id, status=400)
|
||||
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'
|
||||
|
|
|
@ -26,58 +26,58 @@ def login(app, username='admin', password='admin'):
|
|||
assert resp.status_int == 302
|
||||
return app
|
||||
|
||||
def test_unlogged_access():
|
||||
def test_unlogged_access(app):
|
||||
# connect while not being logged in
|
||||
app = TestApp(application)
|
||||
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
||||
|
||||
def test_access(admin_user):
|
||||
app = login(TestApp(application))
|
||||
def test_access(app, admin_user):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/', status=200)
|
||||
assert '<h2>Agendas</h2>' in resp.body
|
||||
assert "This site doesn't have any agenda yet." in resp.body
|
||||
|
||||
def test_logout(admin_user):
|
||||
app = login(TestApp(application))
|
||||
def test_logout(app, admin_user):
|
||||
app = login(app)
|
||||
app.get('/logout/')
|
||||
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
||||
|
||||
def test_menu_json(admin_user):
|
||||
app = login(TestApp(application))
|
||||
def test_menu_json(app, admin_user):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/menu.json', status=200)
|
||||
assert resp.json[0]['url'] == 'http://localhost:80/manage/'
|
||||
assert resp.json[0]['label'] == 'Agendas'
|
||||
resp2 = app.get('/manage/menu.json?callback=Q', status=200)
|
||||
assert resp2.body == 'Q(%s);' % resp.body
|
||||
|
||||
def test_add_agenda(admin_user):
|
||||
app = login(TestApp(application))
|
||||
def test_add_agenda(app, admin_user):
|
||||
app = login(app)
|
||||
resp = app.get('/manage/', status=200)
|
||||
resp = resp.click('New')
|
||||
resp.form['label'] = 'Foo bar'
|
||||
resp = resp.form.submit()
|
||||
assert resp.location == 'http://localhost:80/manage/agendas/1/'
|
||||
agenda = Agenda.objects.get(label='Foo bar')
|
||||
assert resp.location == 'http://localhost:80/manage/agendas/%s/' % agenda.id
|
||||
resp = resp.follow()
|
||||
assert '<h2>Agenda - Foo bar</h2>' in resp.body
|
||||
|
||||
def test_rename_agenda(admin_user):
|
||||
def test_rename_agenda(app, admin_user):
|
||||
agenda = Agenda(label=u'Foo bar')
|
||||
agenda.save()
|
||||
app = login(TestApp(application))
|
||||
app = login(app)
|
||||
resp = app.get('/manage/', status=200)
|
||||
resp = resp.click('Foo bar')
|
||||
resp = resp.click('Rename')
|
||||
assert resp.form['label'].value == 'Foo bar'
|
||||
resp.form['label'] = 'Foo baz'
|
||||
resp = resp.form.submit()
|
||||
assert resp.location == 'http://localhost:80/manage/agendas/1/'
|
||||
assert resp.location == 'http://localhost:80/manage/agendas/%s/' % agenda.id
|
||||
resp = resp.follow()
|
||||
assert '<h2>Agenda - Foo baz</h2>' in resp.body
|
||||
|
||||
def test_delete_agenda(admin_user):
|
||||
def test_delete_agenda(app, admin_user):
|
||||
agenda = Agenda(label=u'Foo bar')
|
||||
agenda.save()
|
||||
app = login(TestApp(application))
|
||||
app = login(app)
|
||||
resp = app.get('/manage/', status=200)
|
||||
resp = resp.click('Foo bar')
|
||||
resp = resp.click('Delete')
|
||||
|
@ -86,41 +86,42 @@ def test_delete_agenda(admin_user):
|
|||
resp = resp.follow()
|
||||
assert not 'Foo bar' in resp.body
|
||||
|
||||
def test_add_event(admin_user):
|
||||
def test_add_event(app, admin_user):
|
||||
agenda = Agenda(label=u'Foo bar')
|
||||
agenda.save()
|
||||
app = login(TestApp(application))
|
||||
resp = app.get('/manage/agendas/1/', status=200)
|
||||
app = login(app)
|
||||
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
||||
assert "This agenda doesn't have any event yet." in resp.body
|
||||
resp = resp.click('New Event')
|
||||
resp.form['start_datetime'] = '2016-02-15 17:00'
|
||||
resp.form['places'] = 10
|
||||
resp = resp.form.submit()
|
||||
resp = resp.follow()
|
||||
event = Event.objects.get(places=10)
|
||||
assert not "This agenda doesn't have any event yet." in resp.body
|
||||
assert '/manage/events/1/' in resp.body
|
||||
assert '/manage/events/%s/' % event.id in resp.body
|
||||
assert 'Feb. 15, 2016, 5 p.m.' in resp.body
|
||||
assert '10 places' in resp.body
|
||||
|
||||
def test_edit_event(admin_user):
|
||||
def test_edit_event(app, admin_user):
|
||||
agenda = Agenda(label=u'Foo bar')
|
||||
agenda.save()
|
||||
event = Event(start_datetime=datetime.datetime(2016, 2, 15, 17, 0),
|
||||
places=20, agenda=agenda)
|
||||
event.save()
|
||||
app = login(TestApp(application))
|
||||
resp = app.get('/manage/agendas/1/', status=200)
|
||||
app = login(app)
|
||||
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
||||
resp = resp.click('Feb. 15, 2016, 5 p.m.')
|
||||
assert resp.form['start_datetime'].value == '15/02/2016 17:00'
|
||||
resp.form['start_datetime'] = '2016-02-16 17:00'
|
||||
resp.form['places'] = 20
|
||||
resp = resp.form.submit()
|
||||
resp = resp.follow()
|
||||
assert '/manage/events/1/' in resp.body
|
||||
assert '/manage/events/%s/' % event.id in resp.body
|
||||
assert 'Feb. 16, 2016, 5 p.m.' in resp.body
|
||||
assert '20 places' in resp.body
|
||||
|
||||
def test_booked_places(admin_user):
|
||||
def test_booked_places(app, admin_user):
|
||||
agenda = Agenda(label=u'Foo bar')
|
||||
agenda.save()
|
||||
event = Event(start_datetime=datetime.datetime(2016, 2, 15, 17, 0),
|
||||
|
@ -128,7 +129,7 @@ def test_booked_places(admin_user):
|
|||
event.save()
|
||||
Booking(event=event).save()
|
||||
Booking(event=event).save()
|
||||
app = login(TestApp(application))
|
||||
resp = app.get('/manage/agendas/1/', status=200)
|
||||
app = login(app)
|
||||
resp = app.get('/manage/agendas/%s/' % agenda.id, status=200)
|
||||
assert '10 places' in resp.body
|
||||
assert '2 booked places' in resp.body
|
||||
|
|
Loading…
Reference in New Issue