<s> from __future__ import absolute_import , unicode_literals <EOL> from django . template import Context , RequestContext , Template , engines <EOL> from django . test import TestCase <EOL> from wagtail . tests . testapp . models import TestSetting <EOL> from wagtail . tests . utils import WagtailTestUtils <EOL> from wagtail . wagtailcore . models import Page , Site <EOL> class TemplateTestCase ( TestCase , WagtailTestUtils ) : <EOL> def setUp ( self ) : <EOL> root = Page . objects . first ( ) <EOL> other_home = Page ( title = '<STR_LIT>' ) <EOL> root . add_child ( instance = other_home ) <EOL> self . default_site = Site . objects . get ( is_default_site = True ) <EOL> self . other_site = Site . objects . create ( hostname = '<STR_LIT>' , root_page = other_home ) <EOL> self . test_setting = TestSetting . objects . create ( <EOL> title = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> site = self . default_site ) <EOL> self . other_setting = TestSetting . objects . create ( <EOL> title = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> site = self . other_site ) <EOL> def get_request ( self , site = None ) : <EOL> if site is None : <EOL> site = self . default_site <EOL> request = self . client . get ( '<STR_LIT>' , HTTP_HOST = site . hostname ) <EOL> request . site = site <EOL> return request <EOL> def render ( self , request , string , context = None , site = None ) : <EOL> template = Template ( string ) <EOL> context = RequestContext ( request , context ) <EOL> return template . render ( context ) <EOL> class TestContextProcessor ( TemplateTestCase ) : <EOL> def test_accessing_setting ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> def test_multisite ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( site = self . default_site ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> request = self . get_request ( site = self . other_site ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . other_setting . title ) <EOL> def test_model_case_insensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( request , '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> def test_models_cached ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( ) <EOL> get_title = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> self . assertEqual ( <EOL> self . render ( request , get_title * i ) , <EOL> self . test_setting . title * i ) <EOL> class TestTemplateTag ( TemplateTestCase ) : <EOL> def test_no_context_processor ( self ) : <EOL> """<STR_LIT>""" <EOL> template = Template ( '<STR_LIT>' ) <EOL> context = Context ( ) <EOL> self . assertEqual ( template . render ( context ) , '<STR_LIT>' ) <EOL> def test_get_settings_request_context ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( site = self . other_site ) <EOL> context = Context ( { '<STR_LIT>' : request } ) <EOL> template = Template ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( template . render ( context ) , self . other_setting . title ) <EOL> def test_get_settings_request_context_use_default ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( site = self . other_site ) <EOL> context = Context ( { '<STR_LIT>' : request } ) <EOL> template = Template ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( template . render ( context ) , self . test_setting . title ) <EOL> def test_get_settings_use_default ( self ) : <EOL> """<STR_LIT>""" <EOL> context = Context ( ) <EOL> template = Template ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( template . render ( context ) , self . test_setting . title ) <EOL> def test_get_settings_no_request_no_default ( self ) : <EOL> """<STR_LIT>""" <EOL> context = Context ( ) <EOL> template = Template ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> template . render ( context ) <EOL> class TestSettingsJinja ( TemplateTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSettingsJinja , self ) . setUp ( ) <EOL> self . engine = engines [ '<STR_LIT>' ] <EOL> def render ( self , string , context = None , request_context = True ) : <EOL> if context is None : <EOL> context = { } <EOL> if request_context : <EOL> if '<STR_LIT>' in context : <EOL> site = context [ '<STR_LIT>' ] <EOL> else : <EOL> site = Site . objects . get ( is_default_site = True ) <EOL> request = self . client . get ( '<STR_LIT>' , HTTP_HOST = site . hostname ) <EOL> request . site = site <EOL> context [ '<STR_LIT>' ] = request <EOL> template = self . engine . from_string ( string ) <EOL> return template . render ( context ) <EOL> def test_accessing_setting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> def test_multisite ( self ) : <EOL> """<STR_LIT>""" <EOL> context = { '<STR_LIT>' : self . default_site } <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' , context ) , <EOL> self . test_setting . title ) <EOL> context = { '<STR_LIT>' : self . other_site } <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' , context ) , <EOL> self . other_setting . title ) <EOL> def test_model_case_insensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> self . assertEqual ( <EOL> self . render ( '<STR_LIT>' ) , <EOL> self . test_setting . title ) <EOL> def test_models_cached ( self ) : <EOL> """<STR_LIT>""" <EOL> get_title = '<STR_LIT>' <EOL> site = Site . objects . get ( is_default_site = True ) <EOL> request = self . client . get ( '<STR_LIT>' , HTTP_HOST = site . hostname ) <EOL> request . site = site <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> context = { '<STR_LIT>' : request } <EOL> template = self . engine . from_string ( get_title * i ) <EOL> self . assertEqual ( <EOL> template . render ( context ) , <EOL> self . test_setting . title * i ) <EOL> def test_settings_use_default_site_override ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . get_request ( site = self . other_site ) <EOL> context = { '<STR_LIT>' : request } <EOL> template = '<STR_LIT>' <EOL> self . assertEqual ( <EOL> self . render ( template , context ) , <EOL> self . test_setting . title ) <EOL> def test_settings_use_default_site ( self ) : <EOL> """<STR_LIT>""" <EOL> context = { } <EOL> template = '<STR_LIT>' <EOL> self . assertEqual ( <EOL> self . render ( template , context , request_context = False ) , <EOL> self . test_setting . title ) <EOL> def test_settings_no_request_no_use_default ( self ) : <EOL> """<STR_LIT>""" <EOL> context = { } <EOL> template = '<STR_LIT>' <EOL> with self . assertRaises ( RuntimeError ) : <EOL> self . render ( template , context , request_context = False ) </s>
