Testing Django-CMS sites

I have been struggling for a while to get a test suite running for my Django-CMS sites, and had a breakthrough today which I hope can help others.

There were two problems.

I would get a string of errors like this:

FATAL ERROR - The following SQL query failed:
SELECT kc.constraint_name, kc.column_name, c.constraint_type
FROM information_schema.constraint_column_usage AS kc
JOIN information_schema.table_constraints AS c ON
kc.table_schema = c.table_schema AND
kc.table_name = c.table_name AND
kc.constraint_name = c.constraint_name
WHERE
kc.table_schema = %s AND
kc.table_name = %s

The error was: no such table: information_schema.constraint_column_usage

This is actually a South problem. You need to add some lines to your settings.py:

import sys
if 'test' in sys.argv:
    SOUTH_TESTS_MIGRATE = False

Thanks to this Stackoverflow post for that solution, though it does not mention the above error. That post also points out that using a sqlite database for tests is much faster.

The second problem was this:

TemplateSyntaxError: You must enable the 'sekizai.context_processors.sekizai'
template context processor or use 'sekizai.context.SekizaiContext' 
to render your templates.

This was confusing because I was using sekizai correctly in my template.

This post pointed me in the right direction here. The problem was Django was raising an exception but I was never getting to see it – just this much less helpful message.

This Stackoverflow post explained how to enable logging of errors. I copied in the changes to settings.py, wrapping them inside the if 'test' statement.

Then when I ran

./manage.py test

I got a much more useful error message: I had forgotten to set up a table that my template was assuming would exist. Easily fixed!

Hope that helps someone else.

My first, simple, test looks like this:

from django.test import TestCase
from django.contrib.auth.models import User
from cms.api import create_page, add_plugin
import myapp.models

def create_globals():
    """
    The templates assume this exists
    """
    return myapp.models.GlobalSettings.objects.create()

class FirstTest(TestCase):
    def test_template(self):
        """
        Test the template can load
        """
        create_globals()
        superuser = User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
        template="test.html"
        page = create_page("home", template, "en", created_by=superuser, published=True)
        page = create_page("second_test", template, "en", created_by=superuser, published=True)
        response = self.client.get('/')
        response = self.client.get('/second_test/')

I have recently discovered this book by Harry Percival, on how to incorporate testing into your Django site.  I highly recommend it.

  

4 thoughts on “Testing Django-CMS sites”

    1. Great, thanks for letting me know about CMSTestCase – I hadn’t come across that before. I’ve been recreating the necessary pages in my tests each time, and assigning a reverse_id to them (“id” in the Advanced Settings panel of the admin) so I can refer to them in my views (which can be a bit ugly, especially now there are draft and published versions of each page; it also requires more effort to handle multiple sites and languages), e.g:
      url = Page.objects.get(reverse_id='pageid', publisher_is_draft=False, site=get_current_site(request) ).get_absolute_url()

  1. Thanks for posting this. I’d been putting up with the
    “no such table: information_schema.constraint_column_usage” after adding django-cms to an existing project. Seems to be working fine without South migrations enabled.

Comments are closed.