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

Commit

Permalink
Refactored tests mocking requests where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
timorieber committed Nov 19, 2016
1 parent 301429e commit 50ae5c9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
10 changes: 1 addition & 9 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@
'django.contrib.sessions',
'bootstrap_ui',
]
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
)
ROOT_URLCONF = 'tests.urls'
SECRET_KEY = 'test-key'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [
'tests/templates',
],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
]
},
'APP_DIRS': True,
},
]
50 changes: 28 additions & 22 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from django.template import Context, TemplateSyntaxError
from django.template.loader import get_template
from django.test import SimpleTestCase, TestCase
from django.test import RequestFactory, SimpleTestCase, TestCase
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -203,7 +203,6 @@ class ListGroupTagsTest(SimpleTestCase):
LIST_GROUP_BUTTON_START = LIST_GROUP_LINK_START
LIST_GROUP_BUTTON_END = LIST_GROUP_LINK_END

LIST_GROUP_ITEM_LINK_START = mark_safe('<a class="list-group-item" href="' + SAMPLE_LINK + '">')
LIST_GROUP_ITEM_BUTTON_START = mark_safe('<button class="list-group-item" type="button">')
LIST_GROUP_ITEM_BUTTON_END = mark_safe('</button>')

Expand Down Expand Up @@ -339,8 +338,12 @@ def test_panel_footer_content_is_rendered(self):

class TemplatesTest(TestCase):
def setUp(self):
# Mock get request
self.request = RequestFactory().get('/')

# Provide template paths
self.template_html5_skeleton = get_template('bootstrap_ui/html5-skeleton.html')
self.url_bootstrap_skeleton = reverse('render_named_template', kwargs={'template': 'bootstrap_ui/bootstrap-skeleton.html'})
self.template_bootstrap_skeleton = get_template('bootstrap_ui/bootstrap-skeleton.html')

def test_html5_skeleton_is_rendered(self):
rendered = self.template_html5_skeleton.render(Context({}))
Expand All @@ -349,7 +352,7 @@ def test_html5_skeleton_is_rendered(self):
'<body><h1>Hello, django-bootstrap-ui!</h1></body></html>', rendered)

def test_bootstrap_skeleton_is_rendered(self):
rendered = str(self.client.get(self.url_bootstrap_skeleton).content)
rendered = self.template_bootstrap_skeleton.render(Context({'request': self.request}))
self.assertInHTML('<meta http-equiv="X-UA-Compatible" content="IE=edge">', rendered)
self.assertInHTML('<meta name="viewport" content="width=device-width, initial-scale=1">', rendered)
self.assertInHTML('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">', rendered)
Expand All @@ -359,32 +362,36 @@ def test_bootstrap_skeleton_is_rendered(self):
self.assertNotIn('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">', rendered)

def test_bootstrap_skeleton_bootstrap_theme_is_rendered(self):
self.client.post(reverse('set_theme'), {'theme': 'bootstrap'})
rendered = str(self.client.get(self.url_bootstrap_skeleton).content)
self.request.session = {'DJANGO_BOOTSTRAP_UI_THEME': 'bootstrap'}
rendered = self.template_bootstrap_skeleton.render(Context({'request': self.request}))
self.assertInHTML('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">', rendered)
self.assertInHTML('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">', rendered)

def test_bootstrap_skeleton_bootswatch_theme_is_rendered(self):
self.client.post(reverse('set_theme'), {'theme': 'bootswatch-paper'})
rendered = str(self.client.get(self.url_bootstrap_skeleton).content)
self.request.session = {'DJANGO_BOOTSTRAP_UI_THEME': 'bootswatch-paper'}
rendered = self.template_bootstrap_skeleton.render(Context({'request': self.request}))
self.assertInHTML('<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.7/paper/bootstrap.min.css" rel="stylesheet" type="text/css">', rendered)
self.assertNotIn('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">', rendered)
self.assertNotIn('<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css">', rendered)


class ThemingTest(TestCase):
def setUp(self):
# Mock get request
self.request = RequestFactory().get('/')

