Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit cd3b6a9

Browse files
committed
Write test for and clean up previous commit
1 parent 3d8b4b5 commit cd3b6a9

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

td/api/views.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,29 @@ def post(self, request, *args, **kwargs):
4949
try:
5050
message = ""
5151
data = request.POST if len(request.POST) else json.loads(request.body)
52-
logger.warning("DATA: %s", data)
5352
questionnaire = Questionnaire.objects.get(pk=data.get("questionnaire_id"))
5453
field_mapping = questionnaire.field_mapping
55-
logger.warning("FIELD MAPPING: %s", field_mapping)
5654
answers = json.loads(data.get("answers")) if len(request.POST) else data.get("answers")
57-
logger.warning("ANSWERS: %s", answers)
5855

5956
obj = TempLanguage(code=data.get("temp_code"), questionnaire=questionnaire, app=data.get("app"),
6057
request_id=data.get("request_id"), requester=data.get("requester"),
6158
answers=answers)
6259

6360
for answer in answers:
6461
answer_list.append(answer)
65-
logger.warning("PROCESSING ANSWER: %s", answer)
6662
qid = str(answer.get("question_id"))
67-
logger.warning("QID: %s", qid)
6863
if qid is not None and qid in field_mapping:
69-
logger.warning("QID IS IN FIELD MAPPING")
7064
answer_text = answer.get("text")
7165
answer_text_list.append(answer_text)
72-
logger.warning("ANSWER TEXT: %s", answer_text)
7366
if field_mapping[qid] == "country":
7467
obj.country = Country.objects.get(name__iexact=answer_text)
7568
obj_list.append(obj.country and obj.country.name)
76-
logger.warning("OBJ.COUNTRY: %s", obj.country)
7769
elif field_mapping[qid] == "direction":
7870
obj.direction = "l" if answer_text.lower() == "yes" else "r"
7971
obj_list.append(obj.direction)
80-
logger.warning("OBJ.DIRECTION: %s", obj.direction)
8172
else:
8273
obj.__dict__[field_mapping[qid]] = answer_text
8374
obj_list.append(obj.__dict__[field_mapping[qid]])
84-
logger.warning("OBJ.whatever: %s", obj.__dict__[field_mapping[qid]])
8575

8676
obj.save()
8777

td/tests/test_views.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import importlib
22
import re
3+
import types
4+
35
import requests
46

57
from mock import patch, Mock
@@ -154,29 +156,12 @@ def test_init(self):
154156
self.assertIsInstance(self.view.questionnaire, Questionnaire)
155157
self.assertEqual(self.view.questionnaire, self.questionnaire)
156158

157-
def test_get_form_list(self):
158-
"""
159-
get_form_list() should return an OrderedDict containing 3 forms. The last form should be TempLanguageForm
160-
"""
161-
# For some reason WizardView turns the form_list from list into an ordered dict. Since I cannot find where and
162-
# when it does this, I manually convert the list into a dictionary before running get_form_list. If not, it
163-
# will still be a list and will complain that 'list' has no property 'update'.
164-
self.view.form_list = dict([(str(step), form) for step, form in enumerate(self.view.form_list)])
165-
result = self.view.get_form_list()
166-
self.assertEqual(len(result), 3)
167-
self.assertIn("0", result)
168-
self.assertIn("1", result)
169-
self.assertIn("2", result)
170-
self.assertIs(result["2"], TempLanguageForm)
171-
172159
@patch("td.views.TempLanguage.save")
173160
@patch("td.views.redirect")
174161
def test_done(self, mock_redirect, mock_save):
175162
"""
176163
done() should save TempLanguage and return a redirect
177164
"""
178-
# Look at the note in test_get_form_list()
179-
self.view.form_list = dict([(str(step), form) for step, form in enumerate(self.view.form_list)])
180165
mock_cleaned_data = {
181166
"code": "tst",
182167
"questionnaire": self.questionnaire,
@@ -186,6 +171,14 @@ def test_done(self, mock_redirect, mock_save):
186171
self.assertEqual(mock_save.call_count, 1)
187172
self.assertEqual(mock_redirect.call_count, 1)
188173

174+
def test_build_forms_from_questionnaire(self):
175+
"""
176+
_build_forms_from_questionnaire() should make form_list a dictionary with TempLanguageForm as the last form
177+
"""
178+
self.view._build_forms_from_questionnaire()
179+
self.assertIsInstance(self.view.form_list, types.DictType)
180+
self.assertIs(self.view.form_list.get(str(len(self.view.form_list) - 1)), TempLanguageForm)
181+
189182

190183
class TempLanguageUpdateViewTestCase(TestCase):
191184
def setUp(self):

td/views.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import types
2+
13
import requests
24
import uuid
35
import time
@@ -707,6 +709,15 @@ def done(self, form_list, **kwargs):
707709
return redirect(obj.lang_assigned_url)
708710

709711
def _build_forms_from_questionnaire(self):
712+
# Somewhere and sometime, WizardView turns self.form_list from a List to an OrderedDict. This check ensures that
713+
# the form_list is converted to an OrderedDict if for some reason it is not. One example is during testing.
714+
# For some reason, when this function is called during testing, form_list is still a list and the test fails
715+
# because 'list' object has no attribute 'update'. But outside of testing, form_list is already converted to
716+
# an OrderedDict and everything is fine. To see what I'm talking about, comment out this check and run the
717+
# test.
718+
if isinstance(self.form_list, types.ListType):
719+
self.form_list = dict([(str(step), form) for step, form in enumerate(self.form_list)])
720+
710721
step = 0
711722
for group in self.questionnaire.grouped_questions:
712723
fields = {}
@@ -731,7 +742,6 @@ def _build_forms_from_questionnaire(self):
731742
self.form_list.update({unicode(step): TempLanguageForm})
732743

733744

734-
735745
class TempLanguageUpdateView(LoginRequiredMixin, UpdateView):
736746
model = TempLanguage
737747
form_class = TempLanguageForm

0 commit comments

Comments
 (0)