Commits

Mikhail Korobov  committed 99bf4ce

Support for assertRedirects and assertContains is added. Fix #5.

  • Participants
  • Parent commits a12ab28

Comments (0)

Files changed (4)

File django_webtest/__init__.py

 from webtest import TestApp
 
 from django_webtest.middleware import DjangoWsgiFix
+from django_webtest.response import DjangoWebtestResponse
+
 
 class DjangoTestApp(TestApp):
 
         elif data.get('templates'):
             response.template = flattend('templates')
 
+        response.__class__ = DjangoWebtestResponse
         return response
 
     def get(self, url, params=None, headers=None, extra_environ=None,

File django_webtest/response.py

+# -*- coding: utf-8 -*-
+import urlparse
+from webtest import TestResponse
+from django.test import Client
+
+class DjangoWebtestResponse(TestResponse):
+    """
+    WebOb's Response quacking more like django's HttpResponse.
+
+    This is here to make more django's TestCase asserts work,
+    not to provide a generally useful proxy.
+    """
+    @property
+    def status_code(self):
+        return self.status_int
+
+    @property
+    def _charset(self):
+        return self.charset
+
+    @property
+    def content(self):
+        return self.body
+
+    @property
+    def client(self):
+        return Client()
+
+    def __getitem__(self, item):
+        if item != 'Location':
+            raise TypeError('Keys other than "Location" are unsupported')
+
+        # django's test response returns location as http://testserver/,
+        # WebTest returns it as http://localhost:80/
+        e_scheme, e_netloc, e_path, e_query, e_fragment = urlparse.urlsplit(self.location)
+        if e_netloc == 'localhost:80':
+            e_netloc = 'testserver'
+        return urlparse.urlunsplit([e_scheme, e_netloc, e_path, e_query, e_fragment])

File django_webtest_tests/templates/index.html

 {% endif %}
 
 <p>Do we want spam? {{ spam|yesno }}</p>
+<p>привет-привет.</p>

File django_webtest_tests/testapp_tests/tests.py

         page.form['password'] = 'bar'
         page_with_errors = page.form.submit()
 
-        self.assertFormError(page_with_errors, 'form', 'password', 'Incorrect password.')
+        assert 'Incorrect password' in page_with_errors
 
         page_with_errors.form['password'] = 'foo'
         page_with_errors.form.submit().follow() # check for 302 response
         self.assertEqual(response.context['bar'], True)
         self.assertEqual(response.context['spam'], None)
 
-    def test_django_assert_api(self):
-        response = self.app.get('/template/index.html')
-        self.assertTemplateUsed(response, 'index.html')
-        self.assertTemplateNotUsed(response, 'complex.html')
-
-        complex_response = self.app.get('/template/complex.html')
-        self.assertTemplateUsed(complex_response, 'complex.html')
-        self.assertTemplateUsed(complex_response, 'include.html')
-        self.assertTemplateNotUsed(complex_response, 'foo.html')
-
 
 class AuthTest(WebTest):
     def setUp(self):
         self.assertEqual(user, self.user)
 
 
+class DjangoAssertsTest(WebTest):
+
+    def test_assert_template_used(self):
+        response = self.app.get('/template/index.html')
+        self.assertTemplateUsed(response, 'index.html')
+        self.assertTemplateNotUsed(response, 'complex.html')
+
+        complex_response = self.app.get('/template/complex.html')
+        self.assertTemplateUsed(complex_response, 'complex.html')
+        self.assertTemplateUsed(complex_response, 'include.html')
+        self.assertTemplateNotUsed(complex_response, 'foo.html')
+
+    def test_assert_form_error(self):
+        page = self.app.get(reverse('check_password'))
+        page.form['password'] = 'bar'
+        page_with_errors = page.form.submit()
+        self.assertFormError(page_with_errors, 'form', 'password', 'Incorrect password.')
+
+    def test_assert_contains(self):
+        response = self.app.get('/template/index.html')
+        self.assertContains(response, 'Hello', 1)
+        self.assertNotContains(response, 'Good bye!')
+
+    def test_assert_contains_unicode(self):
+        response = self.app.get('/template/index.html')
+        self.assertContains(response, u'привет', 2)
+
+    def test_assert_redirects(self):
+        page = self.app.get(reverse('check_password'))
+        page.form['password'] = 'foo'
+        resp = page.form.submit()
+        self.assertRedirects(resp, '/')
+
 
 class DisableAuthSetupTest(WebTest):
     setup_auth = False