def test_chaining_calls_works(self): """ Chained calls to QS-returning methods should return new QuerySetMocks. """ qs = QuerySetMock(None, 1, 2, 3) qs.all().filter(filter_arg="dummy") qs.filter(filter_arg="dummy").order_by("-date")
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_chained_calls_return_new_querysetmocks(self): qs = QuerySetMock(None, 1, 2, 3) qs_all = qs.all() qs_filter = qs.filter() qs_all_filter = qs.filter().all() self.assertIsNot(qs_all, qs_filter) self.assertIsNot(qs_filter, qs_all_filter)
def invoiceitems(self): """ Gets the invoice items associated with this upcoming invoice. This differs from normal (non-upcoming) invoices, in that upcoming invoices are in-memory and do not persist to the database. Therefore, all of the data comes from the Stripe API itself. Instead of returning a normal queryset for the invoiceitems, this will return a mock of a queryset, but with the data fetched from Stripe - It will act like a normal queryset, but mutation will silently fail. """ return QuerySetMock(InvoiceItem, *self._invoiceitems)
def test(self, score_class_mock): save_mock = Mock(return_value=None) score_class_mock.return_value.save = save_mock storage = Storage() storage.populate() self.assertEqual(save_mock.call_count, 1) score = Score() score.score = 1234 score_class_mock.objects.all.return_value.order_by.return_value = QuerySetMock( score_class_mock, score) score = storage.get_score() self.assertEqual(score, 1234)
def __mock_cradmin_instance(self, roles): cradmin_instance = mock.MagicMock() cradmin_instance.get_rolequeryset.return_value.count = mock.MagicMock( return_value=len(roles)) cradmin_instance.get_rolequeryset.return_value.__len__.return_value = len( roles) cradmin_instance.get_titletext_for_role = lambda role: role.title cradmin_instance.get_descriptionhtml_for_role = lambda role: format_html( u'<p>{}<p>', role.description) cradmin_instance.get_roleid = lambda role: role.id cradmin_instance.rolefrontpage_url = lambda roleid: '/role/{}'.format( roleid) cradmin_instance.get_rolequeryset.return_value = QuerySetMock( None, *roles) return cradmin_instance
def test_vals_returned(self): qs = QuerySetMock(None, 1, 2, 3) self.assertEquals(list(qs), [1, 2, 3])
def _get(*a, **k): return QuerySetMock(model, *return_value)
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)
def test_count_is_scalar(self): qs = QuerySetMock(None, 1, 2, 3) self.assertEquals(qs.count(), 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_last(self): qs = QuerySetMock(None, 1, 2, 3) self.assertEquals(qs.last(), 3)
def test_first_empty_qs(self): qs = QuerySetMock(None) self.assertEquals(qs.last(), None)
def test_first(self): qs = QuerySetMock(None, 1, 2, 3) self.assertEquals(qs.first(), 1)
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_accessed(self): """ Exceptions can raise on getitem, too. """ qs = QuerySetMock(None, TestException()) self.assertRaises(TestException, lambda x: x[0], qs)
def get_snippet_queryset(): s1 = Snippet(id=1, title='Fork bomb', code=':(){ :|: & };:', language='bash') s2 = Snippet(id=2, title='French flag', code="print((u'\x1b[3%s;1m\u2588'*78+u'\n')%((4,)*26+(7,)*26+(1,)*26)*30)") snippets_qs = QuerySetMock(Snippet, s1, s2) return snippets_qs