2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-02-01 01:56:26 +08:00
|
|
|
from copy import copy
|
2012-02-01 04:36:11 +08:00
|
|
|
import difflib
|
2013-02-01 01:56:26 +08:00
|
|
|
import errno
|
|
|
|
from functools import wraps
|
2012-04-30 01:58:00 +08:00
|
|
|
import json
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
import os
|
2007-09-04 08:50:06 +08:00
|
|
|
import re
|
2010-10-12 11:33:19 +08:00
|
|
|
import sys
|
2012-07-20 21:36:52 +08:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlsplit, urlunsplit
|
|
|
|
except ImportError: # Python 2
|
|
|
|
from urlparse import urlsplit, urlunsplit
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import threading
|
2013-02-01 01:56:26 +08:00
|
|
|
import warnings
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.conf import settings
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
from django.contrib.staticfiles.handlers import StaticFilesHandler
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
from django.core.exceptions import ValidationError, ImproperlyConfigured
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management import call_command
|
2012-07-25 04:24:16 +08:00
|
|
|
from django.core.management.color import no_style
|
2011-12-30 04:22:13 +08:00
|
|
|
from django.core.servers.basehttp import (WSGIRequestHandler, WSGIServer,
|
|
|
|
WSGIServerException)
|
2008-07-19 22:46:55 +08:00
|
|
|
from django.core.urlresolvers import clear_url_caches
|
2013-03-02 04:29:39 +08:00
|
|
|
from django.db import connection, connections, DEFAULT_DB_ALIAS, transaction
|
2011-08-23 10:32:37 +08:00
|
|
|
from django.forms.fields import CharField
|
2008-07-19 22:46:55 +08:00
|
|
|
from django.http import QueryDict
|
2007-05-31 21:18:12 +08:00
|
|
|
from django.test import _doctest as doctest
|
2007-05-05 11:03:33 +08:00
|
|
|
from django.test.client import Client
|
2012-02-01 04:36:11 +08:00
|
|
|
from django.test.html import HTMLParseError, parse_html
|
2012-02-01 03:23:09 +08:00
|
|
|
from django.test.signals import template_rendered
|
2013-03-02 04:29:39 +08:00
|
|
|
from django.test.utils import (CaptureQueriesContext, ContextList,
|
|
|
|
override_settings, compare_xml, strip_quotes)
|
|
|
|
from django.utils import six, unittest as ut2
|
2012-08-29 02:59:56 +08:00
|
|
|
from django.utils.encoding import force_text
|
2013-03-02 04:29:39 +08:00
|
|
|
from django.utils.unittest import skipIf # Imported here for backward compatibility
|
2012-02-01 04:36:11 +08:00
|
|
|
from django.utils.unittest.util import safe_repr
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
from django.views.static import serve
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2013-03-02 04:29:39 +08:00
|
|
|
|
2010-10-18 23:53:55 +08:00
|
|
|
__all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase',
|
2011-08-13 08:42:08 +08:00
|
|
|
'SimpleTestCase', 'skipIfDBFeature', 'skipUnlessDBFeature')
|
2007-05-05 11:03:33 +08:00
|
|
|
|
2013-03-02 04:29:39 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
|
2011-10-05 20:50:44 +08:00
|
|
|
normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)",
|
|
|
|
lambda m: "Decimal(\"%s\")" % m.groups()[0], s)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2007-09-04 07:14:51 +08:00
|
|
|
def to_list(value):
|
|
|
|
"""
|
|
|
|
Puts value into a list if it's not already one.
|
|
|
|
Returns an empty list if value is None.
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
value = []
|
|
|
|
elif not isinstance(value, list):
|
|
|
|
value = [value]
|
|
|
|
return value
|
|
|
|
|
2009-01-16 10:30:22 +08:00
|
|
|
real_commit = transaction.commit
|
|
|
|
real_rollback = transaction.rollback
|
|
|
|
real_enter_transaction_management = transaction.enter_transaction_management
|
|
|
|
real_leave_transaction_management = transaction.leave_transaction_management
|
2013-02-06 05:52:29 +08:00
|
|
|
real_abort = transaction.abort
|
2009-01-16 10:30:22 +08:00
|
|
|
|
2009-03-31 21:04:28 +08:00
|
|
|
def nop(*args, **kwargs):
|
2009-01-16 10:30:22 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
def disable_transaction_methods():
|
|
|
|
transaction.commit = nop
|
|
|
|
transaction.rollback = nop
|
|
|
|
transaction.enter_transaction_management = nop
|
2009-02-27 21:14:59 +08:00
|
|
|
transaction.leave_transaction_management = nop
|
2013-02-06 05:52:29 +08:00
|
|
|
transaction.abort = nop
|
2009-01-16 10:30:22 +08:00
|
|
|
|
|
|
|
def restore_transaction_methods():
|
|
|
|
transaction.commit = real_commit
|
|
|
|
transaction.rollback = real_rollback
|
|
|
|
transaction.enter_transaction_management = real_enter_transaction_management
|
|
|
|
transaction.leave_transaction_management = real_leave_transaction_management
|
2013-02-06 05:52:29 +08:00
|
|
|
transaction.abort = real_abort
|
2007-09-04 07:14:51 +08:00
|
|
|
|
2012-02-01 04:36:11 +08:00
|
|
|
|
|
|
|
def assert_and_parse_html(self, html, user_msg, msg):
|
|
|
|
try:
|
|
|
|
dom = parse_html(html)
|
2012-04-29 00:09:37 +08:00
|
|
|
except HTMLParseError as e:
|
2012-06-08 00:08:47 +08:00
|
|
|
standardMsg = '%s\n%s' % (msg, e.msg)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.fail(self._formatMessage(user_msg, standardMsg))
|
|
|
|
return dom
|
|
|
|
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
class OutputChecker(doctest.OutputChecker):
|
|
|
|
def check_output(self, want, got, optionflags):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
The entry method for doctest output checking. Defers to a sequence of
|
|
|
|
child checkers
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
2008-07-19 22:46:55 +08:00
|
|
|
checks = (self.check_output_default,
|
2009-01-15 19:06:34 +08:00
|
|
|
self.check_output_numeric,
|
2008-07-19 22:46:55 +08:00
|
|
|
self.check_output_xml,
|
|
|
|
self.check_output_json)
|
|
|
|
for check in checks:
|
|
|
|
if check(want, got, optionflags):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_output_default(self, want, got, optionflags):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
|
|
|
The default comparator provided by doctest - not perfect, but good for
|
|
|
|
most purposes
|
|
|
|
"""
|
2008-07-19 22:46:55 +08:00
|
|
|
return doctest.OutputChecker.check_output(self, want, got, optionflags)
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
def check_output_numeric(self, want, got, optionflags):
|
|
|
|
"""Doctest does an exact string comparison of output, which means that
|
|
|
|
some numerically equivalent values aren't equal. This check normalizes
|
|
|
|
* long integers (22L) so that they equal normal integers. (22)
|
|
|
|
* Decimals so that they are comparable, regardless of the change
|
|
|
|
made to __repr__ in Python 2.6.
|
2008-07-19 22:46:55 +08:00
|
|
|
"""
|
2009-01-15 19:06:34 +08:00
|
|
|
return doctest.OutputChecker.check_output(self,
|
|
|
|
normalize_decimals(normalize_long_ints(want)),
|
|
|
|
normalize_decimals(normalize_long_ints(got)),
|
|
|
|
optionflags)
|
2008-07-19 22:46:55 +08:00
|
|
|
|
|
|
|
def check_output_xml(self, want, got, optionsflags):
|
|
|
|
try:
|
2012-10-06 19:14:11 +08:00
|
|
|
return compare_xml(want, got)
|
2011-10-05 20:50:44 +08:00
|
|
|
except Exception:
|
2008-07-19 22:46:55 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
def check_output_json(self, want, got, optionsflags):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
|
|
|
Tries to compare want and got as if they were JSON-encoded data
|
|
|
|
"""
|
2012-10-06 19:14:11 +08:00
|
|
|
want, got = strip_quotes(want, got)
|
2008-07-19 22:46:55 +08:00
|
|
|
try:
|
2012-04-30 01:58:00 +08:00
|
|
|
want_json = json.loads(want)
|
|
|
|
got_json = json.loads(got)
|
2011-10-05 20:50:44 +08:00
|
|
|
except Exception:
|
2008-07-19 22:46:55 +08:00
|
|
|
return False
|
|
|
|
return want_json == got_json
|
|
|
|
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
class DocTestRunner(doctest.DocTestRunner):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
doctest.DocTestRunner.__init__(self, *args, **kwargs)
|
|
|
|
self.optionflags = doctest.ELLIPSIS
|
2007-09-04 08:50:06 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2013-03-02 04:29:39 +08:00
|
|
|
class _AssertNumQueriesContext(CaptureQueriesContext):
|
2010-10-12 11:33:19 +08:00
|
|
|
def __init__(self, test_case, num, connection):
|
|
|
|
self.test_case = test_case
|
|
|
|
self.num = num
|
2013-03-02 04:29:39 +08:00
|
|
|
super(_AssertNumQueriesContext, self).__init__(connection)
|
2010-10-12 11:33:19 +08:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2013-03-17 17:26:27 +08:00
|
|
|
super(_AssertNumQueriesContext, self).__exit__(exc_type, exc_value, traceback)
|
2010-10-12 11:33:19 +08:00
|
|
|
if exc_type is not None:
|
|
|
|
return
|
2013-03-02 04:29:39 +08:00
|
|
|
executed = len(self)
|
2010-10-12 11:33:19 +08:00
|
|
|
self.test_case.assertEqual(
|
|
|
|
executed, self.num, "%d queries executed, %d expected" % (
|
|
|
|
executed, self.num
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2012-02-01 03:23:09 +08:00
|
|
|
class _AssertTemplateUsedContext(object):
|
|
|
|
def __init__(self, test_case, template_name):
|
|
|
|
self.test_case = test_case
|
|
|
|
self.template_name = template_name
|
|
|
|
self.rendered_templates = []
|
|
|
|
self.rendered_template_names = []
|
|
|
|
self.context = ContextList()
|
|
|
|
|
|
|
|
def on_template_render(self, sender, signal, template, context, **kwargs):
|
|
|
|
self.rendered_templates.append(template)
|
|
|
|
self.rendered_template_names.append(template.name)
|
|
|
|
self.context.append(copy(context))
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
return self.template_name in self.rendered_template_names
|
|
|
|
|
|
|
|
def message(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s was not rendered.' % self.template_name
|
2012-02-01 03:23:09 +08:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
template_rendered.connect(self.on_template_render)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
template_rendered.disconnect(self.on_template_render)
|
|
|
|
if exc_type is not None:
|
|
|
|
return
|
2010-10-12 11:33:19 +08:00
|
|
|
|
2012-02-01 03:23:09 +08:00
|
|
|
if not self.test():
|
|
|
|
message = self.message()
|
|
|
|
if len(self.rendered_templates) == 0:
|
2012-06-08 00:08:47 +08:00
|
|
|
message += ' No template was rendered.'
|
2012-02-01 03:23:09 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
message += ' Following templates were rendered: %s' % (
|
2012-02-01 03:23:09 +08:00
|
|
|
', '.join(self.rendered_template_names))
|
|
|
|
self.test_case.fail(message)
|
|
|
|
|
|
|
|
|
|
|
|
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
|
|
|
|
def test(self):
|
|
|
|
return self.template_name not in self.rendered_template_names
|
|
|
|
|
|
|
|
def message(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s was rendered.' % self.template_name
|
2012-02-01 03:23:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SimpleTestCase(ut2.TestCase):
|
2013-02-01 01:56:26 +08:00
|
|
|
|
|
|
|
_warn_txt = ("save_warnings_state/restore_warnings_state "
|
|
|
|
"django.test.*TestCase methods are deprecated. Use Python's "
|
|
|
|
"warnings.catch_warnings context manager instead.")
|
|
|
|
|
2012-11-25 06:47:41 +08:00
|
|
|
def __call__(self, result=None):
|
|
|
|
"""
|
|
|
|
Wrapper around default __call__ method to perform common Django test
|
|
|
|
set up. This means that user-defined Test Cases aren't required to
|
|
|
|
include a call to super().setUp().
|
|
|
|
"""
|
|
|
|
testMethod = getattr(self, self._testMethodName)
|
|
|
|
skipped = (getattr(self.__class__, "__unittest_skip__", False) or
|
|
|
|
getattr(testMethod, "__unittest_skip__", False))
|
|
|
|
|
|
|
|
if not skipped:
|
|
|
|
try:
|
|
|
|
self._pre_setup()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
result.addError(self, sys.exc_info())
|
|
|
|
return
|
|
|
|
super(SimpleTestCase, self).__call__(result)
|
|
|
|
if not skipped:
|
|
|
|
try:
|
|
|
|
self._post_teardown()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
result.addError(self, sys.exc_info())
|
|
|
|
return
|
|
|
|
|
|
|
|
def _pre_setup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _post_teardown(self):
|
|
|
|
pass
|
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
def save_warnings_state(self):
|
|
|
|
"""
|
|
|
|
Saves the state of the warnings module
|
|
|
|
"""
|
2013-02-01 01:56:26 +08:00
|
|
|
warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
|
|
|
|
self._warnings_state = warnings.filters[:]
|
2011-08-13 08:42:08 +08:00
|
|
|
|
|
|
|
def restore_warnings_state(self):
|
|
|
|
"""
|
2011-10-05 20:50:44 +08:00
|
|
|
Restores the state of the warnings module to the state
|
2011-08-13 08:42:08 +08:00
|
|
|
saved by save_warnings_state()
|
|
|
|
"""
|
2013-02-01 01:56:26 +08:00
|
|
|
warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
|
|
|
|
warnings.filters = self._warnings_state[:]
|
2011-08-13 08:42:08 +08:00
|
|
|
|
|
|
|
def settings(self, **kwargs):
|
|
|
|
"""
|
|
|
|
A context manager that temporarily sets a setting and reverts
|
|
|
|
back to the original value when exiting the context.
|
|
|
|
"""
|
|
|
|
return override_settings(**kwargs)
|
|
|
|
|
|
|
|
def assertRaisesMessage(self, expected_exception, expected_message,
|
|
|
|
callable_obj=None, *args, **kwargs):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
|
|
|
Asserts that the message in a raised exception matches the passed
|
|
|
|
value.
|
2011-08-13 08:42:08 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
expected_exception: Exception class expected to be raised.
|
|
|
|
expected_message: expected error message string value.
|
|
|
|
callable_obj: Function to be called.
|
|
|
|
args: Extra args.
|
|
|
|
kwargs: Extra kwargs.
|
|
|
|
"""
|
2012-09-08 01:17:09 +08:00
|
|
|
return six.assertRaisesRegex(self, expected_exception,
|
2011-08-13 08:42:08 +08:00
|
|
|
re.escape(expected_message), callable_obj, *args, **kwargs)
|
|
|
|
|
2011-08-23 10:32:37 +08:00
|
|
|
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None,
|
2012-06-08 00:08:47 +08:00
|
|
|
field_kwargs=None, empty_value=''):
|
2011-08-23 10:32:37 +08:00
|
|
|
"""
|
|
|
|
Asserts that a form field behaves correctly with various inputs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fieldclass: the class of the field to be tested.
|
|
|
|
valid: a dictionary mapping valid inputs to their expected
|
|
|
|
cleaned values.
|
|
|
|
invalid: a dictionary mapping invalid inputs to one or more
|
|
|
|
raised error messages.
|
|
|
|
field_args: the args passed to instantiate the field
|
|
|
|
field_kwargs: the kwargs passed to instantiate the field
|
2013-03-07 16:21:59 +08:00
|
|
|
empty_value: the expected clean output for inputs in empty_values
|
2011-08-23 10:32:37 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
if field_args is None:
|
|
|
|
field_args = []
|
|
|
|
if field_kwargs is None:
|
|
|
|
field_kwargs = {}
|
|
|
|
required = fieldclass(*field_args, **field_kwargs)
|
2011-10-05 20:50:44 +08:00
|
|
|
optional = fieldclass(*field_args,
|
|
|
|
**dict(field_kwargs, required=False))
|
2011-08-23 10:32:37 +08:00
|
|
|
# test valid inputs
|
|
|
|
for input, output in valid.items():
|
|
|
|
self.assertEqual(required.clean(input), output)
|
|
|
|
self.assertEqual(optional.clean(input), output)
|
|
|
|
# test invalid inputs
|
|
|
|
for input, errors in invalid.items():
|
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
required.clean(input)
|
|
|
|
self.assertEqual(context_manager.exception.messages, errors)
|
|
|
|
|
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
optional.clean(input)
|
|
|
|
self.assertEqual(context_manager.exception.messages, errors)
|
|
|
|
# test required inputs
|
2012-07-21 16:00:10 +08:00
|
|
|
error_required = [force_text(required.error_messages['required'])]
|
2013-03-07 16:21:59 +08:00
|
|
|
for e in required.empty_values:
|
2011-08-23 10:32:37 +08:00
|
|
|
with self.assertRaises(ValidationError) as context_manager:
|
|
|
|
required.clean(e)
|
2011-10-05 20:50:44 +08:00
|
|
|
self.assertEqual(context_manager.exception.messages,
|
|
|
|
error_required)
|
2011-08-23 10:32:37 +08:00
|
|
|
self.assertEqual(optional.clean(e), empty_value)
|
|
|
|
# test that max_length and min_length are always accepted
|
|
|
|
if issubclass(fieldclass, CharField):
|
|
|
|
field_kwargs.update({'min_length':2, 'max_length':20})
|
2011-10-05 20:50:44 +08:00
|
|
|
self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs),
|
|
|
|
fieldclass))
|
2011-08-23 10:32:37 +08:00
|
|
|
|
2012-02-01 04:36:11 +08:00
|
|
|
def assertHTMLEqual(self, html1, html2, msg=None):
|
|
|
|
"""
|
2012-02-04 04:45:45 +08:00
|
|
|
Asserts that two HTML snippets are semantically the same.
|
|
|
|
Whitespace in most cases is ignored, and attribute ordering is not
|
|
|
|
significant. The passed-in arguments must be valid HTML.
|
2012-02-01 04:36:11 +08:00
|
|
|
"""
|
|
|
|
dom1 = assert_and_parse_html(self, html1, msg,
|
2012-06-08 00:08:47 +08:00
|
|
|
'First argument is not valid HTML:')
|
2012-02-01 04:36:11 +08:00
|
|
|
dom2 = assert_and_parse_html(self, html2, msg,
|
2012-06-08 00:08:47 +08:00
|
|
|
'Second argument is not valid HTML:')
|
2012-02-01 04:36:11 +08:00
|
|
|
|
|
|
|
if dom1 != dom2:
|
|
|
|
standardMsg = '%s != %s' % (
|
|
|
|
safe_repr(dom1, True), safe_repr(dom2, True))
|
|
|
|
diff = ('\n' + '\n'.join(difflib.ndiff(
|
2012-07-20 20:48:51 +08:00
|
|
|
six.text_type(dom1).splitlines(),
|
|
|
|
six.text_type(dom2).splitlines())))
|
2012-02-01 04:36:11 +08:00
|
|
|
standardMsg = self._truncateMessage(standardMsg, diff)
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
|
|
|
|
def assertHTMLNotEqual(self, html1, html2, msg=None):
|
|
|
|
"""Asserts that two HTML snippets are not semantically equivalent."""
|
|
|
|
dom1 = assert_and_parse_html(self, html1, msg,
|
2012-06-08 00:08:47 +08:00
|
|
|
'First argument is not valid HTML:')
|
2012-02-01 04:36:11 +08:00
|
|
|
dom2 = assert_and_parse_html(self, html2, msg,
|
2012-06-08 00:08:47 +08:00
|
|
|
'Second argument is not valid HTML:')
|
2012-02-01 04:36:11 +08:00
|
|
|
|
|
|
|
if dom1 == dom2:
|
|
|
|
standardMsg = '%s == %s' % (
|
|
|
|
safe_repr(dom1, True), safe_repr(dom2, True))
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
|
2012-09-28 07:49:10 +08:00
|
|
|
def assertInHTML(self, needle, haystack, count = None, msg_prefix=''):
|
|
|
|
needle = assert_and_parse_html(self, needle, None,
|
|
|
|
'First argument is not valid HTML:')
|
|
|
|
haystack = assert_and_parse_html(self, haystack, None,
|
|
|
|
'Second argument is not valid HTML:')
|
|
|
|
real_count = haystack.count(needle)
|
|
|
|
if count is not None:
|
|
|
|
self.assertEqual(real_count, count,
|
|
|
|
msg_prefix + "Found %d instances of '%s' in response"
|
|
|
|
" (expected %d)" % (real_count, needle, count))
|
|
|
|
else:
|
|
|
|
self.assertTrue(real_count != 0,
|
|
|
|
msg_prefix + "Couldn't find '%s' in response" % needle)
|
|
|
|
|
2012-09-28 07:52:24 +08:00
|
|
|
def assertJSONEqual(self, raw, expected_data, msg=None):
|
|
|
|
try:
|
|
|
|
data = json.loads(raw)
|
|
|
|
except ValueError:
|
|
|
|
self.fail("First argument is not valid JSON: %r" % raw)
|
|
|
|
if isinstance(expected_data, six.string_types):
|
|
|
|
try:
|
|
|
|
expected_data = json.loads(expected_data)
|
|
|
|
except ValueError:
|
|
|
|
self.fail("Second argument is not valid JSON: %r" % expected_data)
|
|
|
|
self.assertEqual(data, expected_data, msg=msg)
|
|
|
|
|
2012-10-06 19:14:11 +08:00
|
|
|
def assertXMLEqual(self, xml1, xml2, msg=None):
|
|
|
|
"""
|
|
|
|
Asserts that two XML snippets are semantically the same.
|
|
|
|
Whitespace in most cases is ignored, and attribute ordering is not
|
|
|
|
significant. The passed-in arguments must be valid XML.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result = compare_xml(xml1, xml2)
|
|
|
|
except Exception as e:
|
|
|
|
standardMsg = 'First or second argument is not valid XML\n%s' % e
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
else:
|
|
|
|
if not result:
|
|
|
|
standardMsg = '%s != %s' % (safe_repr(xml1, True), safe_repr(xml2, True))
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
|
|
|
|
def assertXMLNotEqual(self, xml1, xml2, msg=None):
|
|
|
|
"""
|
|
|
|
Asserts that two XML snippets are not semantically equivalent.
|
|
|
|
Whitespace in most cases is ignored, and attribute ordering is not
|
|
|
|
significant. The passed-in arguments must be valid XML.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
result = compare_xml(xml1, xml2)
|
|
|
|
except Exception as e:
|
|
|
|
standardMsg = 'First or second argument is not valid XML\n%s' % e
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
else:
|
|
|
|
if result:
|
|
|
|
standardMsg = '%s == %s' % (safe_repr(xml1, True), safe_repr(xml2, True))
|
|
|
|
self.fail(self._formatMessage(msg, standardMsg))
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
class TransactionTestCase(SimpleTestCase):
|
2012-07-25 04:24:16 +08:00
|
|
|
|
2010-10-09 12:50:47 +08:00
|
|
|
# The class we'll use for the test client self.client.
|
|
|
|
# Can be overridden in derived classes.
|
|
|
|
client_class = Client
|
|
|
|
|
2012-07-25 04:24:16 +08:00
|
|
|
# Subclasses can ask for resetting of auto increment sequence before each
|
|
|
|
# test case
|
|
|
|
reset_sequences = False
|
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
def _pre_setup(self):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""Performs any pre-test setup. This includes:
|
|
|
|
|
2007-11-23 18:51:17 +08:00
|
|
|
* Flushing the database.
|
2009-01-15 19:06:34 +08:00
|
|
|
* If the Test Case class has a 'fixtures' member, installing the
|
2007-11-23 18:51:17 +08:00
|
|
|
named fixtures.
|
2008-06-30 20:34:29 +08:00
|
|
|
* If the Test Case class has a 'urls' member, replace the
|
|
|
|
ROOT_URLCONF with it.
|
2007-05-08 19:19:34 +08:00
|
|
|
* Clearing the mail test outbox.
|
2007-03-01 21:11:08 +08:00
|
|
|
"""
|
2012-11-25 06:47:41 +08:00
|
|
|
self.client = self.client_class()
|
2009-01-16 10:30:22 +08:00
|
|
|
self._fixture_setup()
|
|
|
|
self._urlconf_setup()
|
|
|
|
mail.outbox = []
|
|
|
|
|
2012-11-24 16:48:21 +08:00
|
|
|
def _databases_names(self, include_mirrors=True):
|
|
|
|
# If the test case has a multi_db=True flag, act on all databases,
|
|
|
|
# including mirrors or not. Otherwise, just on the default DB.
|
|
|
|
if getattr(self, 'multi_db', False):
|
|
|
|
return [alias for alias in connections
|
|
|
|
if include_mirrors or not connections[alias].settings_dict['TEST_MIRROR']]
|
|
|
|
else:
|
|
|
|
return [DEFAULT_DB_ALIAS]
|
|
|
|
|
2012-07-25 04:24:16 +08:00
|
|
|
def _reset_sequences(self, db_name):
|
|
|
|
conn = connections[db_name]
|
|
|
|
if conn.features.supports_sequence_reset:
|
|
|
|
sql_list = \
|
|
|
|
conn.ops.sequence_reset_by_name_sql(no_style(),
|
|
|
|
conn.introspection.sequence_list())
|
|
|
|
if sql_list:
|
2013-03-04 20:12:59 +08:00
|
|
|
with transaction.commit_on_success_unless_managed(using=db_name):
|
2012-07-25 04:24:16 +08:00
|
|
|
cursor = conn.cursor()
|
|
|
|
for sql in sql_list:
|
|
|
|
cursor.execute(sql)
|
|
|
|
|
2009-01-16 10:30:22 +08:00
|
|
|
def _fixture_setup(self):
|
2012-11-24 16:48:21 +08:00
|
|
|
for db_name in self._databases_names(include_mirrors=False):
|
2012-07-25 04:24:16 +08:00
|
|
|
# Reset sequences
|
|
|
|
if self.reset_sequences:
|
|
|
|
self._reset_sequences(db_name)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
if hasattr(self, 'fixtures'):
|
|
|
|
# We have to use this slightly awkward syntax due to the fact
|
|
|
|
# that we're using *args and **kwargs together.
|
2011-10-05 20:50:44 +08:00
|
|
|
call_command('loaddata', *self.fixtures,
|
2012-07-25 04:24:16 +08:00
|
|
|
**{'verbosity': 0, 'database': db_name, 'skip_validation': True})
|
2009-01-16 10:30:22 +08:00
|
|
|
|
|
|
|
def _urlconf_setup(self):
|
2008-06-30 20:34:29 +08:00
|
|
|
if hasattr(self, 'urls'):
|
|
|
|
self._old_root_urlconf = settings.ROOT_URLCONF
|
|
|
|
settings.ROOT_URLCONF = self.urls
|
|
|
|
clear_url_caches()
|
2007-05-13 00:53:27 +08:00
|
|
|
|
2008-06-30 20:34:29 +08:00
|
|
|
def _post_teardown(self):
|
|
|
|
""" Performs any post-test things. This includes:
|
|
|
|
|
|
|
|
* Putting back the original ROOT_URLCONF if it was changed.
|
2010-10-30 21:03:37 +08:00
|
|
|
* Force closing the connection, so that the next test gets
|
|
|
|
a clean cursor.
|
2008-06-30 20:34:29 +08:00
|
|
|
"""
|
2009-01-16 10:30:22 +08:00
|
|
|
self._fixture_teardown()
|
|
|
|
self._urlconf_teardown()
|
2010-10-30 21:03:37 +08:00
|
|
|
# Some DB cursors include SQL statements as part of cursor
|
|
|
|
# creation. If you have a test that does rollback, the effect
|
|
|
|
# of these statements is lost, which can effect the operation
|
|
|
|
# of tests (e.g., losing a timezone setting causing objects to
|
|
|
|
# be created with the wrong time).
|
|
|
|
# To make sure this doesn't happen, get a clean connection at the
|
|
|
|
# start of every test.
|
2011-08-23 10:32:37 +08:00
|
|
|
for conn in connections.all():
|
|
|
|
conn.close()
|
2009-01-16 10:30:22 +08:00
|
|
|
|
|
|
|
def _fixture_teardown(self):
|
2012-11-24 16:48:21 +08:00
|
|
|
for db in self._databases_names(include_mirrors=False):
|
2012-07-25 04:24:16 +08:00
|
|
|
call_command('flush', verbosity=0, interactive=False, database=db,
|
|
|
|
skip_validation=True, reset_sequences=False)
|
2009-01-16 10:30:22 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def _urlconf_teardown(self):
|
2008-06-30 20:34:29 +08:00
|
|
|
if hasattr(self, '_old_root_urlconf'):
|
|
|
|
settings.ROOT_URLCONF = self._old_root_urlconf
|
|
|
|
clear_url_caches()
|
2007-05-05 11:03:33 +08:00
|
|
|
|
2007-09-04 08:50:06 +08:00
|
|
|
def assertRedirects(self, response, expected_url, status_code=302,
|
2010-01-22 23:02:02 +08:00
|
|
|
target_status_code=200, host=None, msg_prefix=''):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""Asserts that a response redirected to a specific URL, and that the
|
2007-05-05 11:03:33 +08:00
|
|
|
redirect URL can be loaded.
|
2007-09-04 08:50:06 +08:00
|
|
|
|
|
|
|
Note that assertRedirects won't work for external links since it uses
|
2007-08-31 19:37:28 +08:00
|
|
|
TestClient to do a request.
|
2007-05-05 11:03:33 +08:00
|
|
|
"""
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
if hasattr(response, 'redirect_chain'):
|
|
|
|
# The request was a followed redirect
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(len(response.redirect_chain) > 0,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Response didn't redirect as expected: Response"
|
|
|
|
" code was %d (expected %d)" %
|
|
|
|
(response.status_code, status_code))
|
2009-02-27 21:14:59 +08:00
|
|
|
|
|
|
|
self.assertEqual(response.redirect_chain[0][1], status_code,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Initial response didn't redirect as expected:"
|
|
|
|
" Response code was %d (expected %d)" %
|
|
|
|
(response.redirect_chain[0][1], status_code))
|
2009-02-27 21:14:59 +08:00
|
|
|
|
|
|
|
url, status_code = response.redirect_chain[-1]
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, target_status_code,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Response didn't redirect as expected: Final"
|
|
|
|
" Response code was %d (expected %d)" %
|
|
|
|
(response.status_code, target_status_code))
|
2009-02-27 21:14:59 +08:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Not a followed redirect
|
|
|
|
self.assertEqual(response.status_code, status_code,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Response didn't redirect as expected: Response"
|
|
|
|
" code was %d (expected %d)" %
|
|
|
|
(response.status_code, status_code))
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2013-02-13 16:55:43 +08:00
|
|
|
url = response.url
|
2009-02-27 21:14:59 +08:00
|
|
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
|
|
|
|
|
|
|
redirect_response = response.client.get(path, QueryDict(query))
|
|
|
|
|
|
|
|
# Get the redirection page, using the same client that was used
|
|
|
|
# to obtain the original response.
|
|
|
|
self.assertEqual(redirect_response.status_code, target_status_code,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Couldn't retrieve redirection page '%s':"
|
|
|
|
" response code was %d (expected %d)" %
|
|
|
|
(path, redirect_response.status_code, target_status_code))
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2011-10-05 20:50:44 +08:00
|
|
|
e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(
|
|
|
|
expected_url)
|
2007-11-11 11:54:21 +08:00
|
|
|
if not (e_scheme or e_netloc):
|
|
|
|
expected_url = urlunsplit(('http', host or 'testserver', e_path,
|
2009-02-27 21:14:59 +08:00
|
|
|
e_query, e_fragment))
|
|
|
|
|
2007-09-04 08:50:06 +08:00
|
|
|
self.assertEqual(url, expected_url,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Response redirected to '%s', expected '%s'" %
|
|
|
|
(url, expected_url))
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
def assertContains(self, response, text, count=None, status_code=200,
|
2012-02-01 04:36:11 +08:00
|
|
|
msg_prefix='', html=False):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
2010-08-07 00:54:17 +08:00
|
|
|
Asserts that a response indicates that some content was retrieved
|
2007-09-04 08:50:06 +08:00
|
|
|
successfully, (i.e., the HTTP status code was as expected), and that
|
|
|
|
``text`` occurs ``count`` times in the content of the response.
|
|
|
|
If ``count`` is None, the count doesn't matter - the assertion is true
|
|
|
|
if the text occurs at least once in the response.
|
2007-05-05 11:03:33 +08:00
|
|
|
"""
|
2011-10-22 17:15:50 +08:00
|
|
|
|
|
|
|
# If the response supports deferred rendering and hasn't been rendered
|
|
|
|
# yet, then ensure that it does get rendered before proceeding further.
|
|
|
|
if (hasattr(response, 'render') and callable(response.render)
|
|
|
|
and not response.is_rendered):
|
|
|
|
response.render()
|
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
self.assertEqual(response.status_code, status_code,
|
2010-08-07 00:54:17 +08:00
|
|
|
msg_prefix + "Couldn't retrieve content: Response code was %d"
|
2010-01-22 23:02:02 +08:00
|
|
|
" (expected %d)" % (response.status_code, status_code))
|
2012-09-19 02:58:40 +08:00
|
|
|
text = force_text(text, encoding=response._charset)
|
2012-10-24 17:33:56 +08:00
|
|
|
if response.streaming:
|
|
|
|
content = b''.join(response.streaming_content)
|
|
|
|
else:
|
|
|
|
content = response.content
|
|
|
|
content = content.decode(response._charset)
|
2012-02-01 04:36:11 +08:00
|
|
|
if html:
|
|
|
|
content = assert_and_parse_html(self, content, None,
|
2012-06-08 00:08:47 +08:00
|
|
|
"Response's content is not valid HTML:")
|
2012-08-14 18:19:17 +08:00
|
|
|
text = assert_and_parse_html(self, text, None,
|
2012-06-08 00:08:47 +08:00
|
|
|
"Second argument is not valid HTML:")
|
2012-08-14 18:19:17 +08:00
|
|
|
real_count = content.count(text)
|
2007-07-21 12:36:28 +08:00
|
|
|
if count is not None:
|
2007-07-20 22:32:20 +08:00
|
|
|
self.assertEqual(real_count, count,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Found %d instances of '%s' in response"
|
|
|
|
" (expected %d)" % (real_count, text, count))
|
2007-07-20 22:32:20 +08:00
|
|
|
else:
|
2010-11-06 08:43:20 +08:00
|
|
|
self.assertTrue(real_count != 0,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Couldn't find '%s' in response" % text)
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
def assertNotContains(self, response, text, status_code=200,
|
2012-02-01 04:36:11 +08:00
|
|
|
msg_prefix='', html=False):
|
2008-06-06 21:50:02 +08:00
|
|
|
"""
|
2010-08-07 00:54:17 +08:00
|
|
|
Asserts that a response indicates that some content was retrieved
|
2008-06-06 21:50:02 +08:00
|
|
|
successfully, (i.e., the HTTP status code was as expected), and that
|
|
|
|
``text`` doesn't occurs in the content of the response.
|
|
|
|
"""
|
2011-10-22 17:15:50 +08:00
|
|
|
|
|
|
|
# If the response supports deferred rendering and hasn't been rendered
|
|
|
|
# yet, then ensure that it does get rendered before proceeding further.
|
|
|
|
if (hasattr(response, 'render') and callable(response.render)
|
|
|
|
and not response.is_rendered):
|
|
|
|
response.render()
|
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2008-06-06 21:50:02 +08:00
|
|
|
self.assertEqual(response.status_code, status_code,
|
2010-08-07 00:54:17 +08:00
|
|
|
msg_prefix + "Couldn't retrieve content: Response code was %d"
|
2010-01-22 23:02:02 +08:00
|
|
|
" (expected %d)" % (response.status_code, status_code))
|
2012-09-19 02:58:40 +08:00
|
|
|
text = force_text(text, encoding=response._charset)
|
2012-08-14 18:19:17 +08:00
|
|
|
content = response.content.decode(response._charset)
|
2012-02-01 04:36:11 +08:00
|
|
|
if html:
|
|
|
|
content = assert_and_parse_html(self, content, None,
|
2012-06-08 00:08:47 +08:00
|
|
|
'Response\'s content is not valid HTML:')
|
2012-08-14 18:19:17 +08:00
|
|
|
text = assert_and_parse_html(self, text, None,
|
2012-06-08 00:08:47 +08:00
|
|
|
'Second argument is not valid HTML:')
|
2012-08-14 18:19:17 +08:00
|
|
|
self.assertEqual(content.count(text), 0,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Response should not contain '%s'" % text)
|
2008-06-06 21:50:02 +08:00
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
def assertFormError(self, response, form, field, errors, msg_prefix=''):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that a form used to render the response has a specific field
|
|
|
|
error.
|
|
|
|
"""
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2007-09-04 07:14:51 +08:00
|
|
|
# Put context(s) into a list to simplify processing.
|
2007-09-04 08:50:06 +08:00
|
|
|
contexts = to_list(response.context)
|
2007-09-04 07:14:51 +08:00
|
|
|
if not contexts:
|
2010-02-13 19:59:09 +08:00
|
|
|
self.fail(msg_prefix + "Response did not use any contexts to "
|
2010-01-22 23:02:02 +08:00
|
|
|
"render the response")
|
2007-05-07 20:34:18 +08:00
|
|
|
|
2007-09-04 07:14:51 +08:00
|
|
|
# Put error(s) into a list to simplify processing.
|
|
|
|
errors = to_list(errors)
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2007-05-07 20:34:18 +08:00
|
|
|
# Search all contexts for the error.
|
|
|
|
found_form = False
|
|
|
|
for i,context in enumerate(contexts):
|
2007-09-04 08:50:06 +08:00
|
|
|
if form not in context:
|
|
|
|
continue
|
|
|
|
found_form = True
|
|
|
|
for err in errors:
|
|
|
|
if field:
|
|
|
|
if field in context[form].errors:
|
|
|
|
field_errors = context[form].errors[field]
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(err in field_errors,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "The field '%s' on form '%s' in"
|
|
|
|
" context %d does not contain the error '%s'"
|
|
|
|
" (actual errors: %s)" %
|
|
|
|
(field, form, i, err, repr(field_errors)))
|
2007-09-04 08:50:06 +08:00
|
|
|
elif field in context[form].fields:
|
2010-01-22 23:02:02 +08:00
|
|
|
self.fail(msg_prefix + "The field '%s' on form '%s'"
|
|
|
|
" in context %d contains no errors" %
|
|
|
|
(field, form, i))
|
2007-05-10 21:48:18 +08:00
|
|
|
else:
|
2010-01-22 23:02:02 +08:00
|
|
|
self.fail(msg_prefix + "The form '%s' in context %d"
|
|
|
|
" does not contain the field '%s'" %
|
2007-09-04 08:50:06 +08:00
|
|
|
(form, i, field))
|
|
|
|
else:
|
|
|
|
non_field_errors = context[form].non_field_errors()
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(err in non_field_errors,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "The form '%s' in context %d does not"
|
|
|
|
" contain the non-field error '%s'"
|
|
|
|
" (actual errors: %s)" %
|
2007-09-04 08:50:06 +08:00
|
|
|
(form, i, err, non_field_errors))
|
2007-05-07 20:34:18 +08:00
|
|
|
if not found_form:
|
2010-01-22 23:02:02 +08:00
|
|
|
self.fail(msg_prefix + "The form '%s' was not used to render the"
|
|
|
|
" response" % form)
|
2007-09-04 08:50:06 +08:00
|
|
|
|
2012-02-01 03:23:09 +08:00
|
|
|
def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that the template with the provided name was used in rendering
|
2012-02-04 04:45:45 +08:00
|
|
|
the response. Also usable as context manager.
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
2012-02-01 03:23:09 +08:00
|
|
|
if response is None and template_name is None:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise TypeError('response and/or template_name argument must be provided')
|
2012-02-01 03:23:09 +08:00
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2012-02-04 04:45:45 +08:00
|
|
|
# Use assertTemplateUsed as context manager.
|
2012-02-01 03:23:09 +08:00
|
|
|
if not hasattr(response, 'templates') or (response is None and template_name):
|
|
|
|
if response:
|
|
|
|
template_name = response
|
|
|
|
response = None
|
|
|
|
context = _AssertTemplateUsedContext(self, template_name)
|
|
|
|
return context
|
|
|
|
|
2010-10-10 10:16:33 +08:00
|
|
|
template_names = [t.name for t in response.templates]
|
2007-09-04 07:14:51 +08:00
|
|
|
if not template_names:
|
2010-01-22 23:02:02 +08:00
|
|
|
self.fail(msg_prefix + "No templates used to render the response")
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(template_name in template_names,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Template '%s' was not a template used to render"
|
|
|
|
" the response. Actual template(s) used: %s" %
|
2012-06-08 00:08:47 +08:00
|
|
|
(template_name, ', '.join(template_names)))
|
2007-05-07 20:34:18 +08:00
|
|
|
|
2012-02-01 03:23:09 +08:00
|
|
|
def assertTemplateNotUsed(self, response=None, template_name=None, msg_prefix=''):
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
|
|
|
Asserts that the template with the provided name was NOT used in
|
2012-02-04 04:45:45 +08:00
|
|
|
rendering the response. Also usable as context manager.
|
2007-09-04 08:50:06 +08:00
|
|
|
"""
|
2012-02-01 03:23:09 +08:00
|
|
|
if response is None and template_name is None:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise TypeError('response and/or template_name argument must be provided')
|
2012-02-01 03:23:09 +08:00
|
|
|
|
2010-01-22 23:02:02 +08:00
|
|
|
if msg_prefix:
|
|
|
|
msg_prefix += ": "
|
|
|
|
|
2012-02-04 04:45:45 +08:00
|
|
|
# Use assertTemplateUsed as context manager.
|
2012-02-01 03:23:09 +08:00
|
|
|
if not hasattr(response, 'templates') or (response is None and template_name):
|
|
|
|
if response:
|
|
|
|
template_name = response
|
|
|
|
response = None
|
|
|
|
context = _AssertTemplateNotUsedContext(self, template_name)
|
|
|
|
return context
|
|
|
|
|
2010-10-10 10:16:33 +08:00
|
|
|
template_names = [t.name for t in response.templates]
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(template_name in template_names,
|
2010-01-22 23:02:02 +08:00
|
|
|
msg_prefix + "Template '%s' was used unexpectedly in rendering"
|
|
|
|
" the response" % template_name)
|
2009-01-16 10:30:22 +08:00
|
|
|
|
2011-08-23 11:38:18 +08:00
|
|
|
def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True):
|
2012-08-08 22:37:10 +08:00
|
|
|
items = six.moves.map(transform, qs)
|
2011-08-23 11:38:18 +08:00
|
|
|
if not ordered:
|
2012-08-08 22:37:10 +08:00
|
|
|
return self.assertEqual(set(items), set(values))
|
2012-12-13 19:33:11 +08:00
|
|
|
values = list(values)
|
|
|
|
# For example qs.iterator() could be passed as qs, but it does not
|
|
|
|
# have 'ordered' attribute.
|
|
|
|
if len(values) > 1 and hasattr(qs, 'ordered') and not qs.ordered:
|
|
|
|
raise ValueError("Trying to compare non-ordered queryset "
|
|
|
|
"against more than one ordered values")
|
2012-08-08 22:37:10 +08:00
|
|
|
return self.assertEqual(list(items), values)
|
2010-05-28 19:15:36 +08:00
|
|
|
|
2010-10-12 11:33:19 +08:00
|
|
|
def assertNumQueries(self, num, func=None, *args, **kwargs):
|
|
|
|
using = kwargs.pop("using", DEFAULT_DB_ALIAS)
|
2011-08-23 10:32:37 +08:00
|
|
|
conn = connections[using]
|
2010-10-12 11:33:19 +08:00
|
|
|
|
2011-08-23 10:32:37 +08:00
|
|
|
context = _AssertNumQueriesContext(self, num, conn)
|
2010-10-12 11:33:19 +08:00
|
|
|
if func is None:
|
|
|
|
return context
|
|
|
|
|
2011-03-28 13:58:43 +08:00
|
|
|
with context:
|
2010-10-12 11:33:19 +08:00
|
|
|
func(*args, **kwargs)
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def connections_support_transactions():
|
|
|
|
"""
|
2011-03-28 10:11:19 +08:00
|
|
|
Returns True if all connections support transactions.
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2011-10-05 20:50:44 +08:00
|
|
|
return all(conn.features.supports_transactions
|
|
|
|
for conn in connections.all())
|
2009-12-22 23:18:51 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2009-01-16 10:30:22 +08:00
|
|
|
class TestCase(TransactionTestCase):
|
|
|
|
"""
|
|
|
|
Does basically the same as TransactionTestCase, but surrounds every test
|
2011-10-05 20:50:44 +08:00
|
|
|
with a transaction, monkey-patches the real transaction management routines
|
|
|
|
to do nothing, and rollsback the test transaction at the end of the test.
|
|
|
|
You have to use TransactionTestCase, if you need transaction management
|
|
|
|
inside a test.
|
2009-01-16 10:30:22 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def _fixture_setup(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
if not connections_support_transactions():
|
2009-01-16 10:30:22 +08:00
|
|
|
return super(TestCase, self)._fixture_setup()
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2012-07-25 04:24:16 +08:00
|
|
|
assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'
|
|
|
|
|
2013-03-08 20:31:14 +08:00
|
|
|
self.atomics = {}
|
2012-11-24 16:48:21 +08:00
|
|
|
for db_name in self._databases_names():
|
2013-03-08 20:31:14 +08:00
|
|
|
self.atomics[db_name] = transaction.atomic(using=db_name)
|
|
|
|
self.atomics[db_name].__enter__()
|
|
|
|
# Remove this when the legacy transaction management goes away.
|
2009-01-16 10:30:22 +08:00
|
|
|
disable_transaction_methods()
|
|
|
|
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
Site.objects.clear_cache()
|
|
|
|
|
2012-11-24 16:48:21 +08:00
|
|
|
for db in self._databases_names(include_mirrors=False):
|
2009-12-22 23:18:51 +08:00
|
|
|
if hasattr(self, 'fixtures'):
|
2011-10-05 20:50:44 +08:00
|
|
|
call_command('loaddata', *self.fixtures,
|
|
|
|
**{
|
|
|
|
'verbosity': 0,
|
|
|
|
'commit': False,
|
2012-05-01 13:40:04 +08:00
|
|
|
'database': db,
|
|
|
|
'skip_validation': True,
|
2011-10-05 20:50:44 +08:00
|
|
|
})
|
2009-01-16 10:30:22 +08:00
|
|
|
|
|
|
|
def _fixture_teardown(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
if not connections_support_transactions():
|
2009-01-16 10:30:22 +08:00
|
|
|
return super(TestCase, self)._fixture_teardown()
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2013-03-08 20:31:14 +08:00
|
|
|
# Remove this when the legacy transaction management goes away.
|
2009-01-16 10:30:22 +08:00
|
|
|
restore_transaction_methods()
|
2013-03-08 20:31:14 +08:00
|
|
|
for db_name in reversed(self._databases_names()):
|
|
|
|
# Hack to force a rollback
|
|
|
|
connections[db_name].needs_rollback = True
|
|
|
|
self.atomics[db_name].__exit__(None, None, None)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def _deferredSkip(condition, reason):
|
2010-10-17 12:26:47 +08:00
|
|
|
def decorator(test_func):
|
2011-10-05 20:50:44 +08:00
|
|
|
if not (isinstance(test_func, type) and
|
|
|
|
issubclass(test_func, TestCase)):
|
2010-10-17 12:26:47 +08:00
|
|
|
@wraps(test_func)
|
2010-10-11 20:55:17 +08:00
|
|
|
def skip_wrapper(*args, **kwargs):
|
|
|
|
if condition():
|
2010-10-18 23:53:55 +08:00
|
|
|
raise ut2.SkipTest(reason)
|
2010-10-17 12:26:47 +08:00
|
|
|
return test_func(*args, **kwargs)
|
2010-10-11 20:55:17 +08:00
|
|
|
test_item = skip_wrapper
|
2010-10-17 12:26:47 +08:00
|
|
|
else:
|
|
|
|
test_item = test_func
|
2010-10-11 20:55:17 +08:00
|
|
|
test_item.__unittest_skip_why__ = reason
|
|
|
|
return test_item
|
|
|
|
return decorator
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def skipIfDBFeature(feature):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
|
|
|
Skip a test if a database has the named feature
|
|
|
|
"""
|
2010-10-11 20:55:17 +08:00
|
|
|
return _deferredSkip(lambda: getattr(connection.features, feature),
|
|
|
|
"Database has feature %s" % feature)
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def skipUnlessDBFeature(feature):
|
2011-10-05 20:50:44 +08:00
|
|
|
"""
|
|
|
|
Skip a test unless a database has the named feature
|
|
|
|
"""
|
2010-10-11 20:55:17 +08:00
|
|
|
return _deferredSkip(lambda: not getattr(connection.features, feature),
|
|
|
|
"Database doesn't support feature %s" % feature)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class QuietWSGIRequestHandler(WSGIRequestHandler):
|
|
|
|
"""
|
|
|
|
Just a regular WSGIRequestHandler except it doesn't log to the standard
|
|
|
|
output any of the requests received, so as to not clutter the output for
|
|
|
|
the tests' results.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def log_message(*args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-10-11 01:57:16 +08:00
|
|
|
if sys.version_info >= (3, 3, 0):
|
|
|
|
_ImprovedEvent = threading.Event
|
|
|
|
elif sys.version_info >= (2, 7, 0):
|
2012-08-16 15:56:42 +08:00
|
|
|
_ImprovedEvent = threading._Event
|
|
|
|
else:
|
|
|
|
class _ImprovedEvent(threading._Event):
|
|
|
|
"""
|
|
|
|
Does the same as `threading.Event` except it overrides the wait() method
|
|
|
|
with some code borrowed from Python 2.7 to return the set state of the
|
|
|
|
event (see: http://hg.python.org/cpython/rev/b5aa8aa78c0f/). This allows
|
|
|
|
to know whether the wait() method exited normally or because of the
|
|
|
|
timeout. This class can be removed when Django supports only Python >= 2.7.
|
|
|
|
"""
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2012-08-16 15:56:42 +08:00
|
|
|
def wait(self, timeout=None):
|
|
|
|
self._Event__cond.acquire()
|
|
|
|
try:
|
|
|
|
if not self._Event__flag:
|
|
|
|
self._Event__cond.wait(timeout)
|
|
|
|
return self._Event__flag
|
|
|
|
finally:
|
|
|
|
self._Event__cond.release()
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StoppableWSGIServer(WSGIServer):
|
|
|
|
"""
|
|
|
|
The code in this class is borrowed from the `SocketServer.BaseServer` class
|
|
|
|
in Python 2.6. The important functionality here is that the server is non-
|
|
|
|
blocking and that it can be shut down at any moment. This is made possible
|
|
|
|
by the server regularly polling the socket and checking if it has been
|
|
|
|
asked to stop.
|
|
|
|
Note for the future: Once Django stops supporting Python 2.6, this class
|
|
|
|
can be removed as `WSGIServer` will have this ability to shutdown on
|
|
|
|
demand and will not require the use of the _ImprovedEvent class whose code
|
|
|
|
is borrowed from Python 2.7.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(StoppableWSGIServer, self).__init__(*args, **kwargs)
|
|
|
|
self.__is_shut_down = _ImprovedEvent()
|
|
|
|
self.__serving = False
|
|
|
|
|
|
|
|
def serve_forever(self, poll_interval=0.5):
|
|
|
|
"""
|
|
|
|
Handle one request at a time until shutdown.
|
|
|
|
|
|
|
|
Polls for shutdown every poll_interval seconds.
|
|
|
|
"""
|
|
|
|
self.__serving = True
|
|
|
|
self.__is_shut_down.clear()
|
|
|
|
while self.__serving:
|
|
|
|
r, w, e = select.select([self], [], [], poll_interval)
|
|
|
|
if r:
|
|
|
|
self._handle_request_noblock()
|
|
|
|
self.__is_shut_down.set()
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
"""
|
|
|
|
Stops the serve_forever loop.
|
|
|
|
|
|
|
|
Blocks until the loop has finished. This must be called while
|
|
|
|
serve_forever() is running in another thread, or it will
|
|
|
|
deadlock.
|
|
|
|
"""
|
|
|
|
self.__serving = False
|
|
|
|
if not self.__is_shut_down.wait(2):
|
|
|
|
raise RuntimeError(
|
|
|
|
"Failed to shutdown the live test server in 2 seconds. The "
|
|
|
|
"server might be stuck or generating a slow response.")
|
|
|
|
|
|
|
|
def handle_request(self):
|
|
|
|
"""Handle one request, possibly blocking.
|
|
|
|
"""
|
|
|
|
fd_sets = select.select([self], [], [], None)
|
|
|
|
if not fd_sets[0]:
|
|
|
|
return
|
|
|
|
self._handle_request_noblock()
|
|
|
|
|
|
|
|
def _handle_request_noblock(self):
|
|
|
|
"""
|
|
|
|
Handle one request, without blocking.
|
|
|
|
|
|
|
|
I assume that select.select has returned that the socket is
|
|
|
|
readable before this function was called, so there should be
|
|
|
|
no risk of blocking in get_request().
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
request, client_address = self.get_request()
|
|
|
|
except socket.error:
|
|
|
|
return
|
|
|
|
if self.verify_request(request, client_address):
|
|
|
|
try:
|
|
|
|
self.process_request(request, client_address)
|
|
|
|
except Exception:
|
|
|
|
self.handle_error(request, client_address)
|
|
|
|
self.close_request(request)
|
|
|
|
|
|
|
|
|
|
|
|
class _MediaFilesHandler(StaticFilesHandler):
|
|
|
|
"""
|
|
|
|
Handler for serving the media files. This is a private class that is
|
|
|
|
meant to be used solely as a convenience by LiveServerThread.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_base_dir(self):
|
|
|
|
return settings.MEDIA_ROOT
|
|
|
|
|
|
|
|
def get_base_url(self):
|
|
|
|
return settings.MEDIA_URL
|
|
|
|
|
|
|
|
def serve(self, request):
|
2012-02-02 05:36:18 +08:00
|
|
|
relative_url = request.path[len(self.base_url[2]):]
|
|
|
|
return serve(request, relative_url, document_root=self.get_base_dir())
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LiveServerThread(threading.Thread):
|
|
|
|
"""
|
|
|
|
Thread for running a live http server while the tests are running.
|
|
|
|
"""
|
|
|
|
|
2011-12-30 04:22:13 +08:00
|
|
|
def __init__(self, host, possible_ports, connections_override=None):
|
|
|
|
self.host = host
|
|
|
|
self.port = None
|
|
|
|
self.possible_ports = possible_ports
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
self.is_ready = threading.Event()
|
|
|
|
self.error = None
|
|
|
|
self.connections_override = connections_override
|
|
|
|
super(LiveServerThread, self).__init__()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Sets up the live server and databases, and then loops over handling
|
|
|
|
http requests.
|
|
|
|
"""
|
|
|
|
if self.connections_override:
|
|
|
|
# Override this thread's database connections with the ones
|
|
|
|
# provided by the main thread.
|
|
|
|
for alias, conn in self.connections_override.items():
|
|
|
|
connections[alias] = conn
|
|
|
|
try:
|
|
|
|
# Create the handler for serving static and media files
|
|
|
|
handler = StaticFilesHandler(_MediaFilesHandler(WSGIHandler()))
|
2011-12-30 04:22:13 +08:00
|
|
|
|
|
|
|
# Go through the list of possible ports, hoping that we can find
|
|
|
|
# one that is free to use for the WSGI server.
|
|
|
|
for index, port in enumerate(self.possible_ports):
|
|
|
|
try:
|
|
|
|
self.httpd = StoppableWSGIServer(
|
|
|
|
(self.host, port), QuietWSGIRequestHandler)
|
2012-04-29 00:09:37 +08:00
|
|
|
except WSGIServerException as e:
|
2011-12-30 04:22:13 +08:00
|
|
|
if (index + 1 < len(self.possible_ports) and
|
2012-11-25 19:55:23 +08:00
|
|
|
hasattr(e.args[0], 'errno') and
|
2012-03-30 17:20:04 +08:00
|
|
|
e.args[0].errno == errno.EADDRINUSE):
|
2011-12-30 04:22:13 +08:00
|
|
|
# This port is already in use, so we go on and try with
|
|
|
|
# the next one in the list.
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Either none of the given ports are free or the error
|
|
|
|
# is something else than "Address already in use". So
|
|
|
|
# we let that error bubble up to the main thread.
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
# A free port was found.
|
|
|
|
self.port = port
|
|
|
|
break
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
self.httpd.set_app(handler)
|
|
|
|
self.is_ready.set()
|
|
|
|
self.httpd.serve_forever()
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
self.error = e
|
|
|
|
self.is_ready.set()
|
|
|
|
|
|
|
|
def join(self, timeout=None):
|
|
|
|
if hasattr(self, 'httpd'):
|
|
|
|
# Stop the WSGI server
|
|
|
|
self.httpd.shutdown()
|
|
|
|
self.httpd.server_close()
|
|
|
|
super(LiveServerThread, self).join(timeout)
|
|
|
|
|
|
|
|
|
|
|
|
class LiveServerTestCase(TransactionTestCase):
|
|
|
|
"""
|
|
|
|
Does basically the same as TransactionTestCase but also launches a live
|
|
|
|
http server in a separate thread so that the tests may use another testing
|
|
|
|
framework, such as Selenium for example, instead of the built-in dummy
|
|
|
|
client.
|
|
|
|
Note that it inherits from TransactionTestCase instead of TestCase because
|
|
|
|
the threads do not share the same transactions (unless if using in-memory
|
|
|
|
sqlite) and each thread needs to commit all their transactions so that the
|
|
|
|
other thread can see the changes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def live_server_url(self):
|
2011-12-30 04:22:13 +08:00
|
|
|
return 'http://%s:%s' % (
|
|
|
|
self.server_thread.host, self.server_thread.port)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
connections_override = {}
|
|
|
|
for conn in connections.all():
|
|
|
|
# If using in-memory sqlite databases, pass the connections to
|
|
|
|
# the server thread.
|
2012-11-16 21:25:45 +08:00
|
|
|
if (conn.settings_dict['ENGINE'].rsplit('.', 1)[-1] in ('sqlite3', 'spatialite')
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
and conn.settings_dict['NAME'] == ':memory:'):
|
|
|
|
# Explicitly enable thread-shareability for this connection
|
|
|
|
conn.allow_thread_sharing = True
|
|
|
|
connections_override[conn.alias] = conn
|
|
|
|
|
|
|
|
# Launch the live server's thread
|
2011-12-30 04:22:13 +08:00
|
|
|
specified_address = os.environ.get(
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
'DJANGO_LIVE_TEST_SERVER_ADDRESS', 'localhost:8081')
|
2011-12-30 04:22:13 +08:00
|
|
|
|
|
|
|
# The specified ports may be of the form '8000-8010,8080,9200-9300'
|
|
|
|
# i.e. a comma-separated list of ports or ranges of ports, so we break
|
|
|
|
# it down into a detailed list of all possible ports.
|
|
|
|
possible_ports = []
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
try:
|
2011-12-30 04:22:13 +08:00
|
|
|
host, port_ranges = specified_address.split(':')
|
|
|
|
for port_range in port_ranges.split(','):
|
|
|
|
# A port range can be of either form: '8000' or '8000-8010'.
|
2012-08-15 17:11:55 +08:00
|
|
|
extremes = list(map(int, port_range.split('-')))
|
2011-12-30 04:22:13 +08:00
|
|
|
assert len(extremes) in [1, 2]
|
|
|
|
if len(extremes) == 1:
|
|
|
|
# Port range of the form '8000'
|
|
|
|
possible_ports.append(extremes[0])
|
|
|
|
else:
|
|
|
|
# Port range of the form '8000-8010'
|
|
|
|
for port in range(extremes[0], extremes[1] + 1):
|
|
|
|
possible_ports.append(port)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
except Exception:
|
2013-03-19 13:04:59 +08:00
|
|
|
msg = 'Invalid address ("%s") for live server.' % specified_address
|
|
|
|
six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), sys.exc_info()[2])
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
cls.server_thread = LiveServerThread(
|
2011-12-30 04:22:13 +08:00
|
|
|
host, possible_ports, connections_override)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
cls.server_thread.daemon = True
|
|
|
|
cls.server_thread.start()
|
|
|
|
|
|
|
|
# Wait for the live server to be ready
|
|
|
|
cls.server_thread.is_ready.wait()
|
|
|
|
if cls.server_thread.error:
|
|
|
|
raise cls.server_thread.error
|
|
|
|
|
|
|
|
super(LiveServerTestCase, cls).setUpClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
# There may not be a 'server_thread' attribute if setUpClass() for some
|
|
|
|
# reasons has raised an exception.
|
|
|
|
if hasattr(cls, 'server_thread'):
|
|
|
|
# Terminate the live server's thread
|
|
|
|
cls.server_thread.join()
|
2012-07-22 07:16:47 +08:00
|
|
|
|
|
|
|
# Restore sqlite connections' non-sharability
|
|
|
|
for conn in connections.all():
|
2012-11-16 21:25:45 +08:00
|
|
|
if (conn.settings_dict['ENGINE'].rsplit('.', 1)[-1] in ('sqlite3', 'spatialite')
|
2012-07-22 07:16:47 +08:00
|
|
|
and conn.settings_dict['NAME'] == ':memory:'):
|
|
|
|
conn.allow_thread_sharing = False
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
super(LiveServerTestCase, cls).tearDownClass()
|