Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote branch 'aar/master'

Conflicts:
	djangosanetesting/noseplugins.py
  • Loading branch information...
commit 2637a36d48f777384eb14f9f882286ff60a0cd18 2 parents e3b1d64 + 336c106
Lukas Linhart authored
View
57 djangosanetesting/noseplugins.py
@@ -11,11 +11,12 @@
import unittest
from django.core.management import call_command
-from django.core.handlers.wsgi import WSGIHandler
-from django.core.servers.basehttp import WSGIRequestHandler, AdminMediaHandler, WSGIServerException
+from django.core.servers.basehttp import WSGIRequestHandler, WSGIServerException
from django.core.urlresolvers import clear_url_caches
+from django.test import TestCase as DjangoTestCase
import nose
+from nose import SkipTest
from nose.plugins import Plugin
import djangosanetesting
@@ -25,7 +26,8 @@
#from djagnosanetesting.cache import flush_django_cache
from djangosanetesting.selenium.driver import selenium
from djangosanetesting.utils import (
- get_live_server_path, test_database_exists,
+ get_databases, get_live_server_path, test_databases_exist,
+ get_server_handler,
DEFAULT_LIVE_SERVER_ADDRESS, DEFAULT_LIVE_SERVER_PORT,
)
TEST_CASE_CLASSES = (djangosanetesting.cases.SaneTestCase, unittest.TestCase)
@@ -171,7 +173,7 @@ def __init__(self, address, port):
def run(self):
"""Sets up test server and loops over handling http requests."""
try:
- handler = AdminMediaHandler(WSGIHandler())
+ handler = get_server_handler()
server_address = (self.address, self.port)
httpd = StoppableWSGIServer(server_address, WSGIRequestHandler)
#httpd = basehttp.WSGIServer(server_address, basehttp.WSGIRequestHandler)
@@ -203,7 +205,7 @@ def options(self, parser, env=os.environ):
def configure(self, options, config):
Plugin.configure(self, options, config)
-
+
def start_server(self):
raise NotImplementedError()
@@ -212,12 +214,12 @@ def stop_server(self):
def check_database_multithread_compilant(self):
# When using memory database, complain as we'd use indepenent databases
- from django.conf import settings
- if settings.DATABASE_ENGINE == 'sqlite3' \
- and (not getattr(settings, 'TEST_DATABASE_NAME', False) or settings.TEST_DATABASE_NAME == ':memory:'):
- self.skipped = True
- return False
- #raise SkipTest("You're running database in memory, but trying to use live server in another thread. Skipping.")
+ connections = get_databases()
+ for alias in connections:
+ database = connections[alias]
+ if database.settings_dict['NAME'] == ':memory:' and database.settings_dict['ENGINE'] in ('django.db.backends.sqlite3', 'sqlite3'):
+ self.skipped = True
+ return False
return True
def startTest(self, test):
@@ -226,7 +228,7 @@ def startTest(self, test):
test_case_instance = get_test_case_instance(test)
if not self.server_started and getattr_test(test, "start_live_server", False):
if not self.check_database_multithread_compilant():
- return
+ raise SkipTest("You're running database in memory, but trying to use live server in another thread. Skipping.")
self.start_server(
address=getattr(settings, "LIVE_SERVER_ADDRESS", DEFAULT_LIVE_SERVER_ADDRESS),
port=int(getattr(settings, "LIVE_SERVER_PORT", DEFAULT_LIVE_SERVER_PORT))
@@ -285,11 +287,11 @@ class CherryPyLiveServerPlugin(AbstractLiveServerPlugin):
activation_parameter = '--with-cherrypyliveserver'
def start_server(self, address='0.0.0.0', port=8000):
- _application = AdminMediaHandler(WSGIHandler())
-
+ handler = get_server_handler()
+
def application(environ, start_response):
environ['PATH_INFO'] = environ['SCRIPT_NAME'] + environ['PATH_INFO']
- return _application(environ, start_response)
+ return handler(environ, start_response)
from cherrypy.wsgiserver import CherryPyWSGIServer
from threading import Thread
@@ -373,7 +375,7 @@ def configure(self, options, config):
def setup_databases(self, verbosity, autoclobber, **kwargs):
# Taken from Django 1.2 code, (C) respective Django authors. Modified for backward compatibility by me
- connections = self._get_databases()
+ connections = get_databases()
old_names = []
mirrors = []
@@ -402,7 +404,7 @@ def setup_databases(self, verbosity, autoclobber, **kwargs):
def teardown_databases(self, old_config, verbosity, **kwargs):
# Taken from Django 1.2 code, (C) respective Django authors
- connections = self._get_databases()
+ connections = get_databases()
old_names, mirrors = old_config
# Point all the mirrors back to the originals
for alias, connection in mirrors:
@@ -425,9 +427,6 @@ def prepareTestRunner(self, runner):
# FIXME: this should be lazy for tests that do not need test
# database at all
- from django.conf import settings
- self.old_name = settings.DATABASE_NAME
-
flush_cache()
def finalize(self, result):
@@ -447,6 +446,11 @@ def startTest(self, test):
"""
When preparing test, check whether to make our database fresh
"""
+
+ test_case = get_test_case_class(test)
+ if issubclass(test_case, DjangoTestCase):
+ return
+
#####
### FIXME: It would be nice to separate handlings as plugins et al...but what
### about the context?
@@ -458,6 +462,7 @@ def startTest(self, test):
test_case = get_test_case_class(test)
test_case_instance = get_test_case_instance(test)
+
mail.outbox = []
enable_test(test_case, 'django_plugin_started')
@@ -495,6 +500,11 @@ def stopTest(self, test):
"""
After test is run, clear urlconf, caches and database
"""
+
+ test_case = get_test_case_class(test)
+ if issubclass(test_case, DjangoTestCase):
+ return
+
from django.db import transaction
from django.conf import settings
@@ -592,7 +602,6 @@ def _create_test_databases(self):
if getattr(settings, "FLUSH_TEST_DATABASE_AFTER_INITIAL_SYNCDB", False):
getattr(settings, "TEST_DATABASE_FLUSH_COMMAND", flush_database)(self, database=db)
-
class DjangoTranslationPlugin(Plugin):
"""
For testcases with selenium_start set to True, connect to Selenium RC.
@@ -657,10 +666,14 @@ def startTest(self, test):
selenium = getattr(import_module(selenium_module), selenium_cls)
if getattr_test(test, "selenium_start", False):
+ browser = getattr(test_case, 'selenium_browser_command', None)
+ if browser is None:
+ browser = getattr(settings, "SELENIUM_BROWSER_COMMAND", '*opera')
+
sel = selenium(
getattr(settings, "SELENIUM_HOST", 'localhost'),
int(getattr(settings, "SELENIUM_PORT", 4444)),
- getattr(settings, "SELENIUM_BROWSER_COMMAND", '*opera'),
+ browser,
getattr(settings, "SELENIUM_URL_ROOT", get_live_server_path()),
)
try:
View
4 djangosanetesting/testrunner.py
@@ -20,6 +20,9 @@
__all__ = ("DstNoseTestSuiteRunner",)
+# This file doen't contain tests
+__test__ = False
+
"""
Act as Django test runner, but use nose. Enable common django-sane-testing
plugins by default.
@@ -63,7 +66,6 @@ def run_suite(self, nose_argv=None):
from django.test import utils
utils.setup_test_environment()
- old_db_name = settings.DATABASE_NAME
result_plugin = ResultPlugin()
plugins = [DjangoPlugin(), SeleniumPlugin(), DjangoTranslationPlugin(), result_plugin]
View
49 djangosanetesting/utils.py
@@ -2,6 +2,9 @@
from functools import wraps
import urllib2
+from django.core.servers.basehttp import AdminMediaHandler
+from django.core.handlers.wsgi import WSGIHandler
+
DEFAULT_LIVE_SERVER_PROTOCOL = "http"
DEFAULT_LIVE_SERVER_PORT = 8000
DEFAULT_LIVE_SERVER_ADDRESS = '0.0.0.0'
@@ -55,15 +58,35 @@ def is_test_database():
return settings.DATABASE_NAME == test_database_name
-def test_database_exists():
- from django.db import connection, DatabaseError
- from django.conf import settings
+def get_databases():
try:
- if getattr(settings, "DATABASE_ENGINE", None) == 'sqlite3':
- if not os.path.exists(settings.DATABASE_NAME):
- raise DatabaseError()
- connection.cursor()
+ from django.db import connections
+ except ImportError:
+ from django.conf import settings
+ from django.db import connection
+
+ if settings.TEST_DATABASE_NAME:
+ connection['TEST_NAME'] = settings.TEST_DATABASE_NAME
+
+ connections = {
+ DEFAULT_DB_ALIAS : connection
+ }
+
+ return connections
+
+
+def test_databases_exist():
+ from django.db import DatabaseError
+
+ connections = get_databases()
+ try:
+ for connection in connections:
+ if connection.settings_dict['NAME'] == 'sqlite3':
+ if not os.path.exists(connection.settings_dict['DATABASE_NAME']):
+ raise DatabaseError()
+ connection.cursor()
+
return True
except DatabaseError, err:
return False
@@ -159,3 +182,15 @@ def wrapped(*args, **kwargs):
return wrapped
return wrapper
+
+def get_server_handler():
+ handler = AdminMediaHandler(WSGIHandler())
+ try:
+ from django.contrib.staticfiles.handlers import StaticFilesHandler
+ handler = StaticFilesHandler(handler)
+ except:
+ pass
+
+ return handler
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.