11# -*- coding: utf-8 -*-
22from django .template import Context , TemplateSyntaxError
33from django .template .loader import get_template
4- from django .test import SimpleTestCase , TestCase
4+ from django .test import RequestFactory , SimpleTestCase , TestCase
55from django .core .urlresolvers import reverse
66from django .utils .safestring import mark_safe
77
@@ -203,7 +203,6 @@ class ListGroupTagsTest(SimpleTestCase):
203203 LIST_GROUP_BUTTON_START = LIST_GROUP_LINK_START
204204 LIST_GROUP_BUTTON_END = LIST_GROUP_LINK_END
205205
206- LIST_GROUP_ITEM_LINK_START = mark_safe ('<a class="list-group-item" href="' + SAMPLE_LINK + '">' )
207206 LIST_GROUP_ITEM_BUTTON_START = mark_safe ('<button class="list-group-item" type="button">' )
208207 LIST_GROUP_ITEM_BUTTON_END = mark_safe ('</button>' )
209208
@@ -339,8 +338,12 @@ def test_panel_footer_content_is_rendered(self):
339338
340339class TemplatesTest (TestCase ):
341340 def setUp (self ):
341+ # Mock get request
342+ self .request = RequestFactory ().get ('/' )
343+
344+ # Provide template paths
342345 self .template_html5_skeleton = get_template ('bootstrap_ui/html5-skeleton.html' )
343- self .url_bootstrap_skeleton = reverse ( 'render_named_template' , kwargs = { 'template' : ' bootstrap_ui/bootstrap-skeleton.html'} )
346+ self .template_bootstrap_skeleton = get_template ( ' bootstrap_ui/bootstrap-skeleton.html' )
344347
345348 def test_html5_skeleton_is_rendered (self ):
346349 rendered = self .template_html5_skeleton .render (Context ({}))
@@ -349,7 +352,7 @@ def test_html5_skeleton_is_rendered(self):
349352 '<body><h1>Hello, django-bootstrap-ui!</h1></body></html>' , rendered )
350353
351354 def test_bootstrap_skeleton_is_rendered (self ):
352- rendered = str ( self .client . get ( self .url_bootstrap_skeleton ). content )
355+ rendered = self .template_bootstrap_skeleton . render ( Context ({ 'request' : self .request }) )
353356 self .assertInHTML ('<meta http-equiv="X-UA-Compatible" content="IE=edge">' , rendered )
354357 self .assertInHTML ('<meta name="viewport" content="width=device-width, initial-scale=1">' , rendered )
355358 self .assertInHTML ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">' , rendered )
@@ -359,32 +362,36 @@ def test_bootstrap_skeleton_is_rendered(self):
359362 self .assertNotIn ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">' , rendered )
360363
361364 def test_bootstrap_skeleton_bootstrap_theme_is_rendered (self ):
362- self .client . post ( reverse ( 'set_theme' ), { 'theme ' : 'bootstrap' })
363- rendered = str ( self .client . get ( self .url_bootstrap_skeleton ). content )
365+ self .request . session = { 'DJANGO_BOOTSTRAP_UI_THEME ' : 'bootstrap' }
366+ rendered = self .template_bootstrap_skeleton . render ( Context ({ 'request' : self .request }) )
364367 self .assertInHTML ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">' , rendered )
365368 self .assertInHTML ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">' , rendered )
366369
367370 def test_bootstrap_skeleton_bootswatch_theme_is_rendered (self ):
368- self .client . post ( reverse ( 'set_theme' ), { 'theme ' : 'bootswatch-paper' })
369- rendered = str ( self .client . get ( self .url_bootstrap_skeleton ). content )
371+ self .request . session = { 'DJANGO_BOOTSTRAP_UI_THEME ' : 'bootswatch-paper' }
372+ rendered = self .template_bootstrap_skeleton . render ( Context ({ 'request' : self .request }) )
370373 self .assertInHTML ('<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" rel="stylesheet" type="text/css">' , rendered )
371374 self .assertNotIn ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">' , rendered )
372375 self .assertNotIn ('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">' , rendered )
373376
374377
375378class ThemingTest (TestCase ):
376379 def setUp (self ):
380+ # Mock get request
381+ self .request = RequestFactory ().get ('/' )
382+
383+ # Provide template paths
384+ self .template_assignment_tags = get_template ('assignmenttags.html' )
377385 self .url_set_theme_view = reverse ('set_theme' )
378- self .url_render_assignment_tags_template = reverse ('render_named_template' , kwargs = {'template' : 'assignmenttags.html' })
379386
380387 def test_theme_in_session_is_set (self ):
381- self .client .post (self .url_set_theme_view , {'theme' : 'bootstrap-theme-in-session' })
382- self .assertEqual (self .client .session ['DJANGO_BOOTSTRAP_UI_THEME' ], 'bootstrap-theme-in-session' )
388+ with self .settings (MIDDLEWARE_CLASSES = ('django.contrib.sessions.middleware.SessionMiddleware' ,)):
389+ self .client .post (self .url_set_theme_view , {'theme' : 'bootstrap-theme-in-session' })
390+ self .assertEqual (self .client .session ['DJANGO_BOOTSTRAP_UI_THEME' ], 'bootstrap-theme-in-session' )
383391
384392 def test_theme_in_cookie_is_set (self ):
385- with self .settings (MIDDLEWARE_CLASSES = ()):
386- self .client .post (self .url_set_theme_view , {'theme' : 'bootstrap-theme-in-cookies' })
387- self .assertEqual (self .client .cookies ['DJANGO_BOOTSTRAP_UI_THEME' ].value , 'bootstrap-theme-in-cookies' )
393+ self .client .post (self .url_set_theme_view , {'theme' : 'bootstrap-theme-in-cookies' })
394+ self .assertEqual (self .client .cookies ['DJANGO_BOOTSTRAP_UI_THEME' ].value , 'bootstrap-theme-in-cookies' )
388395
389396 def test_set_theme_is_redirected (self ):
390397 # We are redirecting to non-existent urls/views and therefore expecting 404 codes in the end
@@ -398,21 +405,20 @@ def test_set_theme_is_redirected(self):
398405 self .assertRedirects (response , '/foo/?bar=yes' , 302 , 404 )
399406
400407 def test_get_value_from_session_is_ok (self ):
401- self .client . post ( reverse ( 'set_theme' ), { 'theme ' : 'bootstrap-theme-in-session' })
402- rendered = str ( self .client . get ( self .url_render_assignment_tags_template ). content )
408+ self .request . session = { 'DJANGO_BOOTSTRAP_UI_THEME ' : 'bootstrap-theme-in-session' }
409+ rendered = self .template_assignment_tags . render ( Context ({ 'request' : self .request }) )
403410 self .assertIn ('Theme|bootstrap-theme-in-session' , rendered )
404411
405412 def test_get_value_from_cookie_is_ok (self ):
406- with self .settings (MIDDLEWARE_CLASSES = ()):
407- self .client .post (reverse ('set_theme' ), {'theme' : 'bootstrap-theme-in-cookies' })
408- rendered = str (self .client .get (self .url_render_assignment_tags_template ).content )
409- self .assertIn ('Theme|bootstrap-theme-in-cookies' , rendered )
413+ self .request .COOKIES = {'DJANGO_BOOTSTRAP_UI_THEME' : 'bootstrap-theme-in-cookies' }
414+ rendered = self .template_assignment_tags .render (Context ({'request' : self .request }))
415+ self .assertIn ('Theme|bootstrap-theme-in-cookies' , rendered )
410416
411417 def test_get_value_from_settings_is_ok (self ):
412418 with self .settings (DJANGO_BOOTSTRAP_UI_THEME = 'bootstrap-theme-in-settings' ):
413- rendered = str ( self .client . get ( self .url_render_assignment_tags_template ). content )
419+ rendered = self .template_assignment_tags . render ( Context ({ 'request' : self .request }) )
414420 self .assertIn ('Theme|bootstrap-theme-in-settings' , rendered )
415421
416422 def test_get_value_default_is_ok (self ):
417- rendered = str ( self .client . get ( self .url_render_assignment_tags_template ). content )
423+ rendered = self .template_assignment_tags . render ( Context ({ 'request' : self .request }) )
418424 self .assertIn ('Theme|bootstrap-theme-default' , rendered )
0 commit comments