Skip to content

Commit

Permalink
Fixed: Fix export_table method to export question_key and add unit te…
Browse files Browse the repository at this point in the history
…sts (#772)

* fix: Add question key to result choices in forms.py and models.py

* test: Add unit test for export_table method and whether it includes question_key

* chore: Add type annotations to export_table method and clean up imports
  • Loading branch information
drikusroor authored Feb 19, 2024
1 parent b1c398d commit 171611f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
5 changes: 3 additions & 2 deletions backend/experiment/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
('participant_access_info', 'Participant access info'),
('session_start', 'Session start time'),
('session_end', 'Session end time'),
('final_score', 'Final score')
('final_score', 'Final score'),
]

# result_keys for Export CSV
Expand All @@ -22,7 +22,8 @@
('result_score', 'Result score'),
('result_comment', 'Result comment'),
('expected_response', 'Expected response'),
('given_response', 'Given response')
('given_response', 'Given response'),
('question_key', 'Question key'),
]

# export_options for Export CSV
Expand Down
11 changes: 6 additions & 5 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

from django.db import models
from django.utils import timezone
from django.contrib.postgres.fields import ArrayField
from typing import List, Dict, Tuple, Any
from experiment.rules import EXPERIMENT_RULES
from experiment.standards.iso_languages import ISO_LANGUAGES

from django.contrib.postgres.fields import ArrayField
from .questions import QUESTIONS_CHOICES, get_default_question_keys
from django import forms

language_choices = [(key, ISO_LANGUAGES[key]) for key in ISO_LANGUAGES.keys()]
language_choices[0] = ('', 'Unset')
Expand Down Expand Up @@ -94,7 +93,7 @@ def export_sessions(self):
# export session objects
return self.session_set.all()

def export_table(self, session_keys, result_keys, export_options):
def export_table(self, session_keys: List[str], result_keys: List[str], export_options: Dict[str, Any]) -> Tuple[List[Dict[str, Any]], List[str]]:
"""Export filtered tabular data for admin
session_keys : session fieldnames to be included
result_keys : result fieldnames to be included
Expand Down Expand Up @@ -152,8 +151,10 @@ def export_table(self, session_keys, result_keys, export_options):
'result_score': result.score,
'result_comment': result.comment,
'expected_response': result.expected_response,
'given_response': result.given_response
'given_response': result.given_response,
'question_key': result.question_key,
}

result_data = {}
# Add counter for single row / wide format
if 'wide_format' in export_options:
Expand Down
20 changes: 18 additions & 2 deletions backend/experiment/tests/test_admin_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ def setUpTestData(cls):
Result.objects.create(
session=Session.objects.first(),
expected_response = i,
given_response = i
given_response = i,
question_key = 'test_question_' + str(i),
)
Result.objects.create(
participant=cls.participant,
question_key= i,
given_response = i
given_response = i,
)

def setUp(self):
Expand Down Expand Up @@ -132,3 +133,18 @@ def test_admin_export(self):
# test response from forced download
self.assertEqual(response.status_code, 200)
self.assertEqual(response['content-type'], 'application/x-zip-compressed')

def test_export_table_includes_question_key(self):
session_keys = ['session_start', 'session_end']
result_keys = ['question_key']
export_options = ['convert_result_json'] # Adjust based on your needs

# Call the method under test
rows, fieldnames = self.experiment.export_table(session_keys, result_keys, export_options)

# Assert that 'question_key' is in the fieldnames and check its value in rows
self.assertIn('question_key', fieldnames)
for i in range(len(rows)):
row = rows[i]
self.assertIn('question_key', row)
self.assertEqual(row['question_key'], 'test_question_' + str(i))

0 comments on commit 171611f

Please sign in to comment.