Permalink
Browse files

Added framework for writing non-model-based tests, and added tests fo…

…r cache and templates

from the old django/tests location (which has been removed).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@367 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 8483f0a commit 440a2a97e9a8420ddf6186d4e948662bb3015e7d @jacobian jacobian committed Aug 1, 2005
@@ -1,119 +0,0 @@
-"""
-Unit tests for django.core.cache
-
-If you don't have memcached running on localhost port 11211, the memcached tests
-will fail.
-"""
-
-from django.core import cache
-import unittest
-import time
-
-# functions/classes for complex data type tests
-def f():
- return 42
-class C:
- def m(n):
- return 24
-
-class CacheBackendsTest(unittest.TestCase):
-
- def testBackends(self):
- sc = cache.get_cache('simple://')
- mc = cache.get_cache('memcached://127.0.0.1:11211/')
- self.failUnless(isinstance(sc, cache._SimpleCache))
- self.failUnless(isinstance(mc, cache._MemcachedCache))
-
- def testInvalidBackends(self):
- self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'nothing://foo/')
- self.assertRaises(cache.InvalidCacheBackendError, cache.get_cache, 'not a uri')
-
- def testDefaultTimeouts(self):
- sc = cache.get_cache('simple:///?timeout=15')
- mc = cache.get_cache('memcached://127.0.0.1:11211/?timeout=15')
- self.assertEquals(sc.default_timeout, 15)
- self.assertEquals(sc.default_timeout, 15)
-
-class SimpleCacheTest(unittest.TestCase):
-
- def setUp(self):
- self.cache = cache.get_cache('simple://')
-
- def testGetSet(self):
- self.cache.set('key', 'value')
- self.assertEqual(self.cache.get('key'), 'value')
-
- def testNonExistantKeys(self):
- self.assertEqual(self.cache.get('does not exist'), None)
- self.assertEqual(self.cache.get('does not exist', 'bang!'), 'bang!')
-
- def testGetMany(self):
- self.cache.set('a', 'a')
- self.cache.set('b', 'b')
- self.cache.set('c', 'c')
- self.cache.set('d', 'd')
- self.assertEqual(self.cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
- self.assertEqual(self.cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
-
- def testDelete(self):
- self.cache.set('key1', 'spam')
- self.cache.set('key2', 'eggs')
- self.assertEqual(self.cache.get('key1'), 'spam')
- self.cache.delete('key1')
- self.assertEqual(self.cache.get('key1'), None)
- self.assertEqual(self.cache.get('key2'), 'eggs')
-
- def testHasKey(self):
- self.cache.set('hello', 'goodbye')
- self.assertEqual(self.cache.has_key('hello'), True)
- self.assertEqual(self.cache.has_key('goodbye'), False)
-
- def testDataTypes(self):
- items = {
- 'string' : 'this is a string',
- 'int' : 42,
- 'list' : [1, 2, 3, 4],
- 'tuple' : (1, 2, 3, 4),
- 'dict' : {'A': 1, 'B' : 2},
- 'function' : f,
- 'class' : C,
- }
- for (key, value) in items.items():
- self.cache.set(key, value)
- self.assertEqual(self.cache.get(key), value)
-
- def testExpiration(self):
- self.cache.set('expire', 'very quickly', 1)
- time.sleep(2)
- self.assertEqual(self.cache.get('expire'), None)
-
- def testCull(self):
- c = cache.get_cache('simple://?max_entries=9&cull_frequency=3')
- for i in range(10):
- c.set('culltest%i' % i, i)
- n = 0
- for i in range(10):
- if c.get('culltest%i' % i):
- n += 1
- self.assertEqual(n, 6)
-
- def testCullAll(self):
- c = cache.get_cache('simple://?max_entries=9&cull_frequency=0')
- for i in range(10):
- c.set('cullalltest%i' % i, i)
- for i in range(10):
- self.assertEqual(self.cache.get('cullalltest%i' % i), None)
-
-class MemcachedCacheTest(SimpleCacheTest):
-
- def setUp(self):
- self.cache = cache.get_cache('memcached://127.0.0.1:11211/')
-
- testCull = testCullAll = lambda s: None
-
-def tests():
- s = unittest.TestLoader().loadTestsFromName(__name__)
- unittest.TextTestRunner(verbosity=0).run(s)
-
-if __name__ == "__main__":
- tests()
@@ -1,102 +0,0 @@
-from django.core import template, template_loader
-
-# SYNTAX --
-# 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
-TEMPLATE_TESTS = {
- # Standard template with no inheritance
- 'test01': ("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}", {}, '1_3_'),
-
- # Standard two-level inheritance
- 'test02': ("{% extends 'test01' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
-
- # Three-level with no redefinitions on third level
- 'test03': ("{% extends 'test02' %}", {}, '1234'),
-
- # Two-level with no redefinitions on second level
- 'test04': ("{% extends 'test01' %}", {}, '1_3_'),
-
- # Two-level with double quotes instead of single quotes
- 'test05': ('{% extends "test02" %}', {}, '1234'),
-
- # Three-level with variable parent-template name
- 'test06': ("{% extends foo %}", {'foo': 'test02'}, '1234'),
-
- # Two-level with one block defined, one block not defined
- 'test07': ("{% extends 'test01' %}{% block second %}5{% endblock %}", {}, '1_35'),
-
- # Three-level with one block defined on this level, two blocks defined next level
- 'test08': ("{% extends 'test02' %}{% block second %}5{% endblock %}", {}, '1235'),
-
- # Three-level with second and third levels blank
- 'test09': ("{% extends 'test04' %}", {}, '1_3_'),
-
- # Three-level with space NOT in a block -- should be ignored
- 'test10': ("{% extends 'test04' %} ", {}, '1_3_'),
-
- # Three-level with both blocks defined on this level, but none on second level
- 'test11': ("{% extends 'test04' %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {}, '1234'),
-
- # Three-level with this level providing one and second level providing the other
- 'test12': ("{% extends 'test07' %}{% block first %}2{% endblock %}", {}, '1235'),
-
- # Three-level with this level overriding second level
- 'test13': ("{% extends 'test02' %}{% block first %}a{% endblock %}{% block second %}b{% endblock %}", {}, '1a3b'),
-
- # A block defined only in a child template shouldn't be displayed
- 'test14': ("{% extends 'test01' %}{% block newblock %}NO DISPLAY{% endblock %}", {}, '1_3_'),
-
- # A block within another block
- 'test15': ("{% extends 'test01' %}{% block first %}2{% block inner %}inner{% endblock %}{% endblock %}", {}, '12inner3_'),
-
- # A block within another block (level 2)
- 'test16': ("{% extends 'test15' %}{% block inner %}out{% endblock %}", {}, '12out3_'),
-
- # {% load %} tag (parent -- setup for test-exception04)
- 'test17': ("{% load polls.polls %}{% block first %}1234{% endblock %}", {}, '1234'),
-
- # {% load %} tag (standard usage, without inheritance)
- 'test18': ("{% load polls.polls %}{% voteratio choice poll 400 %}5678", {}, '05678'),
-
- # {% load %} tag (within a child template)
- 'test19': ("{% extends 'test01' %}{% block first %}{% load polls.polls %}{% voteratio choice poll 400 %}5678{% endblock %}", {}, '1056783_'),
-
- # Raise exception for invalid template name
- 'test-exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
-
- # Raise exception for invalid template name (in variable)
- 'test-exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
-
- # Raise exception for extra {% extends %} tags
- 'test-exception03': ("{% extends 'test01' %}{% block first %}2{% endblock %}{% extends 'test16' %}", {}, template.TemplateSyntaxError),
-
- # Raise exception for custom tags used in child with {% load %} tag in parent, not in child
- 'test-exception04': ("{% extends 'test17' %}{% block first %}{% votegraph choice poll 400 %}5678{% endblock %}", {}, template.TemplateSyntaxError),
-}
-
-# This replaces the standard template_loader.
-def test_template_loader(template_name):
- try:
- return TEMPLATE_TESTS[template_name][0]
- except KeyError:
- raise template.TemplateDoesNotExist, template_name
-template_loader.load_template_source = test_template_loader
-
-def run_tests():
- tests = TEMPLATE_TESTS.items()
- tests.sort()
- for name, vals in tests:
- try:
- output = template_loader.get_template(name).render(template.Context(vals[1]))
- except Exception, e:
- if e.__class__ == vals[2]:
- print "%s -- Passed" % name
- else:
- print "%s -- FAILED. Got %s, exception: %s" % (name, e.__class__, e)
- continue
- if output == vals[2]:
- print "%s -- Passed" % name
- else:
- print "%s -- FAILED. Expected %r, got %r" % (name, vals[2], output)
-
-if __name__ == "__main__":
- run_tests()
Oops, something went wrong.

0 comments on commit 440a2a9

Please sign in to comment.