Permalink
Browse files

Adding tests for Breadcrumbs and Flatpages.

  • Loading branch information...
1 parent 79d6644 commit d81f5dddaf27905dd2f8aeb5c4b62139c906e862 @chronossc committed Oct 19, 2012
@@ -0,0 +1,62 @@
+[
+ {
+ "pk": 1,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": false,
+ "title": "Flat Page 1",
+ "url": "/flat01/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "This is flat 1",
+ "enable_comments": false
+ }
+ },
+ {
+ "pk": 2,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": false,
+ "title": "Flat page 2",
+ "url": "/flat01/flat02/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "This is flat 2 under flat 1",
+ "enable_comments": false
+ }
+ },
+ {
+ "pk": 3,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": false,
+ "title": "Flat page 3",
+ "url": "/flat01/flat02/flat03/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "This is flat 3 under flat 2 that is under flat 1",
+ "enable_comments": false
+ }
+ },
+ {
+ "pk": 4,
+ "model": "flatpages.flatpage",
+ "fields": {
+ "registration_required": false,
+ "title": "Flat Page 4",
+ "url": "/flat04/",
+ "template_name": "",
+ "sites": [
+ 1
+ ],
+ "content": "This is flat 4 :)",
+ "enable_comments": false
+ }
+ }
+]
@@ -1,5 +1,5 @@
# # coding: utf-8
-
+import os
from django.conf import settings
from django.test import TestCase
from django.utils.datastructures import SortedDict
@@ -8,13 +8,27 @@
class BreadcrumbsTest(TestCase):
+ urls = 'breadcrumbs.tests.urls'
def setUp(self):
self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
breadcrumbs_middleware_class = 'breadcrumbs.middleware.BreadcrumbsMiddleware'
if breadcrumbs_middleware_class not in settings.MIDDLEWARE_CLASSES:
settings.MIDDLEWARE_CLASSES += (breadcrumbs_middleware_class,)
+ self.old_TEMPLATE_CONTEXT_PROCESSORS = settings.TEMPLATE_CONTEXT_PROCESSORS
+ request_processor = 'django.core.context_processors.request'
+ if request_processor not in settings.TEMPLATE_CONTEXT_PROCESSORS:
+ settings.TEMPLATE_CONTEXT_PROCESSORS += (request_processor,)
+
+ self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(
+ os.path.dirname(__file__),
+ 'templates'
+ ),
+ )
+
# now we start singleton. singleton are tested on singleton_tests.py
self.breadcrumbs = Breadcrumbs()
@@ -30,6 +44,8 @@ def setUp(self):
def tearDown(self):
settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+ settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
+ settings.TEMPLATE_CONTEXT_PROCESSORS = self.old_TEMPLATE_CONTEXT_PROCESSORS
# kill singleton
self.breadcrumbs._drop_it()
@@ -62,3 +78,10 @@ def test_breadcrumbs_params_and_iteration(self):
b(self.data[3:5])
for i, bd in enumerate(b):
self.assertEqual(bd.__dict__, Breadcrumb(**self.data[i]).__dict__)
+
+
+ def test_request_breadcrumbs(self):
+ response = self.client.get('/page1/')
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response,
+ '<ul id="breadcrumbs"><li><a href="/page1/">Page 1</a></li></ul>')
@@ -0,0 +1,65 @@
+# coding: utf-8
+import os
+from django.conf import settings
+from django.contrib.flatpages.models import FlatPage
+from django.test import TestCase
+from django.utils.datastructures import SortedDict
+from breadcrumbs.breadcrumbs import Breadcrumb, Breadcrumbs
+
+
+class FlatpagesTest(TestCase):
+ fixtures = ['sample_flatpages_for_breadcrumbs.json']
+
+ def setUp(self):
+ breadcrumbs_middleware_class = 'breadcrumbs.middleware.BreadcrumbsMiddleware'
+ flatpages_middleware_class = 'breadcrumbs.middleware.FlatpageFallbackMiddleware'
+
+ # remove breadcrumbs middlewares to assert that we set correct
+ # order
+ self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
+ settings.MIDDLEWARE_CLASSES = [mid for mid \
+ in self.old_MIDDLEWARE_CLASSES if mid not in \
+ (breadcrumbs_middleware_class, flatpages_middleware_class)]
+ settings.MIDDLEWARE_CLASSES += [
+ breadcrumbs_middleware_class,
+ flatpages_middleware_class
+ ]
+ self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(
+ os.path.dirname(__file__),
+ 'templates'
+ ),
+ )
+ # now we start singleton. singleton are tested on singleton_tests.py
+ self.breadcrumbs = Breadcrumbs()
+
+ def tearDown(self):
+ settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+
+ # kill singleton
+ self.breadcrumbs._drop_it()
+
+ def test_flatpages_fixture_loaded(self):
+ flat1 = FlatPage.objects.get(pk=1)
+ self.assertEqual(flat1.title, u"Flat Page 1")
+ self.assertEqual(flat1.content, u"This is flat 1")
+ flat2 = FlatPage.objects.get(pk=2)
+ self.assertEqual(flat2.title, u"Flat page 2")
+ self.assertEqual(flat2.content, u"This is flat 2 under flat 1")
+
+ def test_404_flatpage(self):
+ response = self.client.get('/404_not_found/')
+ self.assertEqual(response.status_code, 404)
+ # self.assertContains(response, "<p>Isn't it flat!</p>")
+
+ def test_fallback_flatpage(self):
+ response = self.client.get('/flat01/')
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response,
+ '<ul id="breadcrumbs"><li><a href="/flat01/">Flat Page 1</a></li></ul>')
+
+ response = self.client.get('/flat01/flat02/')
+ self.assertEqual(response.status_code, 200)
+ self.assertContains(response,
+ '<ul id="breadcrumbs"><li><a href="/flat01/">Flat Page 1</a> &raquo; </li><li><a href="/flat01/flat02/">Flat page 2</a></li></ul>')
@@ -0,0 +1,9 @@
+<!doctype html>
+<html>
+<head>
+ <title>Not Found</title>
+</head>
+<body>
+ 404 - Not found
+</body>
+</html>
@@ -0,0 +1,17 @@
+{% spaceless %}<!DOCTYPE html>
+<html>
+<head>
+<title>{{ flatpage.title }}</title>
+</head>
+<body>
+{% block breadcrumb %}
+<ul id="breadcrumbs">
+{% for breadcrumb in request.breadcrumbs %}
+<li><a href="{{ breadcrumb.url }}">{{ breadcrumb.name }}</a>{% if not forloop.last %} &raquo; {% endif %}</li>
+{% endfor %}
+</ul>
+{% endblock breadcrumb %}
+{% block body %}
+{% endblock body %}
+</body>
+</html>{% endspaceless %}
@@ -0,0 +1,4 @@
+{% extends "base.html" %}
+{% block body %}
+<p>{{ flatpage.content }}</p>
+{% endblock body %}
@@ -0,0 +1,4 @@
+{% extends "base.html" %}
+{% block body %}
+Page 1 :)
+{% endblock body %}
@@ -0,0 +1,7 @@
+from django.conf.urls import patterns, include
+from .views import page1
+# special urls for flatpage test cases
+urlpatterns = patterns('',
+ (r'^page1/', page1),
+)
+
@@ -0,0 +1,9 @@
+# -*- coding: utf-8 -*-
+
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+
+def page1(request):
+ request.breadcrumbs("Page 1", request.get_full_path())
+ return render_to_response('page1.html', {},
+ context_instance=RequestContext(request))
View
@@ -7,6 +7,8 @@
if not settings.configured:
settings_dict = dict(
+ SITE_ID=1,
+ ROOT_URLCONF='breadcrumbs.tests.urls',
INSTALLED_APPS=(
'django.contrib.contenttypes',
'django.contrib.sites',
View
Binary file not shown.
@@ -130,7 +130,7 @@
'django.contrib.staticfiles',
'django.contrib.flatpages',
# Uncomment the next line to enable the admin:
- # 'django.contrib.admin',
+ 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'webui',
@@ -1,8 +1,8 @@
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+from django.contrib import admin
+admin.autodiscover()
urlpatterns = patterns('',
# Examples:
@@ -13,7 +13,7 @@
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
- # url(r'^admin/', include(admin.site.urls)),
+ url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'webui.views.home', name='home'),
url(r'^someview/$', 'webui.views.someview', name='someview'),
(r'^pages/', include('breadcrumbs.urls')),

0 comments on commit d81f5dd

Please sign in to comment.