Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 9c566eb3b95096dae395cd10aedf1a10fafa027c 1 parent ef05804
Jannis Leidel authored April 03, 2011 sebleier committed January 09, 2012

Showing 17 changed files with 107 additions and 104 deletions. Show diff stats Hide diff stats

  1. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/async_test.js
  2. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite.json
  3. 0  .../{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite1/subsuite/subsuite_test.js
  4. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite1/test-a.js
  5. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite1/test-b.js
  6. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite2/test-c.js
  7. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/suite2/test-d.js
  8. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/test1.js
  9. 0  django/{test/javascript/static/qunit/tests → contrib/auth/tests/javascript}/test2.js
  10. 24  django/core/management/commands/jstest.py
  11. 37  django/test/javascript/templates/javascript/runner.html
  12. 16  django/test/javascript/templates/qunit/base.html
  13. 17  django/test/javascript/templates/qunit/overview.html
  14. 24  django/test/javascript/templates/qunit/runner.html
  15. 8  django/test/javascript/urls.py
  16. 82  django/test/javascript/views.py
  17. 3  tests/urls.py
0  .../test/javascript/static/qunit/tests/async_test.js → django/contrib/auth/tests/javascript/async_test.js
File renamed without changes
0  django/test/javascript/static/qunit/tests/suite.json → django/contrib/auth/tests/javascript/suite.json
File renamed without changes
0  ...atic/qunit/tests/suite1/subsuite/subsuite_test.js → ...tests/javascript/suite1/subsuite/subsuite_test.js
File renamed without changes
0  ...st/javascript/static/qunit/tests/suite1/test-a.js → ...go/contrib/auth/tests/javascript/suite1/test-a.js
File renamed without changes
0  ...st/javascript/static/qunit/tests/suite1/test-b.js → ...go/contrib/auth/tests/javascript/suite1/test-b.js
File renamed without changes
0  ...st/javascript/static/qunit/tests/suite2/test-c.js → ...go/contrib/auth/tests/javascript/suite2/test-c.js
File renamed without changes
0  ...st/javascript/static/qunit/tests/suite2/test-d.js → ...go/contrib/auth/tests/javascript/suite2/test-d.js
File renamed without changes
0  django/test/javascript/static/qunit/tests/test1.js → django/contrib/auth/tests/javascript/test1.js
File renamed without changes
0  django/test/javascript/static/qunit/tests/test2.js → django/contrib/auth/tests/javascript/test2.js
File renamed without changes
24  django/core/management/commands/jstest.py
... ...
@@ -0,0 +1,24 @@
  1
+from optparse import make_option
  2
+
  3
+from django.conf import settings
  4
+from django.core.handlers.wsgi import WSGIHandler
  5
+from django.core.management.commands.runserver import BaseRunserverCommand
  6
+from django.http import Http404
  7
+from django.views import debug
  8
+
  9
+from django.test.javascript.views import templates_path
  10
+
  11
+
  12
+class JavascriptTestHandler(WSGIHandler):
  13
+
  14
+    def get_response(self, request):
  15
+        request.urlconf = "django.test.javascript.urls"
  16
+        return super(JavascriptTestHandler, self).get_response(request)
  17
+
  18
+
  19
+class Command(BaseRunserverCommand):
  20
+    help = 'Runs the Javascript test runner'
  21
+
  22
+    def get_handler(self, *args, **options):
  23
+        settings.TEMPLATE_DIRS = list(settings.TEMPLATE_DIRS) + [templates_path]
  24
+        return JavascriptTestHandler(*args)
37  django/test/javascript/templates/javascript/runner.html
... ...
@@ -0,0 +1,37 @@
  1
+<!DOCTYPE html>
  2
+<html>
  3
+<head>
  4
+  <title>QUnit Test Suite</title>
  5
+  <link rel="stylesheet" href="{{ linkbase }}static/qunit/qunit.css" type="text/css" media="screen">
  6
+  <script type="text/javascript" src="/static/qunit/qunit.js"></script>
  7
+  {% for remote_url in suite.remote_urls %}<script type="text/javascript" src="{{ remote_url }}"></script>
  8
+  {% endfor %}
  9
+  {% for local_url in suite.local_urls %}<script type="text/javascript" src="{{ STATIC_URL }}{{ local_url }}"></script>
  10
+  {% endfor %}
  11
+  {% for file in files %}<script type="text/javascript" src="{{ file }}"></script>
  12
+  {% endfor %}
  13
+</head>
  14
+
  15
+<body>
  16
+  <h1 id="qunit-header">QUnit Test Suite {% if suite %}({{ suite.name }}){% endif %}</h1>
  17
+  <h2 id="qunit-banner"></h2>
  18
+  <div id="qunit-testrunner-toolbar">
  19
+      <div id="navigation">
  20
+      {% if suites %}
  21
+        {% for label, options in suites.items %}
  22
+        <a href="{{ linkbase }}{{ label }}/">{{ options.name }}</a>
  23
+        {% endfor %}
  24
+      {% endif %}
  25
+      <p>
  26
+      <a href="..">..</a>
  27
+        {% for suite in subsuites %}
  28
+        <a href="{{ suite }}/">{{ suite }}</a>
  29
+        {% endfor %}
  30
+      </p>
  31
+    
  32
+  </div>
  33
+  <h2 id="qunit-userAgent"></h2>
  34
+  <ol id="qunit-tests"></ol>
  35
+  <div id="qunit-fixture">{% if suite.fixture %}{{ fixture }}{% endif %}</div>
  36
+</body>
  37
+</html>
16  django/test/javascript/templates/qunit/base.html
... ...
@@ -1,16 +0,0 @@
1  
-<!DOCTYPE html>
2  
-<html>
3  
-<head>
4  
-  <title>QUnit Test Suite</title>
5  
-  <link rel="stylesheet" href="{{ STATIC_URL }}qunit/qunit.css" type="text/css" media="screen">
6  
-  <script type="text/javascript" src="{{ STATIC_URL }}qunit/qunit.js"></script>
7  
-  {% block extrahead %}{% endblock extrahead %}
8  
-</head>
9  
-
10  
-<body>
11  
-  <h1 id="qunit-header">QUnit Test Suite {% block title %}{% endblock title %}</h1>
12  
-  <h2 id="qunit-banner">{% block banner %}{% endblock banner %}</h2>
13  
-  <div id="qunit-testrunner-toolbar">{% block toolbar %}{% endblock toolbar %}</div>
14  
-  {% block content %}{% endblock content %}
15  
-</body>
16  
-</html>
17  django/test/javascript/templates/qunit/overview.html
... ...
@@ -1,17 +0,0 @@
1  
-{% extends "qunit/base.html" %}
2  
-
3  
-{% block title %}Overview{% endblock title %}
4  
-
5  
-{% block banner %}{% endblock banner %}
6  
-
7  
-{% block toolbar %}{% endblock toolbar %}
8  
-
9  
-{% block content %}
10  
-  {% if suites %}
11  
-  <div id="navigation">
12  
-    {% for label, options in suites.items %}
13  
-    <a href="{{ label }}/">{{ options.name }}</a>
14  
-    {% endfor %}
15  
-  </div>
16  
-  {% endif %}
17  
-{% endblock content %}
24  django/test/javascript/templates/qunit/runner.html
... ...
@@ -1,24 +0,0 @@
1  
-{% extends "qunit/base.html" %}
2  
-{% load url from future %}
3  
-
4  
-{% block extrahead %}
5  
-  {% for remote_url in suite.remote_urls %}<script type="text/javascript" src="{{ remote_url }}"></script>
6  
-  {% endfor %}
7  
-  {% for local_url in suite.local_urls %}<script type="text/javascript" src="{{ STATIC_URL }}{{ local_url }}"></script>
8  
-  {% endfor %}
9  
-  {% for file in files %}<script type="text/javascript" src="{{ file }}"></script>
10  
-  {% endfor %}
11  
-{% endblock extrahead %}
12  
-
13  
-{% block title %}({{ suite.name }}){% endblock title %}
14  
-
15  
-{% block content %}
16  
-  <div id="navigation">
17  
-    <a href="..">..</a>
18  
-    {% for suite in subsuites %}
19  
-    <a href="{{ suite }}/">{{ suite }}</a>
20  
-    {% endfor %}
21  
-  </div>
22  
-  <h2 id="qunit-userAgent"></h2>
23  
-  <ol id="qunit-tests"></ol>
24  
-{% endblock content %}
8  django/test/javascript/urls.py
... ...
@@ -1,8 +1,10 @@
1 1
 from django.conf.urls.defaults import url, patterns
2 2
 
3  
-from django.contrib.qunit.views import QUnitOverview, QUnitRunner
  3
+from django.test.javascript.views import JavascriptTestRunner, static_path
  4
+
4 5
 
5 6
 urlpatterns = patterns('',
6  
-    url('^$', QUnitOverview.as_view(), name='qunit-test-overview'),
7  
-    url('^(?P<label>[\w]+)/(?P<path>.*)$', QUnitRunner.as_view(), name='qunit-test-runner'),
  7
+    url('^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': static_path}, name='javascript-static'),
  8
+    url('^$', JavascriptTestRunner.as_view(), name='javascript-test-overview'),
  9
+    url('^(?P<label>[\w]+)/(?P<path>.*)$', JavascriptTestRunner.as_view(), name='javascript-test-runner'),
8 10
 )
