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

Commit

Permalink
Write test for and clean up previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vleong2332 committed Sep 8, 2016
1 parent 3d8b4b5 commit cd3b6a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
10 changes: 0 additions & 10 deletions td/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,29 @@ def post(self, request, *args, **kwargs):
try:
message = ""
data = request.POST if len(request.POST) else json.loads(request.body)
logger.warning("DATA: %s", data)
questionnaire = Questionnaire.objects.get(pk=data.get("questionnaire_id"))
field_mapping = questionnaire.field_mapping
logger.warning("FIELD MAPPING: %s", field_mapping)
answers = json.loads(data.get("answers")) if len(request.POST) else data.get("answers")
logger.warning("ANSWERS: %s", answers)

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

for answer in answers:
answer_list.append(answer)
logger.warning("PROCESSING ANSWER: %s", answer)
qid = str(answer.get("question_id"))
logger.warning("QID: %s", qid)
if qid is not None and qid in field_mapping:
logger.warning("QID IS IN FIELD MAPPING")
answer_text = answer.get("text")
answer_text_list.append(answer_text)
logger.warning("ANSWER TEXT: %s", answer_text)
if field_mapping[qid] == "country":
obj.country = Country.objects.get(name__iexact=answer_text)
obj_list.append(obj.country and obj.country.name)
logger.warning("OBJ.COUNTRY: %s", obj.country)
elif field_mapping[qid] == "direction":
obj.direction = "l" if answer_text.lower() == "yes" else "r"
obj_list.append(obj.direction)
logger.warning("OBJ.DIRECTION: %s", obj.direction)
else:
obj.__dict__[field_mapping[qid]] = answer_text
obj_list.append(obj.__dict__[field_mapping[qid]])
logger.warning("OBJ.whatever: %s", obj.__dict__[field_mapping[qid]])

obj.save()

Expand Down
27 changes: 10 additions & 17 deletions td/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import importlib
import re
import types

import requests

from mock import patch, Mock
Expand Down Expand Up @@ -154,29 +156,12 @@ def test_init(self):
self.assertIsInstance(self.view.questionnaire, Questionnaire)
self.assertEqual(self.view.questionnaire, self.questionnaire)

def test_get_form_list(self):
"""
get_form_list() should return an OrderedDict containing 3 forms. The last form should be TempLanguageForm
"""
# For some reason WizardView turns the form_list from list into an ordered dict. Since I cannot find where and
# when it does this, I manually convert the list into a dictionary before running get_form_list. If not, it
# will still be a list and will complain that 'list' has no property 'update'.
self.view.form_list = dict([(str(step), form) for step, form in enumerate(self.view.form_list)])
result = self.view.get_form_list()
self.assertEqual(len(result), 3)
self.assertIn("0", result)
self.assertIn("1", result)
self.assertIn("2", result)
self.assertIs(result["2"], TempLanguageForm)

@patch("td.views.TempLanguage.save")
@patch("td.views.redirect")
def test_done(self, mock_redirect, mock_save):
"""
done() should save TempLanguage and return a redirect
"""
# Look at the note in test_get_form_list()
self.view.form_list = dict([(str(step), form) for step, form in enumerate(self.view.form_list)])
mock_cleaned_data = {
"code": "tst",
"questionnaire": self.questionnaire,
Expand All @@ -186,6 +171,14 @@ def test_done(self, mock_redirect, mock_save):
self.assertEqual(mock_save.call_count, 1)
self.assertEqual(mock_redirect.call_count, 1)

def test_build_forms_from_questionnaire(self):
"""
_build_forms_from_questionnaire() should make form_list a dictionary with TempLanguageForm as the last form
"""
self.view._build_forms_from_questionnaire()
self.assertIsInstance(self.view.form_list, types.DictType)
self.assertIs(self.view.form_list.get(str(len(self.view.form_list) - 1)), TempLanguageForm)


class TempLanguageUpdateViewTestCase(TestCase):
def setUp(self):
Expand Down
12 changes: 11 additions & 1 deletion td/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import types

import requests
import uuid
import time
Expand Down Expand Up @@ -707,6 +709,15 @@ def done(self, form_list, **kwargs):
return redirect(obj.lang_assigned_url)

def _build_forms_from_questionnaire(self):
# Somewhere and sometime, WizardView turns self.form_list from a List to an OrderedDict. This check ensures that
# the form_list is converted to an OrderedDict if for some reason it is not. One example is during testing.
# For some reason, when this function is called during testing, form_list is still a list and the test fails
# because 'list' object has no attribute 'update'. But outside of testing, form_list is already converted to
# an OrderedDict and everything is fine. To see what I'm talking about, comment out this check and run the
# test.
if isinstance(self.form_list, types.ListType):
self.form_list = dict([(str(step), form) for step, form in enumerate(self.form_list)])

step = 0
for group in self.questionnaire.grouped_questions:
fields = {}
Expand All @@ -731,7 +742,6 @@ def _build_forms_from_questionnaire(self):
self.form_list.update({unicode(step): TempLanguageForm})



class TempLanguageUpdateView(LoginRequiredMixin, UpdateView):
model = TempLanguage
form_class = TempLanguageForm
Expand Down

0 comments on commit cd3b6a9

Please sign in to comment.