Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add tests for QuerySetMock and ModelMock #14

Merged
merged 3 commits into from

2 participants

@boydjj

I don't think there's full coverage in these tests, but they should be useful in figuring out how to use QuerySetMock and ModelMock objects. Also updated some documentation that seems out of date.

@dcramer dcramer merged commit fa8de1f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 7, 2013
  1. Add a bunch of query tests, update some docs

    Jeremy Boyd authored
  2. @boydjj

    Clean up README a bit

    boydjj authored
Commits on Feb 8, 2013
  1. Add a few model tests to demonstrate attribute behavior; use assertEq…

    Jeremy Boyd authored
    …uals to fit codebase style
This page is out of date. Refresh to see the latest.
View
19 README.rst
@@ -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
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
30 tests/mock_django/models/tests.py
@@ -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)
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.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)
Something went wrong with that request. Please try again.