Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Thanks for post. But i have a question - how do you test these methods?
- simple example:
- def filter_by_published(self):
- return self.filter(is_published=True)
- Thats ok, i just create instance with is_published==True, is_pubslished==False and then check, which of instance in these queryset method response.
- more complecated
- def filter_by_ready_for_sending(self):
- return self.filter_by_published().filter(date_send=now())
- How i do it now. I just mock filter_by_published with QuerySet.all method and testing second filter.
- class TestQuerySetMethods(TestCase):
- def test_filter_by_ready_for_sending(self):
- with patch('filter_by_published', QuerySet.all):
- # test that filtering by date works right
- # i dont care, how filter_by_published works,
- # because i've already tested it before
- even more complecated
- def filter_by_ready_for_buying(self):
- return (self.filter_by_published()
- .filter(price__isnull=False)
- .filter_by_delivered())
- And now i have to mock default .filter, and custom filter_by_published, filter_by_delivered:
- with patch('filter_by_published', QuerySet.all):
- with patch('filter', QuerySet.all):
- with patch('filter_by_delivered', Mock(return_value=SomeExpectedSet)):
- response = Books.objects.filter_by_ready_for_buying()
- self.assertEqual(set(response), set(SomeExpectedSet))
- I create 3 test methods to test every queryset method, which appeares in filter_by_ready_for_buying.
- But what if i'll add new filtering to this method? I'll have to add new mock for every test method.
- What if it will be more than 1 default filter method? My mock will mock them all and SomeExpectedSet will not
- be representative.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement