Skip to content

Commit

Permalink
add tests for json files inside zip
Browse files Browse the repository at this point in the history
  • Loading branch information
Evert-R committed Dec 14, 2023
1 parent c3c234d commit c00c574
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend/experiment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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'
Expand Down
78 changes: 72 additions & 6 deletions backend/experiment/tests/test_admin_experiment.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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]
Expand All @@ -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')

0 comments on commit c00c574

Please sign in to comment.