diff --git a/jenkins.sh b/jenkins.sh index 5cabacf8..895fc1cd 100755 --- a/jenkins.sh +++ b/jenkins.sh @@ -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 \ diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..6a58cb4a --- /dev/null +++ b/tests/conftest.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() diff --git a/tests/test_api.py b/tests/test_api.py index ef3058d8..8b84843d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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' diff --git a/tests/test_manager.py b/tests/test_manager.py index ea84026a..b668883a 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -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 '