From 65171d1276484bfb6c707be1b9f73e9491697203 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Mon, 11 May 2020 01:16:58 +0200 Subject: [PATCH] Fix event tests --- events/tests/test_views.py | 54 ++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/events/tests/test_views.py b/events/tests/test_views.py index ee17128b..1ccd3530 100644 --- a/events/tests/test_views.py +++ b/events/tests/test_views.py @@ -1,4 +1,3 @@ -import csv from unittest import mock from django.contrib.auth import get_user_model @@ -14,6 +13,7 @@ from events.models import ( OptionChoice, Registration, ) +from shared.tests.mixins import CSVResponseMixin User = get_user_model() @@ -70,7 +70,7 @@ class CSVExportAccessTest(MessagePatch, TestCase): self.assertEqual(r.status_code, 403) -class CSVExportContentTest(MessagePatch, TestCase): +class CSVExportContentTest(MessagePatch, CSVResponseMixin, TestCase): def setUp(self): super().setUp() @@ -90,13 +90,26 @@ class CSVExportContentTest(MessagePatch, TestCase): def test_simple_event(self): self.event.subscribers.set([self.u1, self.u2]) - participants = self.client.get(self.url).content.decode("utf-8") - participants = [ - line for line in csv.reader(participants.split("\n")) if line != [] - ] - self.assertEqual(len(participants), 3) - self.assertEqual(participants[1], ["toto_foo", "toto@a.b", "toto", "foo"]) - self.assertEqual(participants[2], ["titi_bar", "titi@a.b", "titi", "bar"]) + response = self.client.get(self.url) + + content = self.load_from_csv_response(response, as_dict=True) + self.assertListEqual( + content, + [ + { + "username": "toto_foo", + "prénom": "toto", + "nom de famille": "foo", + "email": "toto@a.b", + }, + { + "username": "titi_bar", + "prénom": "titi", + "nom de famille": "bar", + "email": "titi@a.b", + }, + ], + ) def test_complex_event(self): registration = Registration.objects.create(event=self.event, user=self.u1) @@ -127,15 +140,22 @@ class CSVExportContentTest(MessagePatch, TestCase): field=field, registration=registration, content="hello" ) - participants = self.client.get(self.url).content.decode("utf-8") - participants = list(csv.reader(participants.split("\n"))) - toto_registration = participants[1] + response = self.client.get(self.url) + content = self.load_from_csv_response(response, as_dict=True) + toto_dict = content[0] # This is not super nice, but it makes the test deterministic. - if toto_registration[5] == "f & d": - toto_registration[5] = "d & f" + toto_dict["def"] = [x.strip() for x in toto_dict["def"].split("&")] - self.assertEqual( - ["toto_foo", "toto@a.b", "toto", "foo", "a", "d & f", "hello"], - toto_registration, + self.assertDictEqual( + toto_dict, + { + "username": "toto_foo", + "prénom": "toto", + "nom de famille": "foo", + "email": "toto@a.b", + "abc": "a", + "def": ["d", "f"], + "remarks": "hello", + }, )