# Provide template paths
self.template_assignment_tags = get_template('assignmenttags.html')
self.url_set_theme_view = reverse('set_theme')
self.url_render_assignment_tags_template = reverse('render_named_template', kwargs={'template': 'assignmenttags.html'})

def test_theme_in_session_is_set(self):
self.client.post(self.url_set_theme_view, {'theme': 'bootstrap-theme-in-session'})
self.assertEqual(self.client.session['DJANGO_BOOTSTRAP_UI_THEME'], 'bootstrap-theme-in-session')
with self.settings(MIDDLEWARE_CLASSES=('django.contrib.sessions.middleware.SessionMiddleware',)):
self.client.post(self.url_set_theme_view, {'theme': 'bootstrap-theme-in-session'})
self.assertEqual(self.client.session['DJANGO_BOOTSTRAP_UI_THEME'], 'bootstrap-theme-in-session')

def test_theme_in_cookie_is_set(self):
with self.settings(MIDDLEWARE_CLASSES=()):
self.client.post(self.url_set_theme_view, {'theme': 'bootstrap-theme-in-cookies'})
self.assertEqual(self.client.cookies['DJANGO_BOOTSTRAP_UI_THEME'].value, 'bootstrap-theme-in-cookies')
self.client.post(self.url_set_theme_view, {'theme': 'bootstrap-theme-in-cookies'})
self.assertEqual(self.client.cookies['DJANGO_BOOTSTRAP_UI_THEME'].value, 'bootstrap-theme-in-cookies')

def test_set_theme_is_redirected(self):
# We are redirecting to non-existent urls/views and therefore expecting 404 codes in the end
Expand All @@ -398,21 +405,20 @@ def test_set_theme_is_redirected(self):
self.assertRedirects(response, '/foo/?bar=yes', 302, 404)

def test_get_value_from_session_is_ok(self):
self.client.post(reverse('set_theme'), {'theme': 'bootstrap-theme-in-session'})
rendered = str(self.client.get(self.url_render_assignment_tags_template).content)
self.request.session = {'DJANGO_BOOTSTRAP_UI_THEME': 'bootstrap-theme-in-session'}
rendered = self.template_assignment_tags.render(Context({'request': self.request}))
self.assertIn('Theme|bootstrap-theme-in-session', rendered)

def test_get_value_from_cookie_is_ok(self):
with self.settings(MIDDLEWARE_CLASSES=()):
self.client.post(reverse('set_theme'), {'theme': 'bootstrap-theme-in-cookies'})
rendered = str(self.client.get(self.url_render_assignment_tags_template).content)
self.assertIn('Theme|bootstrap-theme-in-cookies', rendered)
self.request.COOKIES = {'DJANGO_BOOTSTRAP_UI_THEME': 'bootstrap-theme-in-cookies'}
rendered = self.template_assignment_tags.render(Context({'request': self.request}))
self.assertIn('Theme|bootstrap-theme-in-cookies', rendered)

def test_get_value_from_settings_is_ok(self):
with self.settings(DJANGO_BOOTSTRAP_UI_THEME='bootstrap-theme-in-settings'):
rendered = str(self.client.get(self.url_render_assignment_tags_template).content)
rendered = self.template_assignment_tags.render(Context({'request': self.request}))
self.assertIn('Theme|bootstrap-theme-in-settings', rendered)

def test_get_value_default_is_ok(self):
rendered = str(self.client.get(self.url_render_assignment_tags_template).content)
rendered = self.template_assignment_tags.render(Context({'request': self.request}))
self.assertIn('Theme|bootstrap-theme-default', rendered)
3 changes: 0 additions & 3 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from django.conf.urls import include, url

from .views import render_named_template

urlpatterns = [
url(r'^render_named_template/(?P<template>.*)/$', render_named_template, name='render_named_template'),
url(r'^themes/', include("bootstrap_ui.urls")),
]
5 changes: 0 additions & 5 deletions tests/views.py

This file was deleted.

0 comments on commit 50ae5c9

Please sign in to comment.