Skip to content

Commit 5e508a5

Browse files
committed
Update Test
1 parent 53f5527 commit 5e508a5

15 files changed

Lines changed: 208 additions & 18 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: python
22
python:
33
- "2.7"
4+
- "2.6"
45
install: "pip install -r requirements.txt --use-mirrors"
5-
script: python demo_app/manage.py validate
6+
script: "cd tests/ && python runtests.py"

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
author_email='[email protected]',
1414
url='http://www.xadmin.io',
1515
download_url='http://github.com/sshwsfc/django-xadmin/archive/master.zip',
16-
packages=['xadmin', 'xadmin.plugins', 'xadmin.templatetags',
17-
'xadmin.tests', 'xadmin.views'],
16+
packages=['xadmin', 'xadmin.plugins', 'xadmin.templatetags', 'xadmin.views'],
1817
include_package_data=True,
1918
install_requires=[
2019
'setuptools',

tests/__init__.py

Whitespace-only changes.

tests/runtests.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env python
2+
import os
3+
import shutil
4+
import sys
5+
import tempfile
6+
7+
TEST_ROOT = os.path.realpath(os.path.dirname(__file__))
8+
sys.path.insert(0, os.path.join(TEST_ROOT, os.pardir))
9+
10+
RUNTESTS_DIR = os.path.join(TEST_ROOT, 'xtests')
11+
TEST_TEMPLATE_DIR = 'templates'
12+
TEMP_DIR = tempfile.mkdtemp(prefix='django_')
13+
os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR
14+
15+
ALWAYS_INSTALLED_APPS = [
16+
'django.contrib.auth',
17+
'django.contrib.contenttypes',
18+
'django.contrib.sessions',
19+
'django.contrib.messages',
20+
'django.contrib.staticfiles',
21+
22+
'xadmin',
23+
'crispy_forms',
24+
'reversion',
25+
]
26+
27+
def get_test_modules():
28+
modules = []
29+
for f in os.listdir(RUNTESTS_DIR):
30+
if (f.startswith('__init__') or
31+
f.startswith('.') or
32+
f.startswith('sql')):
33+
continue
34+
modules.append(f)
35+
return modules
36+
37+
def setup(verbosity, test_labels):
38+
from django.conf import settings
39+
state = {
40+
'INSTALLED_APPS': settings.INSTALLED_APPS,
41+
'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""),
42+
'TEMPLATE_DIRS': settings.TEMPLATE_DIRS,
43+
'USE_I18N': settings.USE_I18N,
44+
'LOGIN_URL': settings.LOGIN_URL,
45+
'LANGUAGE_CODE': settings.LANGUAGE_CODE,
46+
'MIDDLEWARE_CLASSES': settings.MIDDLEWARE_CLASSES,
47+
'STATIC_URL': settings.STATIC_URL,
48+
'STATIC_ROOT': settings.STATIC_ROOT,
49+
}
50+
51+
# Redirect some settings for the duration of these tests.
52+
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
53+
settings.ROOT_URLCONF = 'urls'
54+
settings.STATIC_URL = '/static/'
55+
settings.STATIC_ROOT = os.path.join(TEMP_DIR, 'static')
56+
settings.TEMPLATE_DIRS = (os.path.join(RUNTESTS_DIR, TEST_TEMPLATE_DIR),)
57+
settings.USE_I18N = True
58+
settings.LANGUAGE_CODE = 'en'
59+
settings.MIDDLEWARE_CLASSES = (
60+
'django.contrib.sessions.middleware.SessionMiddleware',
61+
'django.contrib.auth.middleware.AuthenticationMiddleware',
62+
'django.contrib.messages.middleware.MessageMiddleware',
63+
'django.middleware.common.CommonMiddleware',
64+
)
65+
settings.SITE_ID = 1
66+
# For testing comment-utils, we require the MANAGERS attribute
67+
# to be set, so that a test email is sent out which we catch
68+
# in our tests.
69+
settings.MANAGERS = ("[email protected]",)
70+
71+
# Load all the ALWAYS_INSTALLED_APPS.
72+
# (This import statement is intentionally delayed until after we
73+
# access settings because of the USE_I18N dependency.)
74+
from django.db.models.loading import get_apps, load_app
75+
get_apps()
76+
77+
# Load all the test model apps.
78+
test_labels_set = set([label.split('.')[0] for label in test_labels])
79+
test_modules = get_test_modules()
80+
81+
for module_name in test_modules:
82+
module_label = '.'.join(['xtests', module_name])
83+
# if the module was named on the command line, or
84+
# no modules were named (i.e., run all), import
85+
# this module and add it to the list to test.
86+
if not test_labels or module_name in test_labels_set:
87+
if verbosity >= 2:
88+
print "Importing application %s" % module_name
89+
mod = load_app(module_label)
90+
if mod:
91+
if module_label not in settings.INSTALLED_APPS:
92+
settings.INSTALLED_APPS.append(module_label)
93+
94+
return state
95+
96+
def teardown(state):
97+
from django.conf import settings
98+
# Removing the temporary TEMP_DIR. Ensure we pass in unicode
99+
# so that it will successfully remove temp trees containing
100+
# non-ASCII filenames on Windows. (We're assuming the temp dir
101+
# name itself does not contain non-ASCII characters.)
102+
shutil.rmtree(unicode(TEMP_DIR))
103+
# Restore the old settings.
104+
for key, value in state.items():
105+
setattr(settings, key, value)
106+
107+
def django_tests(verbosity, interactive, failfast, test_labels):
108+
from django.conf import settings
109+
state = setup(verbosity, test_labels)
110+
extra_tests = []
111+
112+
# Run the test suite, including the extra validation tests.
113+
from django.test.utils import get_runner
114+
if not hasattr(settings, 'TEST_RUNNER'):
115+
settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
116+
TestRunner = get_runner(settings)
117+
118+
test_runner = TestRunner(verbosity=verbosity, interactive=interactive,
119+
failfast=failfast)
120+
failures = test_runner.run_tests(test_labels or get_test_modules(), extra_tests=extra_tests)
121+
122+
teardown(state)
123+
return failures
124+
125+
if __name__ == "__main__":
126+
from optparse import OptionParser
127+
usage = "%prog [options] [module module module ...]"
128+
parser = OptionParser(usage=usage)
129+
parser.add_option(
130+
'-v','--verbosity', action='store', dest='verbosity', default='1',
131+
type='choice', choices=['0', '1', '2', '3'],
132+
help='Verbosity level; 0=minimal output, 1=normal output, 2=all '
133+
'output')
134+
parser.add_option(
135+
'--noinput', action='store_false', dest='interactive', default=True,
136+
help='Tells Django to NOT prompt the user for input of any kind.')
137+
parser.add_option(
138+
'--failfast', action='store_true', dest='failfast', default=False,
139+
help='Tells Django to stop running the test suite after first failed '
140+
'test.')
141+
parser.add_option(
142+
'--settings',
143+
help='Python path to settings module, e.g. "myproject.settings". If '
144+
'this isn\'t provided, the DJANGO_SETTINGS_MODULE environment '
145+
'variable will be used.')
146+
parser.add_option(
147+
'--liveserver', action='store', dest='liveserver', default=None,
148+
help='Overrides the default address where the live server (used with '
149+
'LiveServerTestCase) is expected to run from. The default value '
150+
'is localhost:8081.'),
151+
options, args = parser.parse_args()
152+
if options.settings:
153+
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
154+
elif "DJANGO_SETTINGS_MODULE" not in os.environ:
155+
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
156+
else:
157+
options.settings = os.environ['DJANGO_SETTINGS_MODULE']
158+
159+
if options.liveserver is not None:
160+
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = options.liveserver
161+
162+
failures = django_tests(int(options.verbosity), options.interactive,
163+
options.failfast, args)
164+
if failures:
165+
sys.exit(bool(failures))

tests/settings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DEBUG = True
2+
DATABASES = {
3+
'default': {
4+
'ENGINE': 'django.db.backends.sqlite3',
5+
}
6+
}
7+
8+
# Required for Django 1.4+
9+
STATIC_URL = '/static/'
10+
11+
# Required for Django 1.5+
12+
SECRET_KEY = 'abc123'

tests/xtests/__init__.py

Whitespace-only changes.

tests/xtests/site/__init__.py

Whitespace-only changes.

tests/xtests/site/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
4+
class ModelA(models.Model):
5+
name = models.CharField(max_length=64)
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from xadmin.tests.base import TestCase
1+
from django.test import TestCase
22
from django.http import HttpResponse
3+
from django.contrib.auth.models import User
4+
from django.test.client import RequestFactory
35

46
from xadmin.sites import AdminSite
57
from xadmin.views import BaseAdminView, BaseAdminPlugin, ModelAdminView, filter_hook
@@ -40,6 +42,18 @@ def get(self, request, obj_id):
4042

4143
class AdminSiteTest(TestCase):
4244

45+
def setUp(self):
46+
# Every test needs access to the request factory.
47+
self.factory = RequestFactory()
48+
49+
def _create_superuser(self, username):
50+
return User.objects.create(username=username, is_superuser=True)
51+
52+
def _mocked_authenticated_request(self, url, user):
53+
request = self.factory.get(url)
54+
request.user = user
55+
return request
56+
4357
def get_site(self):
4458
return AdminSite('test', 'test_app')
4559

@@ -76,7 +90,8 @@ def test_plugin(self):
7690
c = site.get_view_class(TestAdminView)
7791
self.assertIn(TestPlugin, c.plugin_classes)
7892

79-
cv = c(self.get_factory().get('test/'))
93+
admin = self._create_superuser('admin')
94+
cv = c(self._mocked_authenticated_request('test/', admin))
8095

8196
self.assertEqual(cv.get_title(), "TEST TITLE PLUGIN")
8297

tests/xtests/views/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)