Merge branch 'aureplop/cof-tests_export' into 'master'

cof -- Add tests for export views

See merge request cof-geek/gestioCOF!291
This commit is contained in:
Martin Pepin 2018-04-07 12:15:06 +02:00
commit 0cceed9052
4 changed files with 175 additions and 9 deletions

View file

@ -7,15 +7,15 @@
<h2>Liens utiles du COF</h2>
<h3>COF</h3>
<ul>
<li><a href="{% url 'gestioncof.views.export_members' %}">Export des membres du COF</a></li>
<li><a href="{% url 'cof.membres_export' %}">Export des membres du COF</a></li>
<li><a href="{% url 'gestioncof.views.liste_diffcof' %}">Diffusion COF</a></li>
</ul>
<h3>Mega</h3>
<ul>
<li><a href="{% url 'gestioncof.views.export_mega_participants' %}">Export des non-orgas uniquement</a></li>
<li><a href="{% url 'gestioncof.views.export_mega_orgas' %}">Export des orgas uniquement</a></li>
<li><a href="{% url 'gestioncof.views.export_mega' %}">Export de tout le monde</a></li>
<li><a href="{% url 'cof.mega_export_participants' %}">Export des non-orgas uniquement</a></li>
<li><a href="{% url 'cof.mega_export_orgas' %}">Export des orgas uniquement</a></li>
<li><a href="{% url 'cof.mega_export' %}">Export de tout le monde</a></li>
</ul>
<p>Note&nbsp;: pour ouvrir les fichiers .csv avec Excel, il faut

View file

@ -1,3 +1,4 @@
import csv
import uuid
from datetime import timedelta
@ -16,6 +17,161 @@ from gestioncof.tests.testcases import ViewTestCaseMixin
from .utils import create_user
class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
url_name = 'cof.membres_export'
url_expected = '/export/members'
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
def test(self):
u1, u2 = self.users['member'], self.users['staff']
u1.first_name = 'first'
u1.last_name = 'last'
u1.email = 'user@mail.net'
u1.save()
u1.profile.phone = '0123456789'
u1.profile.departement = 'Dept'
u1.profile.save()
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
data = list(csv.reader(r.content.decode('utf-8').split('\n')[:-1]))
expected = [
[
str(u1.pk), 'member', 'first', 'last', 'user@mail.net',
'0123456789', '1A', 'Dept', 'normalien',
],
[str(u2.pk), 'staff', '', '', '', '', '1A', '', 'normalien'],
]
# Sort before checking equality, the order of the output of csv.reader
# does not seem deterministic
expected.sort(key=lambda row: int(row[0]))
data.sort(key=lambda row: int(row[0]))
self.assertListEqual(data, expected)
class MegaHelpers:
def setUp(self):
super().setUp()
u1 = create_user('u1')
u1.first_name = 'first'
u1.last_name = 'last'
u1.email = 'user@mail.net'
u1.save()
u1.profile.phone = '0123456789'
u1.profile.departement = 'Dept'
u1.profile.comments = 'profile.comments'
u1.profile.save()
u2 = create_user('u2')
u2.profile.save()
m = Event.objects.create(title='MEGA 2017')
cf1 = m.commentfields.create(name='Commentaire')
cf2 = m.commentfields.create(
name='Comment Field 2', fieldtype='char',
)
option_type = m.options.create(name='Conscrit/Orga ?')
choice_orga = option_type.choices.create(value='Orga')
choice_conscrit = option_type.choices.create(value='Conscrit')
mr1 = m.eventregistration_set.create(user=u1)
mr1.options.add(choice_orga)
mr1.comments.create(commentfield=cf1, content='Comment 1')
mr1.comments.create(commentfield=cf2, content='Comment 2')
mr2 = m.eventregistration_set.create(user=u2)
mr2.options.add(choice_conscrit)
self.u1 = u1
self.u2 = u2
self.m = m
self.choice_orga = choice_orga
self.choice_conscrit = choice_conscrit
class ExportMegaViewTests(MegaHelpers, ViewTestCaseMixin, TestCase):
url_name = 'cof.mega_export'
url_expected = '/export/mega'
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
def test(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(self.load_from_csv_response(r), [
[
'u1', 'first', 'last', 'user@mail.net', '0123456789',
str(self.u1.pk), 'profile.comments', 'Comment 1---Comment 2',
],
['u2', '', '', '', '', str(self.u2.pk), '', ''],
])
class ExportMegaOrgasViewTests(MegaHelpers, ViewTestCaseMixin, TestCase):
url_name = 'cof.mega_export_orgas'
url_expected = '/export/mega/orgas'
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
def test(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(self.load_from_csv_response(r), [
[
'u1', 'first', 'last', 'user@mail.net', '0123456789',
str(self.u1.pk), 'profile.comments', 'Comment 1---Comment 2',
],
])
class ExportMegaParticipantsViewTests(
MegaHelpers, ViewTestCaseMixin, TestCase):
url_name = 'cof.mega_export_participants'
url_expected = '/export/mega/participants'
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
def test(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(self.load_from_csv_response(r), [
['u2', '', '', '', '', str(self.u2.pk), '', ''],
])
class ExportMegaRemarksViewTests(
MegaHelpers, ViewTestCaseMixin, TestCase):
url_name = 'cof.mega_export_remarks'
url_expected = '/export/mega/avecremarques'
auth_user = 'staff'
auth_forbidden = [None, 'user', 'member']
def test(self):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(self.load_from_csv_response(r), [
[
'u1', 'first', 'last', 'user@mail.net', '0123456789',
str(self.u1.pk), 'profile.comments', 'Comment 1',
],
])
class ClubListViewTests(ViewTestCaseMixin, TestCase):
url_name = 'liste-clubs'
url_expected = '/clubs/liste'

View file

@ -6,12 +6,17 @@ from gestioncof import views, petits_cours_views
from gestioncof.decorators import buro_required
export_patterns = [
url(r'^members$', views.export_members),
url(r'^mega/avecremarques$', views.export_mega_remarksonly),
url(r'^mega/participants$', views.export_mega_participants),
url(r'^mega/orgas$', views.export_mega_orgas),
url(r'^members$', views.export_members,
name='cof.membres_export'),
url(r'^mega/avecremarques$', views.export_mega_remarksonly,
name='cof.mega_export_remarks'),
url(r'^mega/participants$', views.export_mega_participants,
name='cof.mega_export_participants'),
url(r'^mega/orgas$', views.export_mega_orgas,
name='cof.mega_export_orgas'),
# url(r'^mega/(?P<type>.+)$', views.export_mega_bytype),
url(r'^mega$', views.export_mega),
url(r'^mega$', views.export_mega,
name='cof.mega_export'),
]
petitcours_patterns = [

View file

@ -1,3 +1,4 @@
import csv
from unittest import mock
from urllib.parse import parse_qs, urlparse
@ -94,6 +95,10 @@ class TestCaseMixin:
else:
self.assertEqual(actual, expected)
def load_from_csv_response(self, r):
decoded = r.content.decode('utf-8')
return list(csv.reader(decoded.split('\n')[:-1]))
def _test_event_equal(self, event, exp):
for k, v_desc in exp.items():
if isinstance(v_desc, tuple):