Skip to content

Instantly share code, notes, and snippets.

@rozza
Created May 18, 2011 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rozza/978162 to your computer and use it in GitHub Desktop.
Save rozza/978162 to your computer and use it in GitHub Desktop.
mongoengine/issues/166
# -*- coding: utf-8 -*-
from django.conf import settings
import unittest
from mongoengine import *
from django.template.defaultfilters import length
from django.template import Context, Template
class TemplateFilterTest(unittest.TestCase):
def setUp(self):
connect(db='mongoenginetest')
settings.configure()
class Note(Document):
text = StringField()
self.Note = Note
def tearDown(self):
self.Note.drop_collection()
def test_filter(self):
"""Ensure that a queryset and filters work as expected
"""
for i in xrange(1, 101):
n = self.Note(name="Note: %s" % i)
n.save()
# Check the count
self.assertEqual(self.Note.objects.count(), 100)
# Get the first 10 and confirm
notes = self.Note.objects[:10]
self.assertEqual(len(notes), 10)
self.assertEqual(notes.count(), 10)
# Test djangos template filters
self.assertEqual(length(notes), 10)
t = Template("{{ notes|length }}")
c = Context({"notes": notes})
self.assertEqual(t.render(c), "10")
# Test with limit
notes = self.Note.objects.limit(10)
self.assertEqual(len(notes), 10)
self.assertEqual(notes.count(), 10)
# Test djangos template filters
self.assertEqual(length(notes), 10)
t = Template("{{ notes|length }}")
c = Context({"notes": notes})
self.assertEqual(t.render(c), "10")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment