Created
April 30, 2015 18:24
-
-
Save edgabaldi/ffdffa60f2add650f052 to your computer and use it in GitHub Desktop.
mocking queryset example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import Count | |
from .models import DummyModel | |
class ExampleClass(object): | |
def get_queryset(self) | |
filter = { | |
'foo': 'bar', | |
'baz': True | |
} | |
quertset = DummyModel.objects.filter(**filter).annotate(total=Count('related__field')).values('field', 'total') | |
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mock import patch | |
from unittest import TestCase | |
from example import ExampleClass | |
class ExampleClassTest(TestCase): | |
@patch('example.DummyModel') | |
@patch('example.Count') | |
def test_get_queryset(self, _count, _dummymodel): | |
_count.return_value='counting' | |
obj = ExampleClass() | |
queryset = obj.get_queryset() | |
_dummymodel.objects.filter.assert_called_once_with(foo='bar', baz=True) | |
_dummymodel.objects.filter.return_value.annotate.assert_called_once_with(total='counting') | |
_dummy.objects.filter.return_value.annotate.return_value.values.assert_called_once_with('field', 'total') | |
self.assertEqual(queryset, _dummymodel.objects.filter.return_value.annotate.return_value.values.return_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment