Beispiel #1
0
	def testGetSingleFieldAnswers(self):
		submission = Submission.objects.get(slug="testSubmission")

		# Test get_answers when given a single field string
		lang_answer = forms.get_answers(submission, field="other-languages")
		bio_answer = forms.get_answers(submission, field="biography")

		self.assertEqual(len(lang_answer), 1, "Too many answers returned! Got: %s, should have: 1." % len(lang_answer))
		self.assertEqual(lang_answer['other-languages'], u'\u2600')

		self.assertEqual(len(bio_answer), 1, "Too many answers returned! Got: %s, should have: 1." % len(bio_answer))
		self.assertEqual(bio_answer['biography'], u'Blah blah blah\u2600')		
Beispiel #2
0
	def testGetMultipleFieldAnswers(self):
		submission = Submission.objects.get(slug="testSubmission")
		
		# Test get_answers when given multiple field slugs
		answers = forms.get_answers(submission=submission)

		self.assertEqual(answers['other-languages'], u'\u2600')
		self.assertEqual(answers['biography'], u'Blah blah blah\u2600')
Beispiel #3
0
    def testGetMultipleFieldAnswers(self):
        submission = Submission.objects.get(slug="testSubmission")

        # Test get_answers when given multiple field slugs
        answers = forms.get_answers(submission=submission)

        self.assertEqual(answers['other-languages'], u'\u2600')
        self.assertEqual(answers['biography'], u'Blah blah blah\u2600')
Beispiel #4
0
	def testGetAnswers(self):
		# Test getting answers the submission from the tests fixture using a string arg
		answers = forms.get_answers(submission="testSubmission")

		# Just make sure it's not empty.
		# FIXME: actually compare data against `tests` fixture
		# FIXME: should verify that answers come back WITHOUT form name prepended
		# (ie. default argument to get_answers of for_form=False) 
		self.assertTrue(answers)
		
		# Test the submission from the tests fixture using a Submission object arg
		submission = Submission.objects.get(slug="testSubmission")
		answers = forms.get_answers(submission=submission, for_form=True)
		
		# Just make sure it's not empty (could do more I guess)
		# FIXME: should verify that answers come back WITH form name prepended
		# (ie. argument to get_answers of for_form=True) 
		self.assertTrue(answers)
Beispiel #5
0
    def testGetSingleFieldAnswers(self):
        submission = Submission.objects.get(slug="testSubmission")

        # Test get_answers when given a single field string
        lang_answer = forms.get_answers(submission, field="other-languages")
        bio_answer = forms.get_answers(submission, field="biography")

        self.assertEqual(
            len(lang_answer), 1,
            "Too many answers returned! Got: %s, should have: 1." %
            len(lang_answer))
        self.assertEqual(lang_answer['other-languages'], u'\u2600')

        self.assertEqual(
            len(bio_answer), 1,
            "Too many answers returned! Got: %s, should have: 1." %
            len(bio_answer))
        self.assertEqual(bio_answer['biography'], u'Blah blah blah\u2600')
Beispiel #6
0
    def testGetAnswers(self):
        # Test getting answers the submission from the tests fixture using a string arg
        answers = forms.get_answers(submission="testSubmission")

        # Just make sure it's not empty.
        # FIXME: actually compare data against `tests` fixture
        # FIXME: should verify that answers come back WITHOUT form name prepended
        # (ie. default argument to get_answers of for_form=False)
        self.assertTrue(answers)

        # Test the submission from the tests fixture using a Submission object arg
        submission = Submission.objects.get(slug="testSubmission")
        answers = forms.get_answers(submission=submission, for_form=True)

        # Just make sure it's not empty (could do more I guess)
        # FIXME: should verify that answers come back WITH form name prepended
        # (ie. argument to get_answers of for_form=True)
        self.assertTrue(answers)
def get_upload_url(request, submission):
	files = forms.get_answers(submission=submission, for_form=True, field=request.FILES.keys()[0])
	return os.path.join(settings.MEDIA_URL, files[request.FILES.keys()[0]])
