Changeset 6041
- Timestamp:
- 09/03/07 18:14:51 (1 year ago)
- Files:
-
- django/trunk/django/test/testcases.py (modified) (3 diffs)
- django/trunk/tests/regressiontests/test_client_regress/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/testcases.py
r6039 r6041 10 10 11 11 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) 12 13 def to_list(value): 14 """ 15 Puts value into a list if it's not already one. 16 Returns an empty list if value is None. 17 """ 18 if value is None: 19 value = [] 20 elif not isinstance(value, list): 21 value = [value] 22 return value 23 12 24 13 25 class OutputChecker(doctest.OutputChecker): … … 107 119 def assertFormError(self, response, form, field, errors): 108 120 "Assert that a form used to render the response has a specific field error" 109 if not response.context: 110 self.fail('Response did not use any contexts to render the response') 121 # Put context(s) into a list to simplify processing. 122 contexts = to_list(response.context) 123 if not contexts: 124 self.fail('Response did not use any contexts to render the' 125 ' response') 111 126 112 # If there is a single context, put it into a list to simplify processing 113 if not isinstance(response.context, list): 114 contexts = [response.context] 115 else: 116 contexts = response.context 117 118 # If a single error string is provided, make it a list to simplify processing 119 if not isinstance(errors, list): 120 errors = [errors] 127 # Put error(s) into a list to simplify processing. 128 errors = to_list(errors) 121 129 122 130 # Search all contexts for the error. … … 145 153 def assertTemplateUsed(self, response, template_name): 146 154 "Assert that the template with the provided name was used in rendering the response" 147 if isinstance(response.template, list): 148 template_names = [t.name for t in response.template] 149 self.failUnless(template_name in template_names, 150 u"Template '%s' was not one of the templates used to render the response. Templates used: %s" % 151 (template_name, u', '.join(template_names))) 152 elif response.template: 153 self.assertEqual(template_name, response.template.name, 154 u"Template '%s' was not used to render the response. Actual template was '%s'" % 155 (template_name, response.template.name)) 156 else: 155 template_names = [t.name for t in to_list(response.template)] 156 if not template_names: 157 157 self.fail('No templates used to render the response') 158 self.failUnless(template_name in template_names, 159 (u"Template '%s' was not a template used to render the response." 160 " Actual template(s) used: %s") % (template_name, 161 u', '.join(template_names))) 158 162 159 163 def assertTemplateNotUsed(self, response, template_name): 160 164 "Assert that the template with the provided name was NOT used in rendering the response" 161 if isinstance(response.template, list): 162 self.failIf(template_name in [t.name for t in response.template], 163 u"Template '%s' was used unexpectedly in rendering the response" % template_name) 164 elif response.template: 165 self.assertNotEqual(template_name, response.template.name, 166 u"Template '%s' was used unexpectedly in rendering the response" % template_name) 167 165 template_names = [t.name for t in to_list(response.template)] 166 self.failIf(template_name in template_names, 167 (u"Template '%s' was used unexpectedly in rendering the" 168 " response") % template_name) django/trunk/tests/regressiontests/test_client_regress/models.py
r6039 r6041 76 76 self.assertTemplateUsed(response, 'Empty POST Template') 77 77 except AssertionError, e: 78 self.assertEquals(str(e), "Template 'Empty POST Template' was not used to render the response. Actual template was 'Empty GET Template'")78 self.assertEquals(str(e), "Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template") 79 79 80 80 def test_multiple_context(self): … … 102 102 self.assertTemplateUsed(response, "Valid POST Template") 103 103 except AssertionError, e: 104 self.assertEquals(str(e), "Template 'Valid POST Template' was not one of the templates used to render the response. Templatesused: form_view.html, base.html")104 self.assertEquals(str(e), "Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html") 105 105 106 106 class AssertRedirectsTests(TestCase):
