Permalink
Browse files

Add a bunch of query tests, update some docs

  • Loading branch information...
1 parent 7ab8e2c commit 14a1877efa254561256405e6bd20513d6ef937de @boydjj boydjj committed Feb 7, 2013
Showing with 88 additions and 8 deletions.
  1. +15 −0 README.rst
  2. +1 −8 mock_django/query.py
  3. +72 −0 tests/mock_django/query/tests.py
View
15 README.rst
@@ -3,6 +3,21 @@ mock-django
A simple library for mocking certain Django behavior, such as the ORM.
+Using mock-django objects
+-------------------------
+Inside your virtualenv::
+
+>>> from django.conf import settings # required to confince Django we've properly configured
+>>> settings.configure()
+>>> from mock_django.query import QuerySetMock
+>>> class Post(object): pass
+>>> qs = QuerySetMock(Post, 1, 2, 3)
+>>> list(qs.all())
+[1, 2, 3]
+>>> qs.count()
+3
+
+
Testing
-------
View
9 mock_django/query.py
@@ -17,18 +17,11 @@ def QuerySetMock(model, *return_value):
Set the results to two items:
>>> objects = QuerySetMock(Post, 'return', 'values')
- >>> assert objects.filter() == objects.all()
+ >>> assert list(objects.filter()) == list(objects.all())
Force an exception:
>>> objects = QuerySetMock(Post, Exception())
-
- Note that only methods returning querysets are currently
- explicitly supported; since we use SharedMock, others all behave
- as if they did, so use with caution:
-
- >>> objects.count() == objects.all()
- True
"""
def make_get(self, model):
View
72 tests/mock_django/query/tests.py
@@ -0,0 +1,72 @@
+from mock_django.query import QuerySetMock
+from unittest2 import TestCase
+
+class TestException(Exception):
+ pass
+
+class TestModel(object):
+ def foo(self):
+ pass
+
+ def bar(self):
+ return 'bar'
+
+class QuerySetTestCase(TestCase):
+ def test_vals_returned(self):
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertEquals(list(qs), [1, 2, 3])
+
+ def test_qs_generator_inequality(self):
+ """
+ Each QuerySet-returning method's return value is unique.
+ """
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertNotEqual(qs.all(), qs.filter())
+ self.assertNotEqual(qs.filter(), qs.order_by())
+
+ def test_qs_yield_equality(self):
+ """
+ The generators may not the same, but they do produce the same output.
+ """
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertEqual(list(qs.all()), list(qs.filter()))
+
+ def test_qs_method_takes_arg(self):
+ """
+ QS-returning methods are impotent, but they do take args.
+ """
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertEqual(list(qs.order_by('something')), [1, 2, 3])
+
+ def test_raises_exception_when_evaluated(self):
+ """
+ Exception raises when you actually use a QS-returning method.
+ """
+ qs = QuerySetMock(None, TestException())
+ self.assertRaises(TestException, list, qs.all())
+
+ def test_raises_exception_when_accessed(self):
+ """
+ Exceptions can raise on getitem, too.
+ """
+ qs = QuerySetMock(None, TestException())
+ self.assertRaises(TestException, lambda x: x[0], qs)
+
+ # Test reserved methods
+ def test_count_is_scalar(self):
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertEqual(qs.count(), 3)
+
+ def test_exists_is_boolean(self):
+ qs = QuerySetMock(None)
+ self.assertFalse(qs.exists())
+
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertTrue(qs.exists())
+
+ def test_objects_returned_do_not_change_type(self):
+ """
+ Not sure this is the behavior we want, but it's the behavior we have.
+ """
+ qs = QuerySetMock(TestModel, 1, 2, 3)
+ self.assertNotIsInstance(qs[0], TestModel)

0 comments on commit 14a1877

Please sign in to comment.