1. from django.template import TemplateSyntaxError
    
  2. from django.test import SimpleTestCase
    
  3. 
    
  4. from ..utils import setup
    
  5. 
    
  6. 
    
  7. class LoadTagTests(SimpleTestCase):
    
  8.     libraries = {
    
  9.         "subpackage.echo": "template_tests.templatetags.subpackage.echo",
    
  10.         "testtags": "template_tests.templatetags.testtags",
    
  11.     }
    
  12. 
    
  13.     @setup(
    
  14.         {
    
  15.             "load01": (
    
  16.                 '{% load testtags subpackage.echo %}{% echo test %} {% echo2 "test" %}'
    
  17.             )
    
  18.         }
    
  19.     )
    
  20.     def test_load01(self):
    
  21.         output = self.engine.render_to_string("load01")
    
  22.         self.assertEqual(output, "test test")
    
  23. 
    
  24.     @setup({"load02": '{% load subpackage.echo %}{% echo2 "test" %}'})
    
  25.     def test_load02(self):
    
  26.         output = self.engine.render_to_string("load02")
    
  27.         self.assertEqual(output, "test")
    
  28. 
    
  29.     # {% load %} tag, importing individual tags
    
  30.     @setup({"load03": "{% load echo from testtags %}{% echo this that theother %}"})
    
  31.     def test_load03(self):
    
  32.         output = self.engine.render_to_string("load03")
    
  33.         self.assertEqual(output, "this that theother")
    
  34. 
    
  35.     @setup(
    
  36.         {
    
  37.             "load04": "{% load echo other_echo from testtags %}"
    
  38.             "{% echo this that theother %} {% other_echo and another thing %}"
    
  39.         }
    
  40.     )
    
  41.     def test_load04(self):
    
  42.         output = self.engine.render_to_string("load04")
    
  43.         self.assertEqual(output, "this that theother and another thing")
    
  44. 
    
  45.     @setup(
    
  46.         {
    
  47.             "load05": "{% load echo upper from testtags %}"
    
  48.             "{% echo this that theother %} {{ statement|upper }}"
    
  49.         }
    
  50.     )
    
  51.     def test_load05(self):
    
  52.         output = self.engine.render_to_string("load05", {"statement": "not shouting"})
    
  53.         self.assertEqual(output, "this that theother NOT SHOUTING")
    
  54. 
    
  55.     @setup({"load06": '{% load echo2 from subpackage.echo %}{% echo2 "test" %}'})
    
  56.     def test_load06(self):
    
  57.         output = self.engine.render_to_string("load06")
    
  58.         self.assertEqual(output, "test")
    
  59. 
    
  60.     # {% load %} tag errors
    
  61.     @setup({"load07": "{% load echo other_echo bad_tag from testtags %}"})
    
  62.     def test_load07(self):
    
  63.         msg = "'bad_tag' is not a valid tag or filter in tag library 'testtags'"
    
  64.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  65.             self.engine.get_template("load07")
    
  66. 
    
  67.     @setup({"load08": "{% load echo other_echo bad_tag from %}"})
    
  68.     def test_load08(self):
    
  69.         msg = (
    
  70.             "'echo' is not a registered tag library. Must be one of:\n"
    
  71.             "subpackage.echo\ntesttags"
    
  72.         )
    
  73.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  74.             self.engine.get_template("load08")
    
  75. 
    
  76.     @setup({"load09": "{% load from testtags %}"})
    
  77.     def test_load09(self):
    
  78.         msg = (
    
  79.             "'from' is not a registered tag library. Must be one of:\n"
    
  80.             "subpackage.echo\ntesttags"
    
  81.         )
    
  82.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  83.             self.engine.get_template("load09")
    
  84. 
    
  85.     @setup({"load10": "{% load echo from bad_library %}"})
    
  86.     def test_load10(self):
    
  87.         msg = (
    
  88.             "'bad_library' is not a registered tag library. Must be one of:\n"
    
  89.             "subpackage.echo\ntesttags"
    
  90.         )
    
  91.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  92.             self.engine.get_template("load10")
    
  93. 
    
  94.     @setup({"load12": "{% load subpackage.missing %}"})
    
  95.     def test_load12(self):
    
  96.         msg = (
    
  97.             "'subpackage.missing' is not a registered tag library. Must be one of:\n"
    
  98.             "subpackage.echo\ntesttags"
    
  99.         )
    
  100.         with self.assertRaisesMessage(TemplateSyntaxError, msg):
    
  101.             self.engine.get_template("load12")