82  django/test/javascript/views.py
@@ -5,70 +5,70 @@
5 5
 from django.conf import settings
6 6
 from django.core.files.storage import FileSystemStorage
7 7
 from django.http import Http404
8  
-from django.utils import simplejson
9  
-from django.utils.datastructures import SortedDict
  8
+from django.utils import simplejson, datastructures
10 9
 from django.utils.importlib import import_module
11 10
 from django.views import generic, static
  11
+from django.core.urlresolvers import reverse
12 12
 
  13
+thisdir = os.path.dirname(__file__)
  14
+static_path = os.path.join(thisdir, 'static')
  15
+templates_path = os.path.join(thisdir, 'templates')
13 16
 
14  
-class QUnitOverview(generic.TemplateView):
  17
+
  18
+class JavascriptTestRunner(generic.TemplateView):
15 19
     manifest = 'suite.json'
16  
-    template_name = 'qunit/overview.html'
  20
+    template_name = 'javascript/runner.html'
17 21
 
18 22
     def __init__(self, *args, **kwargs):
19  
-        super(QUnitOverview, self).__init__(*args, **kwargs)
20  
-        self.suites = self.get_suites(*args, **kwargs)
21  
-
22  
-    def get_suites(self, **kwargs):
23  
-        suites = SortedDict()
  23
+        super(JavascriptTestRunner, self).__init__(*args, **kwargs)
  24
+        self.suites = datastructures.SortedDict()
24 25
         for app_name in settings.INSTALLED_APPS:
25 26
             mod = import_module(app_name)
26 27
             mod_path = os.path.dirname(mod.__file__)
27 28
             label = app_name.split('.')[-1]
28  
-            tests_path = os.path.join(mod_path, 'static', label, 'tests')
29  
-            if os.path.isfile(os.path.join(tests_path, self.manifest)):
30  
-                suites[label] = {
  29
+            tests_path = os.path.join(mod_path, 'tests', 'javascript')
  30
+            if os.path.isdir(tests_path):
  31
+                self.suites[label] = {
31 32
                     "local_urls": [],
32 33
                     "remote_urls": [],
33 34
                     "name": app_name,
34 35
                     "tests_path": tests_path,
35 36
                 }
36  
-        return suites
37  
-
38  
-    def get_context_data(self, **kwargs):
39  
-        context = super(QUnitOverview, self).get_context_data(**kwargs)
40  
-        context["suites"] = self.suites
41  
-        return context
42  
-
43  
-
44  
-class QUnitRunner(QUnitOverview):
45  
-    template_name = 'qunit/runner.html'
46 37
 
47 38
     def get(self, request, label=None, path=None, *args, **kwargs):
48 39
         """
49 40
         
50 41
         """
51  
-        if label not in self.suites:
52  
-            raise Http404("No test suite found with label '%s'" % label)
53  
-        suite = self.suites.get(label)
54  
-        full_path = os.path.join(suite['tests_path'], path)
55  
-        if os.path.isfile(full_path):
56  
-            directory, filename = os.path.split(full_path)
57  
-            return static.serve(request, path=filename, document_root=directory)
58  
-        storage = FileSystemStorage(location=full_path, base_url="")
59  
-        context = self.get_context_data(suite, storage, **kwargs)
  42
+        if label is not None:
  43
+            suite = self.suites.get(label)
  44
+            if suite is None:
  45
+                raise Http404("No test suite found with label '%s'" % label)
  46
+            full_path = os.path.join(suite['tests_path'], path)
  47
+            if os.path.isfile(full_path):
  48
+                dir, filename = os.path.split(full_path)
  49
+                return static.serve(request, path=filename, document_root=dir)
  50
+            context = self.get_context_data(suite, full_path, *args, **kwargs)
  51
+        else:
  52
+            context = self.get_context_data(*args, **kwargs)
60 53
         return self.render_to_response(context)
61 54
 
62  
-    def get_context_data(self, suite, storage, **kwargs):
63  
-        context = super(QUnitRunner, self).get_context_data(**kwargs)
64  
-        # load suite.json if present
65  
-        if storage.exists(self.manifest):
66  
-            with storage.open(self.manifest) as json:
67  
-                suite.update(simplejson.loads(json.read()))
68  
-        directories, files = storage.listdir('.')
  55
+    def get_context_data(self, suite=None, full_path=None, **kwargs):
  56
+        context = super(JavascriptTestRunner, self).get_context_data(**kwargs)
69 57
         context.update({
70  
-            'suite': suite,
71  
-            'subsuites': directories,
72  
-            'files': [file for file in files if file.endswith('js')],
  58
+            'suites': self.suites,
  59
+            'linkbase': reverse('javascript-test-overview'),
73 60
         })
  61
+        if full_path:
  62
+            storage = FileSystemStorage(location=full_path)
  63
+            # load suite.json if present
  64
+            if storage.exists(self.manifest):
  65
+                with storage.open(self.manifest) as json:
  66
+                    suite.update(simplejson.loads(json.read()))
  67
+
  68
+            directories, files = storage.listdir('.')
  69
+            context.update({
  70
+                'suite': suite,
  71
+                'subsuites': directories,
  72
+                'files': [file for file in files if file.endswith('js')],
  73
+            })
74 74
         return context
3  tests/urls.py
@@ -32,7 +32,4 @@
32 32
 
33 33
     # test util views
34 34
     (r'test_utils/', include('regressiontests.test_utils.urls')),
35  
-
36  
-    # qunit tests
37  
-    ('^qunit/', include('django.contrib.qunit.urls')),
38 35
 )

0 notes on commit 9c566eb

Please sign in to comment.
Something went wrong with that request. Please try again.