Serotonin Storm

source>flatblocks>tests.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
import unittest

from django import template
from django.test import TestCase
from django.core.cache import cache
from django.contrib.auth.models import User

from flatblocks import models
from flatblocks.settings import CACHE_PREFIX


class BasicTests(TestCase):
    urls = 'flatblocks.urls'

    def setUp(self):
        self.testblock = models.FlatBlock()
        self.testblock.slug = 'block'
        self.testblock.header = 'HEADER'
        self.testblock.content = 'CONTENT'
        self.testblock.save()
        self.admin = User.objects.create_superuser('admin', 'admin@localhost', 'adminpwd')
    
    def testURLConf(self):
        self.assertEquals(self.client.get('/edit/1/').template.name, 'admin/login.html')
        self.client.login(username='admin', password='adminpwd')
        self.assertEquals(self.client.get('/edit/1/').template.name, 'flatblocks/edit.html')

    def testCacheReset(self):
        """
        Tests if FlatBlock.save() resets the cache.
        """
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 60 %}')
        tpl.render({})
        name = '%sblock' % CACHE_PREFIX
        self.assertNotEquals(None, cache.get(name))
        block = models.FlatBlock.objects.get(slug='block')
        block.header = 'UPDATED'
        block.save()
        self.assertEquals(None, cache.get(name))
        
    def tearDown(self):
        self.testblock.delete()

class TagTests(unittest.TestCase):
    def setUp(self):
        self.testblock = models.FlatBlock()
        self.testblock.slug = 'block'
        self.testblock.header = 'HEADER'
        self.testblock.content = 'CONTENT'
        self.testblock.save()

    def testLoadingTaglib(self):
        """Tests if the taglib defined in this app can be loaded"""
        tpl = template.Template('{% load flatblock_tags %}')
        tpl.render({})

    def testMissingBlock(self):
        """Tests if a missing block will simply return an empty string"""
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "missing_block" %}')
        self.assertEqual('', tpl.render({}).strip())

    def testExistingPlain(self):
        tpl = template.Template('{% load flatblock_tags %}{% plain_flatblock "block" %}')
        self.assertEqual(u'CONTENT', tpl.render({}).strip()) 

    def testExistingTemplate(self):
        expected = """<div class="flatblock block-block">
    
    <h2 class="title">HEADER</h2>
    
    <div class="content">CONTENT</div>
</div>"""
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" %}')
        self.assertEqual(expected, tpl.render({}))

    def testUsingMissingTemplate(self):
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" using "missing_template.html" %}')
        exception = template.TemplateDoesNotExist
        self.assertRaises(exception, tpl.render, {})

    def testSyntax(self):
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" %}')
        tpl.render({})
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 %}')
        tpl.render({})
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" using "flatblocks/flatblock.html" %}')
        tpl.render({})
        tpl = template.Template('{% load flatblock_tags %}{% flatblock "block" 123 using "flatblocks/flatblock.html" %}')
        tpl.render({})

    def testBlockAsVariable(self):
        tpl = template.Template('{% load flatblock_tags %}{% flatblock blockvar %}')
        tpl.render({'blockvar': 'block'})

    def tearDown(self):
        self.testblock.delete()

class ModelTestCase(unittest.TestCase):
    """A selection of testcases regarding the models themselves"""
    pass