Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Ordered recent answers #25

Merged
merged 3 commits into from

1 participant

@ctrlspc
Owner

You can now get the most recent question_answers back in the order that the questions were defined int he question group

@ctrlspc ctrlspc merged commit 50c8532 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 20, 2012
  1. @ctrlspc

    added stub test and function for

    ctrlspc authored
    get_latest_question_answer_in_order
Commits on Sep 21, 2012
  1. @ctrlspc
  2. @ctrlspc

    brought the branch up to date with master

    ctrlspc authored
    as it was originally branched off an out of date master branch
This page is out of date. Refresh to see the latest.
View
21 questionnaire/fixtures/test_questionnaire_fixtures_formodels.json
@@ -20,6 +20,27 @@
"model" : "questionnaire.question",
"pk" : 3
},
+ { "fields" : { "field_type" : "booleanfield",
+ "label" : "question_test_booleanfield",
+ "selectoptions" : null
+ },
+ "model" : "questionnaire.question",
+ "pk" : 4
+ },
+ { "fields" : { "field_type" : "booleanfield",
+ "label" : "question_test_booleanfield",
+ "selectoptions" : null
+ },
+ "model" : "questionnaire.question",
+ "pk" : 5
+ },
+ { "fields" : { "field_type" : "booleanfield",
+ "label" : "question_test_booleanfield",
+ "selectoptions" : null
+ },
+ "model" : "questionnaire.question",
+ "pk" : 6
+ },
{ "fields" : { "field_type" : "radioselectfield",
"label" : "question_test_radioselectfield",
"selectoptions" : "Radio 1, Radio 2, Radio 3"
View
20 questionnaire/models.py
@@ -274,7 +274,24 @@ def get_latest_question_answers(self):
have more than one QuestionAnswer for each question in a given answer set).
'''
return [record.question_answer for record in LatestQuestionAnswer.objects.filter(answer_set=self)]
-
+
+ def get_latest_question_answer_in_order(self):
+ '''
+ This function will return a list of QuestionAnswer objects in th same order that the questions are defined in
+ the group. Where an answer is not present for a question it will simply be missing from the list
+ and the sequence will move up
+ '''
+
+ answer_dict = {record.question:record for record in self.get_latest_question_answers()}
+
+ ordered_answers = []
+
+ for question in self.questiongroup.get_ordered_questions():
+ if question in answer_dict:
+ ordered_answers.append(answer_dict[question])
+
+ return ordered_answers
+
def is_complete(self):
'''
This function will return True is there is an answer for each of the
@@ -295,6 +312,7 @@ def is_complete(self):
return True
+
class QuestionAnswer(models.Model):
'''
This model stores questions, answers and related answer_set
View
47 questionnaire/tests/models.py
@@ -433,6 +433,53 @@ def test_get_latest_question_answers(self):
objects_patch.filter.assert_called_once_with(answer_set=test_answer_set)
self.assertEqual(answers, ['questionAnswer1','questionAnswer2'])
+
+ def test_get_latest_question_answer_in_order(self):
+ '''
+ This function will return the latest question answers for this answerset,
+ in the correct order as defined in the question ordering in the group.
+
+ If a questiongroup has questions 1-6 ordered the same as their primary key and
+ the get_latest_question_answer function returns the questionAnsers in
+ order 4,8,1 then we should expect this function to return 1,4,8.
+ However we want to make sure that this function orders the groups by the order_info
+ and not by the primary key of the questions. So we will define a new question group that has 4
+ questions:
+ 1 : id:4
+ 2 : id:2
+ 3 : id:6
+ 4 : id:1
+
+ and we will mock the get_latest_question_answer function to return ids 1,2,4 and we should expect
+ 4,2,1 to be returned.
+ '''
+ my_question_group = QuestionGroup.objects.create(name='test_qg')
+ my_questionnaire = Questionnaire.objects.create(name='test_Q')
+ my_questionnaire.add_question_group(my_question_group)
+ user = User.objects.create_user('username', 'email@me.com', 'password')
+ q4 = Question.objects.get(id=4)
+ q2 = Question.objects.get(id=2)
+ q6 = Question.objects.get(id=6)
+ q1 = Question.objects.get(id=1)
+
+ Question_order.objects.create(questiongroup=my_question_group, question=q4, order_info=1)
+ Question_order.objects.create(questiongroup=my_question_group, question=q2, order_info=2)
+ Question_order.objects.create(questiongroup=my_question_group, question=q6, order_info=3)
+ Question_order.objects.create(questiongroup=my_question_group, question=q1, order_info=4)
+
+ test_answer_set = AnswerSet.objects.create(questionnaire=my_questionnaire,
+ questiongroup=my_question_group,
+ user = user)
+ qa4 = QuestionAnswer.objects.create(question = q4, answer='yes', answer_set=test_answer_set)
+ qa2 = QuestionAnswer.objects.create(question = q2, answer='yes', answer_set=test_answer_set)
+ qa1 = QuestionAnswer.objects.create(question = q1, answer='yes', answer_set=test_answer_set)
+
+
+ with patch('questionnaire.models.AnswerSet.get_latest_question_answers') as mocked_function:
+ mocked_function.return_value = [qa1,qa2,qa4]
+
+ self.assertEqual(test_answer_set.get_latest_question_answer_in_order(),[qa4,qa2,qa1])
+ mocked_function.assert_called_once_with()
def fabricate_question_answer(self, question_id, answer_set, response):
Something went wrong with that request. Please try again.