Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 2142b5ebd93047e644930027913d1c15ab089a62 @dcramer committed Feb 6, 2012
2 .gitignore
@@ -0,0 +1,2 @@
+*.pyc
+*.egg-info/
0 README.rst
No changes.
2 mock_django/__init__.py
@@ -0,0 +1,2 @@
+from .managers import *
+from .models import *
20 mock_django/managers.py
@@ -0,0 +1,20 @@
+import mock
+
+__all__ = ('ManagerMock',)
+
+
+def ManagerMock(manager, return_value=[]):
+ """
+ >>> objects = ManagerMock(Post.objects, ['queryset', 'result'])
+ >>> assert objects.filter() == objects.all()
+ """
+ class ManagerMock(mock.MagicMock):
+ def _get_child_mock(self, **kwargs):
+ name = kwargs.get('name', '')
+ if name[:2] == name[-2:] == '__':
+ return super(ManagerMock, self)._get_child_mock(**kwargs)
+ return self
+ m = ManagerMock()
+ m.__iter__.return_value = iter(return_value)
+ m.__getitem__ = lambda s, n: list(s)[n]
+ return m
17 mock_django/models.py
@@ -0,0 +1,17 @@
+import mock
+
+__all__ = ('ModelMock',)
+
+
+def ModelMock(model):
+ """
+ >>> Post = ModelMock(Post)
+ >>> assert post.pk == post.id
+ """
+ class ModelMock(mock.MagicMock):
+ def _get_child_mock(self, **kwargs):
+ name = kwargs.get('name', '')
+ if name == 'pk':
+ return self.id
+ return super(ModelMock, self)._get_child_mock(**kwargs)
+ return ModelMock(spec=model())
19 setup.py
@@ -0,0 +1,19 @@
+from setuptools import setup, find_packages
+
+setup(
+ name='mock-django',
+ version='0.1.0',
+ description='',
+ author='David Cramer',
+ author_email='dcramer@gmail.com',
+ url='http://github.com/dcramer/mock-django',
+ packages=find_packages(),
+ install_requires=[
+ 'nose',
+ 'unittest2',
+ 'mock>=0.8',
+ ],
+ test_suite='nose.collector',
+ zip_safe=False,
+ include_package_data=True,
+)
0 tests/__init__.py
No changes.
0 tests/mock_django/__init__.py
No changes.
0 tests/mock_django/managers/__init__.py
No changes.
22 tests/mock_django/managers/tests.py
@@ -0,0 +1,22 @@
+import mock
+from mock_django.managers import ManagerMock
+from unittest2 import TestCase
+
+
+def make_manager():
+ manager = mock.MagicMock(spec=(
+ 'all', 'filter', 'order_by',
+ ))
+ return manager
+
+
+class ManagerMockTestCase(TestCase):
+ def test_iter(self):
+ manager = make_manager()
+ mock = ManagerMock(manager, ['foo'])
+ self.assertEquals(list(mock.all()), ['foo'])
+
+ def test_getitem(self):
+ manager = make_manager()
+ mock = ManagerMock(manager, ['foo'])
+ self.assertEquals(mock.all()[0], 'foo')
0 tests/mock_django/models/__init__.py
No changes.
13 tests/mock_django/models/tests.py
@@ -0,0 +1,13 @@
+from mock_django.models import ModelMock
+from unittest2 import TestCase
+
+
+class Model(object):
+ id = '1'
+ pk = '2'
+
+
+class ModelMockTestCase(TestCase):
+ def test_pk_alias(self):
+ mock = ModelMock(Model)
+ self.assertEquals(mock.id, mock.pk)

0 comments on commit 2142b5e

Please sign in to comment.