Beispiel #8
0
    def assertValidSave(self, data, submission):
        """
		Asserts that data from a POST request was saved correctly in the DB.
		We can't compare them directly because we need to mash the DB data
		back into a form that is comparable to request.POST data.
		
		:param data: request.POST data
		:param submission: a Submission object or slug
		"""

        # Slightly evil, do type checking to see if submission is a Submission object or string
        if isinstance(submission, str):
            submission = Submission.objects.get(slug=submission)
        answers_from_db = get_answers(submission=submission, for_form=True)

        # ------ 1 ------
        # This fixes the fact that a POST request will not have a checkbox key,
        # but the DB _may_ contain a blank string for a checkbox False value
        # and _will_ contain a '1' for a checkbox True value.

        # Get the boolean field answers
        answers = Answer.objects.select_related('field').filter(
            submission=submission, field__field_type__in=BOOLEAN_FIELDS)
        boolean_field_names = [(answer.data_form.slug, answer.field.slug)
                               for answer in answers]

        # For boolean fields that aren't checked, remove these from the DB answers because
        # they will not exist in the form POST
        for form_name, field_name in boolean_field_names:
            key = _field_for_form(name=field_name, form=form_name)
            if data.has_key(key) and not data[key]:
                # Checkbox was not checked, delete element containing *blank string* in DB returned answers
                # to make these "equal" for testing
                del answers_from_db[key]
            elif data.has_key(key) and data[key]:
                # Checkbox was checked, so DB has "1" and post has "[1]" ... make these "equal" for testing
                answers_from_db[key] = [answers_from_db[key]]

        # ------ 2 ------
        # This fixes the fact that each request.POST element will be a list
        # and that won't be true from get_answers

        # Get all Answers that won't be lists
        # FIXME: excluding upload fields here. Once testing is implemented, remove + UPLOAD_FIELDS
        answers = Answer.objects.select_related('field').filter(
            submission=submission).exclude(
                Q(field__field_type__in=MULTI_CHOICE_FIELDS + BOOLEAN_FIELDS +
                  UPLOAD_FIELDS))

        field_names = [(answer.data_form.slug, answer.field.slug)
                       for answer in answers]

        # Wrap them as lists
        for form_name, field_name in field_names:
            form_field_name = _field_for_form(name=field_name, form=form_name)
            try:
                # Delete blank answers, we assume they are not in the post.
                if not answers_from_db[form_field_name]:
                    del answers_from_db[form_field_name]
                else:
                    answers_from_db[form_field_name] = [
                        answers_from_db[form_field_name]
                    ]
            except KeyError:
                self.fail(
                    "It looks like get_answers() (or saving) might be borked. '%s'"
                    " was supposed to exist in answers_from_db, but doesn't. Perhaps a certain"
                    " storage mechanism in the save() function is not working properly? Or you messed"
                    " with the fields and haven't updated TEST_FORM_POST_DATA?"
                    % form_field_name)

        # ------ 3 ------
        # Actually compare the submitted data to the DB data
        self.assertDictionaryEqual(from_post=data, from_db=answers_from_db)
	def assertValidSave(self, data, submission):
		"""
		Asserts that data from a POST request was saved correctly in the DB.
		We can't compare them directly because we need to mash the DB data
		back into a form that is comparable to request.POST data.
		
		:param data: request.POST data
		:param submission: a Submission object or slug
		"""
		
		# Slightly evil, do type checking to see if submission is a Submission object or string
		if isinstance(submission, str):
			submission = Submission.objects.get(slug=submission)
		answers_from_db = get_answers(submission=submission, for_form=True)
		
		# ------ 1 ------
		# This fixes the fact that a POST request will not have a checkbox key,
		# but the DB _may_ contain a blank string for a checkbox False value
		# and _will_ contain a '1' for a checkbox True value.
				
		# Get the boolean field answers
		answers = Answer.objects.select_related('field').filter(
			submission=submission,
			field__field_type__in=BOOLEAN_FIELDS
		)
		boolean_field_names = [(answer.data_form.slug, answer.field.slug) for answer in answers]
		
		# For boolean fields that aren't checked, remove these from the DB answers because
		# they will not exist in the form POST
		for form_name, field_name in boolean_field_names:
			key = _field_for_form(name=field_name, form=form_name)
			if data.has_key(key) and not data[key]:
				# Checkbox was not checked, delete element containing *blank string* in DB returned answers
				# to make these "equal" for testing
				del answers_from_db[key]
			elif data.has_key(key) and data[key]:
				# Checkbox was checked, so DB has "1" and post has "[1]" ... make these "equal" for testing
				answers_from_db[key] = [answers_from_db[key]]
		
		# ------ 2 ------
		# This fixes the fact that each request.POST element will be a list
		# and that won't be true from get_answers
		
		# Get all Answers that won't be lists
		# FIXME: excluding upload fields here. Once testing is implemented, remove +UPLOAD_FIELDS
		answers = Answer.objects.select_related('field').filter(
			submission=submission
		).exclude(Q(field__field_type__in=MULTI_CHOICE_FIELDS+BOOLEAN_FIELDS+UPLOAD_FIELDS))
		
		field_names = [(answer.data_form.slug, answer.field.slug) for answer in answers]
		
		# Wrap them as lists
		for form_name, field_name in field_names:
			form_field_name = _field_for_form(name=field_name, form=form_name)
			try:
				answers_from_db[form_field_name] = [answers_from_db[form_field_name]]
			except KeyError:
				self.fail("It looks like get_answers() (or saving) might be borked. '%s' was supposed to exist in answers_from_db, but doesn't. Perhaps a certain storage mechanism in the save() function is not working properly? Or you messed with the fields and haven't updated TEST_FORM_POST_DATA?" % form_field_name)
		
		# ------ 3 ------
		# Actually compare the submitted data to the DB data
		self.assertDictionaryEqual(from_post=data, from_db=answers_from_db)