From c00c57441b955db1d7df0efaad41947a5b953359 Mon Sep 17 00:00:00 2001 From: Evert-R Date: Thu, 14 Dec 2023 10:57:09 +0100 Subject: [PATCH] add tests for json files inside zip --- backend/experiment/admin.py | 4 +- .../experiment/tests/test_admin_experiment.py | 78 +++++++++++++++++-- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/backend/experiment/admin.py b/backend/experiment/admin.py index 0fe9939ae..fca9d6f32 100644 --- a/backend/experiment/admin.py +++ b/backend/experiment/admin.py @@ -67,7 +67,7 @@ def export(self, request, obj, parent_obj=None): all_songs |= Song.objects.filter(pk=section.song.pk) # create empty zip file in memory - zip_buffer = BytesIO() + zip_buffer = BytesIO() with ZipFile(zip_buffer, 'w') as new_zip: # serialize data to new json files within the zip file new_zip.writestr('sessions.json', data=str(serializers.serialize("json", all_sessions))) @@ -76,7 +76,7 @@ def export(self, request, obj, parent_obj=None): new_zip.writestr('results.json', data=str(serializers.serialize("json", all_results.order_by('session')))) new_zip.writestr('sections.json', data=str(serializers.serialize("json", all_sections.order_by('playlist', 'pk')))) new_zip.writestr('songs.json', data=str(serializers.serialize("json", all_songs.order_by('pk')))) - + # create forced download response response = HttpResponse(zip_buffer.getbuffer()) response['Content-Type'] = 'application/x-zip-compressed' diff --git a/backend/experiment/tests/test_admin_experiment.py b/backend/experiment/tests/test_admin_experiment.py index 0cd9372e6..d3f40e408 100644 --- a/backend/experiment/tests/test_admin_experiment.py +++ b/backend/experiment/tests/test_admin_experiment.py @@ -1,3 +1,6 @@ +from zipfile import ZipFile +from io import BytesIO +import json from django.test import Client, TestCase from django.forms.models import model_to_dict from django.contrib.admin.sites import AdminSite @@ -22,7 +25,7 @@ class MockRequest: this_experiment_admin = ExperimentAdmin( model=Experiment, admin_site=AdminSite) -class TestAdminExperiment(TestCase): +class TestAdminExperiment(TestCase): @classmethod def setUpTestData(cls): @@ -39,9 +42,6 @@ def setUpTestData(cls): session=Session.objects.first() ) - def setUp(self): - self.client = Client() - def test_experiment_model_fields(self): experiment = model_to_dict(Experiment.objects.first()) experiment_fields = [key for key in experiment] @@ -62,8 +62,74 @@ def test_participant_model(self): participant_fields = [key for key in participant] self.assertEqual(len(participant_fields), EXPECTED_PARTICIPANT_FIELDS) + + +class TestAdminExperimentExport(TestCase): + + fixtures = ['playlist', 'experiment'] + + @classmethod + def setUpTestData(cls): + cls.participant = Participant.objects.create(unique_hash=42) + cls.experiment = Experiment.objects.get(name='Hooked-China') + print(cls.experiment) + for playlist in cls.experiment.playlists.all(): + playlist.update_sections() + print(cls.experiment.pk) + cls.session = Session.objects.create( + experiment=cls.experiment, + participant=cls.participant, + ) + for i in range(5): + Result.objects.create( + session=Session.objects.first(), + expected_response = i, + given_response = i + ) + Result.objects.create( + participant=cls.participant, + question_key= i, + given_response = i + ) + + def setUp(self): + self.client = Client() + def test_admin_export(self): - experiment = Experiment.objects.first() - response = this_experiment_admin.export(request, experiment) + response = this_experiment_admin.export(request, self.experiment) + zip_buffer = BytesIO(response.content) + with ZipFile(zip_buffer, 'r') as test_zip: + # Test files inside zip + self.assertIn('participants.json', test_zip.namelist()) + self.assertIn('profiles.json', test_zip.namelist()) + self.assertIn('results.json', test_zip.namelist()) + self.assertIn('sections.json', test_zip.namelist()) + self.assertIn('sessions.json', test_zip.namelist()) + self.assertIn('songs.json', test_zip.namelist()) + self.assertEqual(len(test_zip.namelist()), 6) + + # test content of the json files in the zip + these_participants = json.loads(test_zip.read('participants.json').decode("utf-8")) + self.assertEqual(len(these_participants), 1) + self.assertEqual(Participant.objects.first().unique_hash, '42') + + these_profiles = json.loads(test_zip.read('profiles.json').decode("utf-8")) + self.assertEqual(len(these_profiles), 5) + + these_results = json.loads(test_zip.read('results.json').decode("utf-8")) + self.assertEqual(len(these_results), 5) + + these_sections = json.loads(test_zip.read('sections.json').decode("utf-8")) + self.assertEqual(len(these_sections), 1000) + + these_sessions = json.loads(test_zip.read('sessions.json').decode("utf-8")) + + self.assertEqual(len(these_sessions), 1) + self.assertEqual(these_sessions[0]['fields']['experiment'], 14) + + these_songs = json.loads(test_zip.read('songs.json').decode("utf-8")) + self.assertEqual(len(these_songs), 100) + + # test response from forced download self.assertEqual(response.status_code, 200) self.assertEqual(response['content-type'], 'application/x-zip-compressed')