Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit 50ae5c9

Browse files
committed
Refactored tests mocking requests where appropriate
1 parent 301429e commit 50ae5c9

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

tests/settings.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@
88
'django.contrib.sessions',
99
'bootstrap_ui',
1010
]
11-
MIDDLEWARE_CLASSES = (
12-
'django.contrib.sessions.middleware.SessionMiddleware',
13-
)
1411
ROOT_URLCONF = 'tests.urls'
1512
SECRET_KEY = 'test-key'
1613
TEMPLATES = [
1714
{
1815
'BACKEND': 'django.template.backends.django.DjangoTemplates',
19-
'APP_DIRS': True,
2016
'DIRS': [
2117
'tests/templates',
2218
],
23-
'OPTIONS': {
24-
'context_processors': [
25-
'django.template.context_processors.request',
26-
]
27-
},
19+
'APP_DIRS': True,
2820
},
2921
]

tests/tests.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from django.template import Context, TemplateSyntaxError
33
from django.template.loader import get_template
4-
from django.test import SimpleTestCase, TestCase
4+
from django.test import RequestFactory, SimpleTestCase, TestCase
55
from django.core.urlresolvers import reverse
66
from 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

340339
class 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

375378
class 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)

tests/urls.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from django.conf.urls import include, url
22

3-
from .views import render_named_template
4-
53
urlpatterns = [
6-
url(r'^render_named_template/(?P<template>.*)/$', render_named_template, name='render_named_template'),
74
url(r'^themes/', include("bootstrap_ui.urls")),
85
]

tests/views.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)