[pypy-svn] r54942 - pypy/django/tests

antocuni at codespeak.net antocuni at codespeak.net
Mon May 19 16:48:03 CEST 2008


Author: antocuni
Date: Mon May 19 16:47:58 2008
New Revision: 54942

Modified:
   pypy/django/tests/conftest.py
Log:
(antocuni, fijal, pedronis around) (IN-PROGRESS) conftest.py for django



Modified: pypy/django/tests/conftest.py
==============================================================================
--- pypy/django/tests/conftest.py	(original)
+++ pypy/django/tests/conftest.py	Mon May 19 16:47:58 2008
@@ -8,7 +8,47 @@
 
 DIRNAMES = ['modeltests', 'regressiontests']#, 'django/contrib']
 
+import py.__.test.compat
+import unittest
+unittest.TestCase = py.__.test.compat.TestCase
+
+class DocTestItem(py.test.collect.Item):
+
+    def __init__(self, name, mod, docstr):
+        py.test.collect.Item.__init__(self, name)
+        self.docstr = docstr
+        self.mod = mod
+
+    def run(self):
+        from django.test.utils import setup_test_environment, teardown_test_environment
+        from django.test.utils import create_test_db, destroy_test_db
+        from django.conf import settings
+        old_name = settings.DATABASE_NAME
+        setup_test_environment()
+        create_test_db(0)
+        try:
+            self.execute(self.mod, self.docstr)
+        finally:
+            destroy_test_db(old_name, 0)
+            teardown_test_environment()         
+       
+    def execute(self, mod, docstring):
+        from django.test.testcases import OutputChecker, DocTestRunner
+        from django.test import _doctest
+        checker = OutputChecker()
+        runner = DocTestRunner(checker=checker)
+        finder = _doctest.DocTestFinder()
+        for test in finder.find(mod):
+            runner.run(test)
+        if runner.failures: 
+            py.test.fail("doctest %s: %s failed out of %s" %(
+                         self.fspath, runner.failures, runner.tries))
+
+
+loaded_apps = {}
+
 class Directory(py.test.collect.Directory):
+    
     def run(self):
         rel = self.fspath.relto(rootdir)
         if not rel:
@@ -23,7 +63,28 @@
             return self.run2()
 
     def run2(self):
+        from django.conf import settings
+        from django.db.models.loading import load_app
+        from django.test.simple import build_suite
+        rel = self.fspath.relto(rootdir)
+        app_name = rel.replace(os.path.sep, '.')
+        if app_name in loaded_apps:
+            mod = loaded_apps[app_name]
+        else:
+            mod = load_app(app_name)
+            loaded_apps[app_name] = mod
+            if mod:
+                if app_name not in settings.INSTALLED_APPS:
+                    settings.INSTALLED_APPS.append(app_name)
+
+        if hasattr(mod, '__test__') and mod.__test__.get('API_TESTS'):
+            return [DocTestItem(self.fspath.basename + '.docstring', mod, mod.__test__['API_TESTS'])]
         return []
+        #suite = build_suite(mod)
+        #x = suite._tests[1]._tests[0]
+        #return [x]
+        #return suite._tests
+        # modify the global settings
 
     def run1(self):
         models = []
@@ -39,10 +100,54 @@
     def run0(self):
         return DIRNAMES
 
-    def join(self, name):
+    def join(self, name_or_item):
+        if isinstance(name_or_item, py.test.collect.Item):
+            return name_or_item
+        name = name_or_item
         path = self.fspath.join(name)
         if path.check(dir=1):
             return Directory(self.fspath.join(name))
         else:
-            explode
-            
+            raise NotImplementedError
+
+ALWAYS_INSTALLED_APPS = [
+    'django.contrib.contenttypes',
+    'django.contrib.auth',
+    'django.contrib.sites',
+    'django.contrib.flatpages',
+    'django.contrib.redirects',
+    'django.contrib.sessions',
+    'django.contrib.comments',
+    'django.contrib.admin',
+]
+
+TEST_TEMPLATE_DIR = 'templates'
+
+def setup():
+    import sys
+    sys.path.insert(0, str(rootdir))
+    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
+    
+    from django.conf import settings
+
+    # Redirect some settings for the duration of these tests.
+    settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
+    settings.ROOT_URLCONF = 'urls'
+    settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
+    settings.USE_I18N = True
+    settings.LANGUAGE_CODE = 'en'
+    settings.LOGIN_URL = '/accounts/login/'
+    settings.MIDDLEWARE_CLASSES = (
+        'django.contrib.sessions.middleware.SessionMiddleware',
+        'django.contrib.auth.middleware.AuthenticationMiddleware',
+        'django.middleware.common.CommonMiddleware',
+    )
+    settings.SITE_ID = 1
+
+    # Load all the ALWAYS_INSTALLED_APPS.
+    # (This import statement is intentionally delayed until after we
+    # access settings because of the USE_I18N dependency.)
+    from django.db.models.loading import get_apps, load_app
+    get_apps()
+
+setup()



More information about the Pypy-commit mailing list