changeset 9:35178e70f1cd

urls support in testcase objects
author Victor Ng <victor@monkeybean.ca>
date Thu, 28 Aug 2008 13:33:58 -0400
parents a82369f2574e
children 3b9fe61973cf
files examples/README.TXT examples/project/settings.py examples/project/templates/500.html examples/project/tests/__init__.py examples/project/tests/test_views.py examples/project/zoo/test_urlconf.py examples/project/zoo/tests.py nosedjango/nosedjango.py
diffstat 8 files changed, 71 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/examples/README.TXT	Thu Aug 28 12:42:31 2008 -0400
+++ b/examples/README.TXT	Thu Aug 28 13:33:58 2008 -0400
@@ -27,7 +27,8 @@
 --- Sample test run below ---
 
 C:\dev\nosedjango\examples\project>nosetests -v --with-django --with-doctest --doctest-tests --doctest-tests
-doctest: project.zoo.models.zoo ... ok
+
+Doctest: project.zoo.models.Zoo ... ok
 Doctest: project.zoo.models.Zoo.__str__ ... ok
 Doctest: project.zoo.models.func ... ok
 This is just a stub for a regular test method ... ok
@@ -37,12 +38,15 @@
 project.zoo.test_fixtures.TestFixture2.test_count ... ok
 project.zoo.test_race.TestDBRace1.test1 ... ok
 project.zoo.test_race.TestDBRace2.test1 ... ok
-project.tests.test_views.test_view_index ... ok
+We're customizing the ROOT_URLCONF with zoo.urls, ... ok
+We're using the standard ROOT_URLCONF, so we need to ... ok
+testcase1 (project.zoo.tests.TestDjango) ... ok
+testcase2 (project.zoo.tests.TestDjango) ... ok
 
 ----------------------------------------------------------------------
-Ran 11 tests in 0.859s
+Ran 14 tests in 1.219s
 
 OK
 Destroying test database...
 
-C:\dev\nosedjango\examples\project>
+
--- a/examples/project/settings.py	Thu Aug 28 12:42:31 2008 -0400
+++ b/examples/project/settings.py	Thu Aug 28 13:33:58 2008 -0400
@@ -1,5 +1,8 @@
 # Django settings for 'project' project.
 
+import os
+PROJECT_PATH = os.path.abspath(os.path.split(__file__)[0])
+
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
 
@@ -10,7 +13,7 @@
 MANAGERS = ADMINS
 
 DATABASE_ENGINE = 'sqlite3'           # 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
-DATABASE_NAME = 'zoo.db'             # Or path to database file if using sqlite3.
+DATABASE_NAME = os.path.join(PROJECT_PATH, 'zoo.db')  # Or path to database file if using sqlite3.
 DATABASE_USER = ''             # Not used with sqlite3.
 DATABASE_PASSWORD = ''         # Not used with sqlite3.
 DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
@@ -67,6 +70,7 @@
 ROOT_URLCONF = 'project.urls'
 
 TEMPLATE_DIRS = (
+    os.path.join(PROJECT_PATH, 'templates'),
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
@@ -80,3 +84,5 @@
     'django.contrib.sites',
     'project.zoo'
 )
+handler500 = 'django.views.defaults.server_error'
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/project/templates/500.html	Thu Aug 28 13:33:58 2008 -0400
@@ -0,0 +1,1 @@
+blah.  HTTP 500 error
--- a/examples/project/tests/__init__.py	Thu Aug 28 12:42:31 2008 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-pass
--- a/examples/project/tests/test_views.py	Thu Aug 28 12:42:31 2008 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-from django.test.client import Client
-
-def test_view_index():
-    c = Client()
-    resp = c.get('/zoo/')
-    assert "Just a title" in resp.content
-    assert "foobar" in resp.content
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/project/zoo/test_urlconf.py	Thu Aug 28 13:33:58 2008 -0400
@@ -0,0 +1,38 @@
+from django.test import TestCase
+from django.test.client import Client
+
+class TestStandardUrlConf(TestCase):
+    def test_index(self):
+        '''
+        We're using the standard ROOT_URLCONF, so we need to
+        pass in /zoo/, just the empty string
+        '''
+        c = Client()
+        resp = c.get('')
+        assert resp.status_code == 500
+
+        c = Client()
+        resp = c.get('/zoo/')
+        assert "Just a title" in resp.content
+        assert "foobar" in resp.content
+
+class TestCustomUrlConf(TestCase):
+    urls = 'zoo.urls'
+
+    def test_index(self):
+        '''
+        We're customizing the ROOT_URLCONF with zoo.urls,
+        so we do *not* need to pass in /zoo/, just the empty string
+        '''
+        c = Client()
+        resp = c.get('')
+        assert "Just a title" in resp.content
+        assert "foobar" in resp.content
+
+        c = Client()
+        resp = c.get('/zoo/')
+        assert resp.status_code == 500
+
+
+
+
--- a/examples/project/zoo/tests.py	Thu Aug 28 12:42:31 2008 -0400
+++ b/examples/project/zoo/tests.py	Thu Aug 28 13:33:58 2008 -0400
@@ -5,8 +5,6 @@
     def testcase1(self):
         zoo = Zoo.objects.create(name='blah')
         assert Zoo.objects.count() == 1
-        import pdb
-        pdb.set_trace()
 
     def testcase2(self):
         zoo = Zoo.objects.create(name='blah')
--- a/nosedjango/nosedjango.py	Thu Aug 28 12:42:31 2008 -0400
+++ b/nosedjango/nosedjango.py	Thu Aug 28 13:33:58 2008 -0400
@@ -114,6 +114,7 @@
 
         from django.core.management import call_command
         from django.core.urlresolvers import clear_url_caches
+        from django.conf import settings
         call_command('flush', verbosity=0, interactive=False)
 
         if isinstance(test, nose.case.Test) and \
@@ -122,6 +123,16 @@
                 # We have to use this slightly awkward syntax due to the fact
                 # that we're using *args and **kwargs together.
                 call_command('loaddata', *test.context.fixtures, **{'verbosity': 0}) 
+
+        if isinstance(test, nose.case.Test) and \
+            isinstance(test.test, nose.case.MethodTestCase) and \
+            hasattr(test.context, 'urls'):
+                # We have to use this slightly awkward syntax due to the fact
+                # that we're using *args and **kwargs together.
+                self.old_urlconf = settings.ROOT_URLCONF
+                settings.ROOT_URLCONF = self.urls
+                clear_url_caches()
+
         self.mail.outbox = []
 
     def finalize(self, result=None):
@@ -134,5 +145,11 @@
 
         from django.test.utils import teardown_test_environment
         from django.db import connection
+        from django.conf import settings
         connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity)   
         teardown_test_environment()
+
+        if hasattr(self, 'old_urlconf'):
+            settings.ROOT_URLCONF = self.old_urlconf
+            clear_url_caches()
+