summaryrefslogtreecommitdiff
path: root/tests/template_tests/syntax_tests/test_url.py
diff options
context:
space:
mode:
authorPreston Timmons <prestontimmons@gmail.com>2014-11-11 19:32:44 -0600
committerTim Graham <timograham@gmail.com>2014-12-02 19:18:35 -0500
commitb872134bfc14f6322bd1e4b0a08bf5bfd2c43a52 (patch)
treef82fc6be418adeb1e7ff36728f82008770066999 /tests/template_tests/syntax_tests/test_url.py
parent4a4ad27712b44cebada1bdaebd082cf82df74610 (diff)
Fixed #23768 -- Rewrote template tests as unit tests.
Diffstat (limited to 'tests/template_tests/syntax_tests/test_url.py')
-rw-r--r--tests/template_tests/syntax_tests/test_url.py267
1 files changed, 267 insertions, 0 deletions
diff --git a/tests/template_tests/syntax_tests/test_url.py b/tests/template_tests/syntax_tests/test_url.py
new file mode 100644
index 0000000000..7881bc58ed
--- /dev/null
+++ b/tests/template_tests/syntax_tests/test_url.py
@@ -0,0 +1,267 @@
+# coding: utf-8
+import warnings
+
+from django.core.urlresolvers import NoReverseMatch
+from django.template.base import TemplateSyntaxError
+from django.template.loader import get_template
+from django.test import override_settings, TestCase
+from django.utils.deprecation import RemovedInDjango20Warning
+
+from .utils import render, setup
+
+
+@override_settings(ROOT_URLCONF='template_tests.urls')
+class UrlTagTests(TestCase):
+
+ # Successes
+ @setup({'url01': '{% url "template_tests.views.client" client.id %}'})
+ def test_url01(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url01', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/')
+
+ @setup({'url02': '{% url "template_tests.views.client_action" id=client.id action="update" %}'})
+ def test_url02(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url02', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/update/')
+
+ @setup({'url02a': '{% url "template_tests.views.client_action" client.id "update" %}'})
+ def test_url02a(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url02a', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/update/')
+
+ @setup({'url02b': "{% url 'template_tests.views.client_action' id=client.id action='update' %}"})
+ def test_url02b(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url02b', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/update/')
+
+ @setup({'url02c': "{% url 'template_tests.views.client_action' client.id 'update' %}"})
+ def test_url02c(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url02c', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/update/')
+
+ @setup({'url03': '{% url "template_tests.views.index" %}'})
+ def test_url03(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url03')
+ self.assertEqual(output, '/')
+
+ @setup({'url04': '{% url "named.client" client.id %}'})
+ def test_url04(self):
+ output = render('url04', {'client': {'id': 1}})
+ self.assertEqual(output, '/named-client/1/')
+
+ @setup({'url05': '{% url "метка_оператора" v %}'})
+ def test_url05(self):
+ output = render('url05', {'v': 'Ω'})
+ self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
+
+ @setup({'url06': '{% url "метка_оператора_2" tag=v %}'})
+ def test_url06(self):
+ output = render('url06', {'v': 'Ω'})
+ self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
+
+ @setup({'url07': '{% url "template_tests.views.client2" tag=v %}'})
+ def test_url07(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url07', {'v': 'Ω'})
+ self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
+
+ @setup({'url08': '{% url "метка_оператора" v %}'})
+ def test_url08(self):
+ output = render('url08', {'v': 'Ω'})
+ self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
+
+ @setup({'url09': '{% url "метка_оператора_2" tag=v %}'})
+ def test_url09(self):
+ output = render('url09', {'v': 'Ω'})
+ self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
+
+ @setup({'url10': '{% url "template_tests.views.client_action" id=client.id action="two words" %}'})
+ def test_url10(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url10', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/two%20words/')
+
+ @setup({'url11': '{% url "template_tests.views.client_action" id=client.id action="==" %}'})
+ def test_url11(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url11', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/==/')
+
+ @setup({'url12': '{% url "template_tests.views.client_action" '
+ 'id=client.id action="!$&\'()*+,;=~:@," %}'})
+ def test_url12(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url12', {'client': {'id': 1}})
+ self.assertEqual(output, '/client/1/!$&\'()*+,;=~:@,/')
+
+ @setup({'url13': '{% url "template_tests.views.client_action" '
+ 'id=client.id action=arg|join:"-" %}'})
+ def test_url13(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url13', {'client': {'id': 1}, 'arg': ['a', 'b']})
+ self.assertEqual(output, '/client/1/a-b/')
+
+ @setup({'url14': '{% url "template_tests.views.client_action" client.id arg|join:"-" %}'})
+ def test_url14(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url14', {'client': {'id': 1}, 'arg': ['a', 'b']})
+ self.assertEqual(output, '/client/1/a-b/')
+
+ @setup({'url15': '{% url "template_tests.views.client_action" 12 "test" %}'})
+ def test_url15(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url15')
+ self.assertEqual(output, '/client/12/test/')
+
+ @setup({'url18': '{% url "template_tests.views.client" "1,2" %}'})
+ def test_url18(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url18')
+ self.assertEqual(output, '/client/1,2/')
+
+ @setup({'url19': '{% url named_url client.id %}'})
+ def test_url19(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url19', {'client': {'id': 1}, 'named_url': 'template_tests.views.client'})
+ self.assertEqual(output, '/client/1/')
+
+ @setup({'url20': '{% url url_name_in_var client.id %}'})
+ def test_url20(self):
+ output = render('url20', {'client': {'id': 1}, 'url_name_in_var': 'named.client'})
+ self.assertEqual(output, '/named-client/1/')
+
+ # Failures
+ @setup({'url-fail01': '{% url %}'})
+ def test_url_fail01(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail01')
+
+ @setup({'url-fail02': '{% url "no_such_view" %}'})
+ def test_url_fail02(self):
+ with self.assertRaises(NoReverseMatch):
+ render('url-fail02')
+
+ @setup({'url-fail03': '{% url "template_tests.views.client" %}'})
+ def test_url_fail03(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ with self.assertRaises(NoReverseMatch):
+ render('url-fail03')
+
+ @setup({'url-fail04': '{% url "view" id, %}'})
+ def test_url_fail04(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail04')
+
+ @setup({'url-fail05': '{% url "view" id= %}'})
+ def test_url_fail05(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail05')
+
+ @setup({'url-fail06': '{% url "view" a.id=id %}'})
+ def test_url_fail06(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail06')
+
+ @setup({'url-fail07': '{% url "view" a.id!id %}'})
+ def test_url_fail07(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail07')
+
+ @setup({'url-fail08': '{% url "view" id="unterminatedstring %}'})
+ def test_url_fail08(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail08')
+
+ @setup({'url-fail09': '{% url "view" id=", %}'})
+ def test_url_fail09(self):
+ with self.assertRaises(TemplateSyntaxError):
+ get_template('url-fail09')
+
+ @setup({'url-fail11': '{% url named_url %}'})
+ def test_url_fail11(self):
+ with self.assertRaises(NoReverseMatch):
+ render('url-fail11')
+
+ @setup({'url-fail12': '{% url named_url %}'})
+ def test_url_fail12(self):
+ with self.assertRaises(NoReverseMatch):
+ render('url-fail12', {'named_url': 'no_such_view'})
+
+ @setup({'url-fail13': '{% url named_url %}'})
+ def test_url_fail13(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ with self.assertRaises(NoReverseMatch):
+ render('url-fail13', {'named_url': 'template_tests.views.client'})
+
+ @setup({'url-fail14': '{% url named_url id, %}'})
+ def test_url_fail14(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail14', {'named_url': 'view'})
+
+ @setup({'url-fail15': '{% url named_url id= %}'})
+ def test_url_fail15(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail15', {'named_url': 'view'})
+
+ @setup({'url-fail16': '{% url named_url a.id=id %}'})
+ def test_url_fail16(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail16', {'named_url': 'view'})
+
+ @setup({'url-fail17': '{% url named_url a.id!id %}'})
+ def test_url_fail17(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail17', {'named_url': 'view'})
+
+ @setup({'url-fail18': '{% url named_url id="unterminatedstring %}'})
+ def test_url_fail18(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail18', {'named_url': 'view'})
+
+ @setup({'url-fail19': '{% url named_url id=", %}'})
+ def test_url_fail19(self):
+ with self.assertRaises(TemplateSyntaxError):
+ render('url-fail19', {'named_url': 'view'})
+
+ # {% url ... as var %}
+ @setup({'url-asvar01': '{% url "template_tests.views.index" as url %}'})
+ def test_url_asvar01(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url-asvar01')
+ self.assertEqual(output, '')
+
+ @setup({'url-asvar02': '{% url "template_tests.views.index" as url %}{{ url }}'})
+ def test_url_asvar02(self):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", RemovedInDjango20Warning)
+ output = render('url-asvar02')
+ self.assertEqual(output, '/')
+
+ @setup({'url-asvar03': '{% url "no_such_view" as url %}{{ url }}'})
+ def test_url_asvar03(self):
+ output = render('url-asvar03')
+ self.assertEqual(output, '')