Advertisement
Guest User

Untitled

a guest
May 13th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. Thanks for post. But i have a question - how do you test these methods?
  2.  
  3. simple example:
  4.  
  5. def filter_by_published(self):
  6. return self.filter(is_published=True)
  7.  
  8.  
  9. Thats ok, i just create instance with is_published==True, is_pubslished==False and then check, which of instance in these queryset method response.
  10.  
  11. more complecated
  12.  
  13. def filter_by_ready_for_sending(self):
  14. return self.filter_by_published().filter(date_send=now())
  15.  
  16.  
  17. How i do it now. I just mock filter_by_published with QuerySet.all method and testing second filter.
  18.  
  19. class TestQuerySetMethods(TestCase):
  20. def test_filter_by_ready_for_sending(self):
  21. with patch('filter_by_published', QuerySet.all):
  22. # test that filtering by date works right
  23. # i dont care, how filter_by_published works,
  24. # because i've already tested it before
  25.  
  26.  
  27. even more complecated
  28.  
  29. def filter_by_ready_for_buying(self):
  30. return (self.filter_by_published()
  31. .filter(price__isnull=False)
  32. .filter_by_delivered())
  33.  
  34.  
  35. And now i have to mock default .filter, and custom filter_by_published, filter_by_delivered:
  36.  
  37. with patch('filter_by_published', QuerySet.all):
  38. with patch('filter', QuerySet.all):
  39. with patch('filter_by_delivered', Mock(return_value=SomeExpectedSet)):
  40. response = Books.objects.filter_by_ready_for_buying()
  41. self.assertEqual(set(response), set(SomeExpectedSet))
  42.  
  43. I create 3 test methods to test every queryset method, which appeares in filter_by_ready_for_buying.
  44. But what if i'll add new filtering to this method? I'll have to add new mock for every test method.
  45.  
  46. What if it will be more than 1 default filter method? My mock will mock them all and SomeExpectedSet will not
  47. be representative.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement