Permalink
Browse files

Refactored javascript test module.

* Only one view, django.test.javascript.views.JavascriptTestRunner
* Combined templates.
* Added jstest management command to run a simple js test server.
* Moved example tests to auth app.
  • Loading branch information...
1 parent ef05804 commit 9c566eb3b95096dae395cd10aedf1a10fafa027c @jezdez jezdez committed with Apr 3, 2011
@@ -0,0 +1,24 @@
+from optparse import make_option
+
+from django.conf import settings
+from django.core.handlers.wsgi import WSGIHandler
+from django.core.management.commands.runserver import BaseRunserverCommand
+from django.http import Http404
+from django.views import debug
+
+from django.test.javascript.views import templates_path
+
+
+class JavascriptTestHandler(WSGIHandler):
+
+ def get_response(self, request):
+ request.urlconf = "django.test.javascript.urls"
+ return super(JavascriptTestHandler, self).get_response(request)
+
+
+class Command(BaseRunserverCommand):
+ help = 'Runs the Javascript test runner'
+
+ def get_handler(self, *args, **options):
+ settings.TEMPLATE_DIRS = list(settings.TEMPLATE_DIRS) + [templates_path]
+ return JavascriptTestHandler(*args)
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>QUnit Test Suite</title>
+ <link rel="stylesheet" href="{{ linkbase }}static/qunit/qunit.css" type="text/css" media="screen">
+ <script type="text/javascript" src="/static/qunit/qunit.js"></script>
+ {% for remote_url in suite.remote_urls %}<script type="text/javascript" src="{{ remote_url }}"></script>
+ {% endfor %}
+ {% for local_url in suite.local_urls %}<script type="text/javascript" src="{{ STATIC_URL }}{{ local_url }}"></script>
+ {% endfor %}
+ {% for file in files %}<script type="text/javascript" src="{{ file }}"></script>
+ {% endfor %}
+</head>
+
+<body>
+ <h1 id="qunit-header">QUnit Test Suite {% if suite %}({{ suite.name }}){% endif %}</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar">
+ <div id="navigation">
+ {% if suites %}
+ {% for label, options in suites.items %}
+ <a href="{{ linkbase }}{{ label }}/">{{ options.name }}</a>
+ {% endfor %}
+ {% endif %}
+ <p>
+ <a href="..">..</a>
+ {% for suite in subsuites %}
+ <a href="{{ suite }}/">{{ suite }}</a>
+ {% endfor %}
+ </p>
+
+ </div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">{% if suite.fixture %}{{ fixture }}{% endif %}</div>
+</body>
+</html>
@@ -1,16 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>QUnit Test Suite</title>
- <link rel="stylesheet" href="{{ STATIC_URL }}qunit/qunit.css" type="text/css" media="screen">
- <script type="text/javascript" src="{{ STATIC_URL }}qunit/qunit.js"></script>
- {% block extrahead %}{% endblock extrahead %}
-</head>
-
-<body>
- <h1 id="qunit-header">QUnit Test Suite {% block title %}{% endblock title %}</h1>
- <h2 id="qunit-banner">{% block banner %}{% endblock banner %}</h2>
- <div id="qunit-testrunner-toolbar">{% block toolbar %}{% endblock toolbar %}</div>
- {% block content %}{% endblock content %}
-</body>
-</html>
@@ -1,17 +0,0 @@
-{% extends "qunit/base.html" %}
-
-{% block title %}Overview{% endblock title %}
-
-{% block banner %}{% endblock banner %}
-
-{% block toolbar %}{% endblock toolbar %}
-
-{% block content %}
- {% if suites %}
- <div id="navigation">
- {% for label, options in suites.items %}
- <a href="{{ label }}/">{{ options.name }}</a>
- {% endfor %}
- </div>
- {% endif %}
-{% endblock content %}
@@ -1,24 +0,0 @@
-{% extends "qunit/base.html" %}
-{% load url from future %}
-
-{% block extrahead %}
- {% for remote_url in suite.remote_urls %}<script type="text/javascript" src="{{ remote_url }}"></script>
- {% endfor %}
- {% for local_url in suite.local_urls %}<script type="text/javascript" src="{{ STATIC_URL }}{{ local_url }}"></script>
- {% endfor %}
- {% for file in files %}<script type="text/javascript" src="{{ file }}"></script>
- {% endfor %}
-{% endblock extrahead %}
-
-{% block title %}({{ suite.name }}){% endblock title %}
-
-{% block content %}
- <div id="navigation">
- <a href="..">..</a>
- {% for suite in subsuites %}
- <a href="{{ suite }}/">{{ suite }}</a>
- {% endfor %}
- </div>
- <h2 id="qunit-userAgent"></h2>
- <ol id="qunit-tests"></ol>
-{% endblock content %}
@@ -1,8 +1,10 @@
from django.conf.urls.defaults import url, patterns
-from django.contrib.qunit.views import QUnitOverview, QUnitRunner
+from django.test.javascript.views import JavascriptTestRunner, static_path
+
urlpatterns = patterns('',
- url('^$', QUnitOverview.as_view(), name='qunit-test-overview'),
- url('^(?P<label>[\w]+)/(?P<path>.*)$', QUnitRunner.as_view(), name='qunit-test-runner'),
+ url('^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': static_path}, name='javascript-static'),
+ url('^$', JavascriptTestRunner.as_view(), name='javascript-test-overview'),
+ url('^(?P<label>[\w]+)/(?P<path>.*)$', JavascriptTestRunner.as_view(), name='javascript-test-runner'),
)
@@ -5,70 +5,70 @@
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.http import Http404
-from django.utils import simplejson
-from django.utils.datastructures import SortedDict
+from django.utils import simplejson, datastructures
from django.utils.importlib import import_module
from django.views import generic, static
+from django.core.urlresolvers import reverse
+thisdir = os.path.dirname(__file__)
+static_path = os.path.join(thisdir, 'static')
+templates_path = os.path.join(thisdir, 'templates')
-class QUnitOverview(generic.TemplateView):
+
+class JavascriptTestRunner(generic.TemplateView):
manifest = 'suite.json'
- template_name = 'qunit/overview.html'
+ template_name = 'javascript/runner.html'
def __init__(self, *args, **kwargs):
- super(QUnitOverview, self).__init__(*args, **kwargs)
- self.suites = self.get_suites(*args, **kwargs)
-
- def get_suites(self, **kwargs):
- suites = SortedDict()
+ super(JavascriptTestRunner, self).__init__(*args, **kwargs)
+ self.suites = datastructures.SortedDict()
for app_name in settings.INSTALLED_APPS:
mod = import_module(app_name)
mod_path = os.path.dirname(mod.__file__)
label = app_name.split('.')[-1]
- tests_path = os.path.join(mod_path, 'static', label, 'tests')
- if os.path.isfile(os.path.join(tests_path, self.manifest)):
- suites[label] = {
+ tests_path = os.path.join(mod_path, 'tests', 'javascript')
+ if os.path.isdir(tests_path):
+ self.suites[label] = {
"local_urls": [],
"remote_urls": [],
"name": app_name,
"tests_path": tests_path,
}
- return suites
-
- def get_context_data(self, **kwargs):
- context = super(QUnitOverview, self).get_context_data(**kwargs)
- context["suites"] = self.suites
- return context
-
-
-class QUnitRunner(QUnitOverview):
- template_name = 'qunit/runner.html'
def get(self, request, label=None, path=None, *args, **kwargs):
"""
"""
- if label not in self.suites:
- raise Http404("No test suite found with label '%s'" % label)
- suite = self.suites.get(label)
- full_path = os.path.join(suite['tests_path'], path)
- if os.path.isfile(full_path):
- directory, filename = os.path.split(full_path)
- return static.serve(request, path=filename, document_root=directory)
- storage = FileSystemStorage(location=full_path, base_url="")
- context = self.get_context_data(suite, storage, **kwargs)
+ if label is not None:
+ suite = self.suites.get(label)
+ if suite is None:
+ raise Http404("No test suite found with label '%s'" % label)
+ full_path = os.path.join(suite['tests_path'], path)
+ if os.path.isfile(full_path):
+ dir, filename = os.path.split(full_path)
+ return static.serve(request, path=filename, document_root=dir)
+ context = self.get_context_data(suite, full_path, *args, **kwargs)
+ else:
+ context = self.get_context_data(*args, **kwargs)
return self.render_to_response(context)
- def get_context_data(self, suite, storage, **kwargs):
- context = super(QUnitRunner, self).get_context_data(**kwargs)
- # load suite.json if present
- if storage.exists(self.manifest):
- with storage.open(self.manifest) as json:
- suite.update(simplejson.loads(json.read()))
- directories, files = storage.listdir('.')
+ def get_context_data(self, suite=None, full_path=None, **kwargs):
+ context = super(JavascriptTestRunner, self).get_context_data(**kwargs)
context.update({
- 'suite': suite,
- 'subsuites': directories,
- 'files': [file for file in files if file.endswith('js')],
+ 'suites': self.suites,
+ 'linkbase': reverse('javascript-test-overview'),
})
+ if full_path:
+ storage = FileSystemStorage(location=full_path)
+ # load suite.json if present
+ if storage.exists(self.manifest):
+ with storage.open(self.manifest) as json:
+ suite.update(simplejson.loads(json.read()))
+
+ directories, files = storage.listdir('.')
+ context.update({
+ 'suite': suite,
+ 'subsuites': directories,
+ 'files': [file for file in files if file.endswith('js')],
+ })
return context
View
@@ -32,7 +32,4 @@
# test util views
(r'test_utils/', include('regressiontests.test_utils.urls')),
-
- # qunit tests
- ('^qunit/', include('django.contrib.qunit.urls')),
)

0 comments on commit 9c566eb

Please sign in to comment.