Expand CSVResponseMixin functionality

This commit is contained in:
Ludovic Stephan 2020-05-12 01:11:59 +02:00
parent 9b0440429c
commit 6fff995ccd
3 changed files with 49 additions and 38 deletions

View file

@ -92,9 +92,8 @@ class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase):
response = self.client.get(self.url)
content = self.load_from_csv_response(response, as_dict=True)
self.assertListEqual(
content,
self.assertCSVEqual(
response,
[
{
"username": "toto_foo",
@ -141,7 +140,7 @@ class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase):
)
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])
# This is not super nice, but it makes the test deterministic.

View file

@ -1,4 +1,3 @@
import csv
import os
import uuid
from datetime import timedelta
@ -17,7 +16,7 @@ from django.urls import reverse
from bda.models import Salle, Tirage
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
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 .utils import create_member, create_root, create_user
@ -463,7 +462,7 @@ class UserAutocompleteViewTests(ViewTestCaseMixin, TestCase):
self.assertEqual(r.status_code, 200)
class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
class ExportMembersViewTests(CSVResponseMixin, ViewTestCaseMixin, TestCase):
url_name = "cof.membres_export"
url_expected = "/export/members"
@ -483,8 +482,10 @@ class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
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 = [
self.assertCSVEqual(
r,
[
[
str(u1.pk),
"member",
@ -497,12 +498,8 @@ class ExportMembersViewTests(ViewTestCaseMixin, TestCase):
"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):
@ -516,8 +513,8 @@ class ExportMegaViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(
self.load_from_csv_response(r),
self.assertCSVEqual(
r,
[
[
"u1",
@ -546,8 +543,8 @@ class ExportMegaOrgasViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(
self.load_from_csv_response(r),
self.assertCSVEqual(
r,
[
[
"u1",
@ -574,9 +571,8 @@ class ExportMegaParticipantsViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCa
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), "", ""]],
self.assertCSVEqual(
r, [["u2", "", "", "", "", str(self.u2.pk), "", ""]],
)
@ -591,8 +587,8 @@ class ExportMegaRemarksViewTests(MegaHelperMixin, ViewTestCaseMixin, TestCase):
r = self.client.get(self.url)
self.assertEqual(r.status_code, 200)
self.assertListEqual(
self.load_from_csv_response(r),
self.assertCSVEqual(
r,
[
[
"u1",

View file

@ -58,17 +58,33 @@ class CSVResponseMixin:
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")
# la dernière ligne du fichier CSV est toujours vide
content = content.split("\n")[:-1]
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:
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: