forked from DGNum/gestioCOF
Expand CSVResponseMixin functionality
This commit is contained in:
parent
9b0440429c
commit
6fff995ccd
3 changed files with 49 additions and 38 deletions
|
@ -92,9 +92,8 @@ class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase):
|
||||||
|
|
||||||
response = self.client.get(self.url)
|
response = self.client.get(self.url)
|
||||||
|
|
||||||
content = self.load_from_csv_response(response, as_dict=True)
|
self.assertCSVEqual(
|
||||||
self.assertListEqual(
|
response,
|
||||||
content,
|
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"username": "toto_foo",
|
"username": "toto_foo",
|
||||||
|
@ -141,7 +140,7 @@ class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
response = self.client.get(self.url)
|
response = self.client.get(self.url)
|
||||||
content = self.load_from_csv_response(response, as_dict=True)
|
content = self._load_from_csv_response(response, as_dict=True)
|
||||||
toto_dict = dict(content[0])
|
toto_dict = dict(content[0])
|
||||||
|
|
||||||
# This is not super nice, but it makes the test deterministic.
|
# This is not super nice, but it makes the test deterministic.
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import csv
|
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
@ -17,7 +16,7 @@ from django.urls import reverse
|
||||||
from bda.models import Salle, Tirage
|
from bda.models import Salle, Tirage
|
||||||
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
|
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
|
||||||
from gestioncof.tests.mixins import MegaHelperMixin, ViewTestCaseMixin
|
from gestioncof.tests.mixins import MegaHelperMixin, ViewTestCaseMixin
|
||||||
from shared.tests.mixins import ICalMixin, MockLDAPMixin
|
from shared.tests.mixins import CSVResponseMixin, ICalMixin, MockLDAPMixin
|
||||||
from shared.views.autocomplete import Clipper
|
from shared.views.autocomplete import Clipper
|
||||||
|
|
||||||
from .utils import create_member, create_root, create_user
|
from .utils import create_member, create_root, create_user
|
||||||
|
@ -463,7 +462,7 @@ class UserAutocompleteViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
|
class ExportMembersViewTests(CSVResponseMixin, ViewTestCaseMixin, TestCase):
|
||||||
url_name = "cof.membres_export"
|
url_name = "cof.membres_export"
|
||||||
url_expected = "/export/members"
|
url_expected = "/export/members"
|
||||||
|
|
||||||
|
@ -483,26 +482,24 @@ class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
data = list(csv.reader(r.content.decode("utf-8").split("\n")[:-1]))
|
|
||||||
expected = [
|
self.assertCSVEqual(
|
||||||
|
r,
|
||||||
[
|
[
|
||||||
str(u1.pk),
|
[
|
||||||
"member",
|
str(u1.pk),
|
||||||
"first",
|
"member",
|
||||||
"last",
|
"first",
|
||||||
"user@mail.net",
|
"last",
|
||||||
"0123456789",
|
"user@mail.net",
|
||||||
"1A",
|
"0123456789",
|
||||||
"Dept",
|
"1A",
|
||||||
"normalien",
|
"Dept",
|
||||||
|
"normalien",
|
||||||
|
],
|
||||||
|
[str(u2.pk), "staff", "", "", "", "", "1A", "", "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 ExportMegaViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
|
class ExportMegaViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
|
||||||
|
@ -516,8 +513,8 @@ class ExportMegaViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
self.assertListEqual(
|
self.assertCSVEqual(
|
||||||
self.load_from_csv_response(r),
|
r,
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"u1",
|
"u1",
|
||||||
|
@ -546,8 +543,8 @@ class ExportMegaOrgasViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
self.assertListEqual(
|
self.assertCSVEqual(
|
||||||
self.load_from_csv_response(r),
|
r,
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"u1",
|
"u1",
|
||||||
|
@ -574,9 +571,8 @@ class ExportMegaParticipantsViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCa
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
self.assertListEqual(
|
self.assertCSVEqual(
|
||||||
self.load_from_csv_response(r),
|
r, [["u2", "", "", "", "", str(self.u2.pk), "", ""]],
|
||||||
[["u2", "", "", "", "", str(self.u2.pk), "", ""]],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -591,8 +587,8 @@ class ExportMegaRemarksViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
|
||||||
r = self.client.get(self.url)
|
r = self.client.get(self.url)
|
||||||
|
|
||||||
self.assertEqual(r.status_code, 200)
|
self.assertEqual(r.status_code, 200)
|
||||||
self.assertListEqual(
|
self.assertCSVEqual(
|
||||||
self.load_from_csv_response(r),
|
r,
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"u1",
|
"u1",
|
||||||
|
|
|
@ -58,17 +58,33 @@ class CSVResponseMixin:
|
||||||
par la première ligne du CSV.
|
par la première ligne du CSV.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def load_from_csv_response(self, r, as_dict=False, **reader_kwargs):
|
def _load_from_csv_response(self, r, as_dict=False, **reader_kwargs):
|
||||||
content = r.content.decode("utf-8")
|
content = r.content.decode("utf-8")
|
||||||
|
|
||||||
# la dernière ligne du fichier CSV est toujours vide
|
# la dernière ligne du fichier CSV est toujours vide
|
||||||
content = content.split("\n")[:-1]
|
content = content.split("\n")[:-1]
|
||||||
if as_dict:
|
if as_dict:
|
||||||
reader_class = csv.DictReader
|
content = csv.DictReader(content, **reader_kwargs)
|
||||||
|
# en python3.7, content est une liste d'OrderedDicts
|
||||||
|
return list(map(dict, content))
|
||||||
else:
|
else:
|
||||||
reader_class = csv.reader
|
content = csv.reader(content, **reader_kwargs)
|
||||||
|
return list(content)
|
||||||
|
|
||||||
return list(reader_class(content, **reader_kwargs))
|
def assertCSVEqual(self, response, expected):
|
||||||
|
if type(expected[0]) == list:
|
||||||
|
as_dict = False
|
||||||
|
elif type(expected[0]) == dict:
|
||||||
|
as_dict = True
|
||||||
|
else:
|
||||||
|
raise AssertionError(
|
||||||
|
"Unsupported type in `assertCSVEqual`: "
|
||||||
|
"%(expected)s is not of type `list` nor `dict` !"
|
||||||
|
% {"expected": str(expected[0])}
|
||||||
|
)
|
||||||
|
|
||||||
|
content = self._load_from_csv_response(response, as_dict=as_dict)
|
||||||
|
self.assertCountEqual(content, expected)
|
||||||
|
|
||||||
|
|
||||||
class ICalMixin:
|
class ICalMixin:
|
||||||
|
|
Loading…
Reference in a new issue