Permalink
Browse files

Merge pull request #14 from mapmyfitness/master

Add tests for QuerySetMock and ModelMock
  • Loading branch information...
2 parents 7ab8e2c + f4be5f7 commit fa8de1ffc989a7e2a1ad62f988683148fe58350d @dcramer committed Mar 29, 2013
Showing with 122 additions and 8 deletions.
  1. +19 −0 README.rst
  2. +1 −8 mock_django/query.py
  3. +30 −0 tests/mock_django/models/tests.py
  4. +72 −0 tests/mock_django/query/tests.py
View
@@ -3,6 +3,25 @@ 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
+ >>> settings.configure() # required to convince Django it's properly configured
+ >>> 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
+ >>> qs.all().filter()
+ [1, 2, 3]
+
+See tests for more examples.
+
+
Testing
-------
View
@@ -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):
@@ -1,3 +1,4 @@
+from mock import MagicMock
from mock_django.models import ModelMock
from unittest2 import TestCase
@@ -6,8 +7,37 @@ class Model(object):
id = '1'
pk = '2'
+ def foo(self):
+ pass
+
+ def bar(self):
+ return 'bar'
+
class ModelMockTestCase(TestCase):
def test_pk_alias(self):
mock = ModelMock(Model)
self.assertEquals(mock.id, mock.pk)
+
+ def test_only_model_attrs_exist(self):
+ """
+ ModelMocks have only the members that the Model has.
+ """
+ mock = ModelMock(Model)
+ self.assertRaises(AttributeError, lambda x: x.baz, mock)
+
+ def test_model_attrs_are_mocks(self):
+ """
+ ModelMock members are Mocks, not the actual model members.
+ """
+ mock = ModelMock(Model)
+ self.assertNotEquals(mock.bar(), 'bar')
+ self.assertIsInstance(mock, MagicMock)
+
+ def test_attrs_are_not_identical(self):
+ """
+ Each member of a ModelMock is unique.
+ """
+ mock = ModelMock(Model)
+ self.assertIsNot(mock.foo, mock.bar)
+ self.assertIsNot(mock.foo, mock.id)
@@ -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.assertNotEquals(qs.all(), qs.filter())
+ self.assertNotEquals(qs.filter(), qs.order_by())
+
+ def test_qs_yield_equality(self):
+ """
+ The generators may not be the same, but they do produce the same output.
+ """
+ qs = QuerySetMock(None, 1, 2, 3)
+ self.assertEquals(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.assertEquals(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.assertEquals(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 fa8de1f

Please sign in to comment.