def test_submit_response_to_questions(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id,)) user = self._login() # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) Question.objects.create( survey=survey, question={ 'question': 'Gender?', 'choices': ['Male', 'Female', 'Mixed'] }, order=next_question_order(), ) response = self.client.post(url, { str(question.id): "Green", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter( question=question, user=user ) eq_(answers.count(), 1)
def test_basic_save(self): survey = Survey.objects.create(name='Basic survey') event = Event.objects.get(title='Test event') survey.events.add(event) question = Question.objects.create( survey=survey, question={'some': 'json object'}, order=next_question_order(), ) eq_(question.order, 1) ok_(question.modified) # test creating a second question to make sure the increment # works as expected second_question = Question.objects.create( survey=survey, question={'some': 'json object'}, order=next_question_order(), ) eq_(second_question.order, 2) # the model should have a predetermined order eq_(list(Question.objects.all().values_list('order', flat=True)), [1, 2]) user, = User.objects.all() answer = Answer.objects.create(question=question, user=user, answer={'some': 'other json object'}) ok_(answer.modified)
def test_submit_response_to_questions(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id, )) user = self._login() # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) Question.objects.create( survey=survey, question={ 'question': 'Gender?', 'choices': ['Male', 'Female', 'Mixed'] }, order=next_question_order(), ) response = self.client.post( url, { str(question.id): "Green", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter(question=question, user=user) eq_(answers.count(), 1)
def test_event_survey(self): survey = Survey.objects.create( name='My Survey', active=True ) Question.objects.create( survey=survey, question={}, order=next_question_order(), ) other_survey = Survey.objects.create( name='Other Survey', active=False ) Question.objects.create( survey=other_survey, question={"question": "Something?"}, order=next_question_order(), ) Question.objects.create( survey=other_survey, question={"question": "Something else?"}, order=next_question_order(), ) event = Event.objects.get(title='Test event') event_edit_url = reverse('manage:event_edit', args=(event.id,)) response = self.client.get(event_edit_url) eq_(response.status_code, 200) url = reverse('manage:event_survey', args=(event.id,)) ok_(url in response.content) response = self.client.get(url) eq_(response.status_code, 200) ok_('My Survey' in response.content) ok_('1 question' in response.content) ok_('Other Survey' in response.content) ok_('2 questions' in response.content) ok_('none' in response.content) eq_(Survey.events.through.objects.filter(event=event).count(), 0) response = self.client.post(url, {'survey': survey.id}) eq_(response.status_code, 302) self.assertRedirects(response, event_edit_url) eq_(Survey.events.through.objects.filter(event=event).count(), 1) response = self.client.get(url) eq_(response.status_code, 200) # change it back to none response = self.client.post(url, {'survey': 0}) eq_(response.status_code, 302) self.assertRedirects(response, event_edit_url) eq_(Survey.events.through.objects.filter(event=event).count(), 0)
def test_edit_survey(self): survey = Survey.objects.create(name='Name') url = reverse('manage:survey_edit', args=(survey.id,)) response = self.client.get(url) eq_(response.status_code, 200) ok_('value="Name"' in response.content) # check for error trying to activate with no questions response = self.client.post(url, { 'active': True }) eq_(response.status_code, 200) ok_("Survey must have at least one question in order to be active" in response.content ) # add a question and check for successful activation Question.objects.create( survey=survey, question={}, order=next_question_order(), ) response = self.client.post(url, { 'name': 'New Name', 'active': True }) eq_(response.status_code, 302) self.assertRedirects( response, reverse('manage:surveys') ) survey = Survey.objects.get(id=survey.id) eq_(survey.name, 'New Name') ok_(survey.active)
def survey_question_new(request, id): survey = get_object_or_404(Survey, id=id) Question.objects.create( survey=survey, order=next_question_order(), ) return redirect('manage:survey_questions', survey.id)
def test_ordering_questions(self): survey = Survey.objects.create(name='Name') question_1 = Question.objects.create( survey=survey, question={'one': 1}, order=next_question_order(), ) question_2 = Question.objects.create( survey=survey, question={'two': 2}, order=next_question_order(), ) question_3 = Question.objects.create( survey=survey, question={'three': 3}, order=next_question_order(), ) questions = list(Question.objects.filter(survey=survey)) eq_(questions, [question_1, question_2, question_3]) # let's move question_2 up one url = reverse( 'manage:survey_question_edit', args=(survey.id, question_2.id) ) response = self.client.post(url, {'ordering': 'up'}) survey_questions_url = reverse( 'manage:survey_questions', args=(survey.id,) ) self.assertRedirects(response, survey_questions_url) questions = list(Question.objects.filter(survey=survey)) eq_(questions, [question_2, question_1, question_3]) # let's move question_1 down one url = reverse( 'manage:survey_question_edit', args=(survey.id, question_1.id) ) response = self.client.post(url, {'ordering': 'down'}) self.assertRedirects(response, survey_questions_url) questions = list(Question.objects.filter(survey=survey)) eq_(questions, [question_2, question_3, question_1])
def test_basic_save(self): survey = Survey.objects.create(name='Basic survey') event = Event.objects.get(title='Test event') survey.events.add(event) question = Question.objects.create( survey=survey, question={ 'some': 'json object' }, order=next_question_order(), ) eq_(question.order, 1) ok_(question.modified) # test creating a second question to make sure the increment # works as expected second_question = Question.objects.create( survey=survey, question={ 'some': 'json object' }, order=next_question_order(), ) eq_(second_question.order, 2) # the model should have a predetermined order eq_( list(Question.objects.all().values_list('order', flat=True)), [1, 2] ) user, = User.objects.all() answer = Answer.objects.create( question=question, user=user, answer={ 'some': 'other json object' } ) ok_(answer.modified)
def test_render_questions(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id,)) # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) # empty questions are ignored Question.objects.create( survey=survey, question={}, order=next_question_order(), ) # render the questions response = self.client.get(url) eq_(response.status_code, 200) ok_('type="submit"' not in response.content) self._login() response = self.client.get(url) eq_(response.status_code, 200) ok_('csrfmiddlewaretoken' in response.content) ok_('type="submit"' in response.content) # three choices eq_(response.content.count('name="%s"' % question.id), 3) ok_('Fav color?' in response.content) ok_('Red' in response.content) ok_('Green' in response.content) ok_('Blue' in response.content)
def test_render_questions(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id, )) # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) # empty questions are ignored Question.objects.create( survey=survey, question={}, order=next_question_order(), ) # render the questions response = self.client.get(url) eq_(response.status_code, 200) ok_('type="submit"' not in response.content) self._login() response = self.client.get(url) eq_(response.status_code, 200) ok_('csrfmiddlewaretoken' in response.content) ok_('type="submit"' in response.content) # three choices eq_(response.content.count('name="%s"' % question.id), 3) ok_('Fav color?' in response.content) ok_('Red' in response.content) ok_('Green' in response.content) ok_('Blue' in response.content)
def test_edit_question(self): survey = Survey.objects.create(name='Name') question = Question.objects.create( survey=survey, order=next_question_order(), ) url = reverse('manage:survey_question_edit', args=(survey.id, question.id)) q = {'question': '?', 'choices': ['a', 'b']} payload = json.dumps(q) response = self.client.post(url, {'question': payload}) eq_(response.status_code, 200) eq_(json.loads(response.content), {'question': json.dumps(q, indent=2)}) # reload question = Question.objects.get(id=question.id) eq_(question.question, q)
def test_list_surveys(self): survey = Survey.objects.create(name='My Survey', active=True) for i in range(3): Question.objects.create( survey=survey, question={}, order=next_question_order(), ) event = Event.objects.get(title='Test event') survey.events.add(event) url = reverse('manage:surveys') response = self.client.get(url) eq_(response.status_code, 200) ok_('My Survey' in response.content) ok_('>3</td>' in response.content) ok_('>1</td>' in response.content) ok_("Yes, it's active" in response.content)
def test_submit_multiple_times(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id,)) user = self._login() # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) response = self.client.post(url, { str(question.id): "Green", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter( question=question, user=user ) eq_(answers.count(), 1) answer, = answers eq_(answer.answer['answer'], 'Green') # so far so good # now let's try to submit a different answer response = self.client.post(url, { str(question.id): "Red", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter( question=question, user=user ) eq_(answers.count(), 1) answer, = answers eq_(answer.answer['answer'], 'Red')
def test_submit_multiple_times(self): survey = self._create_survey() url = reverse('surveys:load', args=(survey.id,)) user = self._login() # add a question question = Question.objects.create( survey=survey, question={ 'question': 'Fav color?', 'choices': ['Red', 'Green', 'Blue'] }, order=next_question_order(), ) response = self.client.post(url, { str(question.id): "Green", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter( question=question, user=user ) eq_(answers.count(), 1) answer, = answers eq_(answer.answer['answer'], 'Green') # so far so good # now let's try to submit a different answer response = self.client.post(url, { str(question.id): "Red", # note that we don't submit an answer to the second question }) eq_(response.status_code, 302) self.assertRedirects(response, url) answers = Answer.objects.filter( question=question, user=user ) eq_(answers.count(), 1) answer, = answers eq_(answer.answer['answer'], 'Red')
def test_list_surveys(self): survey = Survey.objects.create( name='My Survey', active=True ) for i in range(3): Question.objects.create( survey=survey, question={}, order=next_question_order(), ) event = Event.objects.get(title='Test event') survey.events.add(event) url = reverse('manage:surveys') response = self.client.get(url) eq_(response.status_code, 200) ok_('My Survey' in response.content) ok_('>3</td>' in response.content) ok_('>1</td>' in response.content) ok_("Yes, it's active" in response.content)
def test_edit_question(self): survey = Survey.objects.create(name='Name') question = Question.objects.create( survey=survey, order=next_question_order(), ) url = reverse( 'manage:survey_question_edit', args=(survey.id, question.id) ) q = { 'question': '?', 'choices': ['a', 'b'] } payload = json.dumps(q) response = self.client.post(url, {'question': payload}) eq_(response.status_code, 200) eq_( json.loads(response.content), {'question': json.dumps(q, indent=2)} ) # reload question = Question.objects.get(id=question.id) eq_(question.question, q)