2015-06-26 00:49:50 +08:00
|
|
|
|
from django.template import RequestContext, TemplateSyntaxError
|
2015-08-18 01:45:07 +08:00
|
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2015-12-30 23:51:16 +08:00
|
|
|
|
from django.urls import NoReverseMatch, resolve
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
2014-12-07 16:43:10 +08:00
|
|
|
|
from ..utils import setup
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='template_tests.urls')
|
2014-12-04 04:36:17 +08:00
|
|
|
|
class UrlTagTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request_factory = RequestFactory()
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
# Successes
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url01': '{% url "client" client.id %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url01(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url01', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url02': '{% url "client_action" id=client.id action="update" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url02(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url02', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/update/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url02a': '{% url "client_action" client.id "update" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url02a(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url02a', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/update/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url02b': "{% url 'client_action' id=client.id action='update' %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url02b(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url02b', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/update/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url02c': "{% url 'client_action' client.id 'update' %}"})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url02c(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url02c', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/update/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url03': '{% url "index" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url03(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url03')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/')
|
|
|
|
|
|
|
|
|
|
@setup({'url04': '{% url "named.client" client.id %}'})
|
|
|
|
|
def test_url04(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url04', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/named-client/1/')
|
|
|
|
|
|
|
|
|
|
@setup({'url05': '{% url "метка_оператора" v %}'})
|
|
|
|
|
def test_url05(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url05', {'v': 'Ω'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
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):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url06', {'v': 'Ω'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
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):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url08', {'v': 'Ω'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
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):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url09', {'v': 'Ω'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url10': '{% url "client_action" id=client.id action="two words" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url10(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url10', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/two%20words/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url11': '{% url "client_action" id=client.id action="==" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url11(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url11', {'client': {'id': 1}})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/==/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url12': '{% url "client_action" id=client.id action="!$&\'()*+,;=~:@," %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url12(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url12', {'client': {'id': 1}})
|
2019-04-24 19:30:34 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/!$&'()*+,;=~:@,/')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url13': '{% url "client_action" id=client.id action=arg|join:"-" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url13(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url13', {'client': {'id': 1}, 'arg': ['a', 'b']})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/a-b/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url14': '{% url "client_action" client.id arg|join:"-" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url14(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url14', {'client': {'id': 1}, 'arg': ['a', 'b']})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/a-b/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url15': '{% url "client_action" 12 "test" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url15(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url15')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/12/test/')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url18': '{% url "client" "1,2" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url18(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url18')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1,2/')
|
|
|
|
|
|
|
|
|
|
@setup({'url19': '{% url named_url client.id %}'})
|
|
|
|
|
def test_url19(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
|
output = self.engine.render_to_string(
|
2015-08-18 01:45:07 +08:00
|
|
|
|
'url19', {'client': {'id': 1}, 'named_url': 'client'}
|
2015-09-12 07:33:12 +08:00
|
|
|
|
)
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/client/1/')
|
|
|
|
|
|
|
|
|
|
@setup({'url20': '{% url url_name_in_var client.id %}'})
|
|
|
|
|
def test_url20(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url20', {'client': {'id': 1}, 'url_name_in_var': 'named.client'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/named-client/1/')
|
|
|
|
|
|
2015-07-21 20:35:11 +08:00
|
|
|
|
@setup({'url21': '{% autoescape off %}'
|
2015-08-18 01:45:07 +08:00
|
|
|
|
'{% url "client_action" id=client.id action="!$&\'()*+,;=~:@," %}'
|
2015-07-21 20:35:11 +08:00
|
|
|
|
'{% endautoescape %}'})
|
|
|
|
|
def test_url21(self):
|
|
|
|
|
output = self.engine.render_to_string('url21', {'client': {'id': 1}})
|
|
|
|
|
self.assertEqual(output, '/client/1/!$&\'()*+,;=~:@,/')
|
|
|
|
|
|
2014-11-12 09:32:44 +08:00
|
|
|
|
# Failures
|
|
|
|
|
@setup({'url-fail01': '{% url %}'})
|
|
|
|
|
def test_url_fail01(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail01')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail02': '{% url "no_such_view" %}'})
|
|
|
|
|
def test_url_fail02(self):
|
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail02')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url-fail03': '{% url "client" %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url_fail03(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
|
self.engine.render_to_string('url-fail03')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail04': '{% url "view" id, %}'})
|
|
|
|
|
def test_url_fail04(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail04')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail05': '{% url "view" id= %}'})
|
|
|
|
|
def test_url_fail05(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail05')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail06': '{% url "view" a.id=id %}'})
|
|
|
|
|
def test_url_fail06(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail06')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail07': '{% url "view" a.id!id %}'})
|
|
|
|
|
def test_url_fail07(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail07')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail08': '{% url "view" id="unterminatedstring %}'})
|
|
|
|
|
def test_url_fail08(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail08')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail09': '{% url "view" id=", %}'})
|
|
|
|
|
def test_url_fail09(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.get_template('url-fail09')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail11': '{% url named_url %}'})
|
|
|
|
|
def test_url_fail11(self):
|
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail11')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail12': '{% url named_url %}'})
|
|
|
|
|
def test_url_fail12(self):
|
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail12', {'named_url': 'no_such_view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail13': '{% url named_url %}'})
|
|
|
|
|
def test_url_fail13(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
|
self.engine.render_to_string('url-fail13', {'named_url': 'template_tests.views.client'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail14': '{% url named_url id, %}'})
|
|
|
|
|
def test_url_fail14(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail14', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail15': '{% url named_url id= %}'})
|
|
|
|
|
def test_url_fail15(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail15', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail16': '{% url named_url a.id=id %}'})
|
|
|
|
|
def test_url_fail16(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail16', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail17': '{% url named_url a.id!id %}'})
|
|
|
|
|
def test_url_fail17(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail17', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail18': '{% url named_url id="unterminatedstring %}'})
|
|
|
|
|
def test_url_fail18(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail18', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-fail19': '{% url named_url id=", %}'})
|
|
|
|
|
def test_url_fail19(self):
|
|
|
|
|
with self.assertRaises(TemplateSyntaxError):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
self.engine.render_to_string('url-fail19', {'named_url': 'view'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
|
|
|
|
|
# {% url ... as var %}
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url-asvar01': '{% url "index" as url %}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url_asvar01(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url-asvar01')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '')
|
|
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
|
@setup({'url-asvar02': '{% url "index" as url %}{{ url }}'})
|
2014-11-12 09:32:44 +08:00
|
|
|
|
def test_url_asvar02(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
|
output = self.engine.render_to_string('url-asvar02')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '/')
|
|
|
|
|
|
|
|
|
|
@setup({'url-asvar03': '{% url "no_such_view" as url %}{{ url }}'})
|
|
|
|
|
def test_url_asvar03(self):
|
2014-12-07 16:43:10 +08:00
|
|
|
|
output = self.engine.render_to_string('url-asvar03')
|
2014-11-12 09:32:44 +08:00
|
|
|
|
self.assertEqual(output, '')
|
2015-06-26 00:49:50 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-namespace01': '{% url "app:named.client" 42 %}'})
|
|
|
|
|
def test_url_namespace01(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request = self.request_factory.get('/')
|
2015-06-26 00:49:50 +08:00
|
|
|
|
request.resolver_match = resolve('/ns1/')
|
|
|
|
|
template = self.engine.get_template('url-namespace01')
|
|
|
|
|
context = RequestContext(request)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
self.assertEqual(output, '/ns1/named-client/42/')
|
|
|
|
|
|
|
|
|
|
@setup({'url-namespace02': '{% url "app:named.client" 42 %}'})
|
|
|
|
|
def test_url_namespace02(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request = self.request_factory.get('/')
|
2015-06-26 00:49:50 +08:00
|
|
|
|
request.resolver_match = resolve('/ns2/')
|
|
|
|
|
template = self.engine.get_template('url-namespace02')
|
|
|
|
|
context = RequestContext(request)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
self.assertEqual(output, '/ns2/named-client/42/')
|
|
|
|
|
|
|
|
|
|
@setup({'url-namespace03': '{% url "app:named.client" 42 %}'})
|
|
|
|
|
def test_url_namespace03(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request = self.request_factory.get('/')
|
2015-06-26 00:49:50 +08:00
|
|
|
|
template = self.engine.get_template('url-namespace03')
|
|
|
|
|
context = RequestContext(request)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
self.assertEqual(output, '/ns2/named-client/42/')
|
2015-10-26 21:28:05 +08:00
|
|
|
|
|
|
|
|
|
@setup({'url-namespace-no-current-app': '{% url "app:named.client" 42 %}'})
|
|
|
|
|
def test_url_namespace_no_current_app(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request = self.request_factory.get('/')
|
2015-10-26 21:28:05 +08:00
|
|
|
|
request.resolver_match = resolve('/ns1/')
|
|
|
|
|
request.current_app = None
|
|
|
|
|
template = self.engine.get_template('url-namespace-no-current-app')
|
|
|
|
|
context = RequestContext(request)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
self.assertEqual(output, '/ns2/named-client/42/')
|
|
|
|
|
|
|
|
|
|
@setup({'url-namespace-explicit-current-app': '{% url "app:named.client" 42 %}'})
|
|
|
|
|
def test_url_namespace_explicit_current_app(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
|
request = self.request_factory.get('/')
|
2015-10-26 21:28:05 +08:00
|
|
|
|
request.resolver_match = resolve('/ns1/')
|
|
|
|
|
request.current_app = 'app'
|
|
|
|
|
template = self.engine.get_template('url-namespace-explicit-current-app')
|
|
|
|
|
context = RequestContext(request)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
self.assertEqual(output, '/ns2/named-client/42/')
|