示例#1
0
class ApiViewSetTestCase(TestCase):
    def setUp(self):
        self.view_set = ApiViewSet()

    def test_get_queryset_model_defined(self, queryset_mock, get_form_mock,
                                        get_form_class_mock):
        self.view_set.queryset = None
        self.view_set.model = MagicMock()

        self.view_set.get_queryset()

        self.assertEqual(self.view_set.model.objects.all.call_count, 1)

    def test_get_queryset(self, queryset_mock, get_form_mock,
                          get_form_class_mock):
        self.view_set.model = MagicMock()

        self.view_set.get_queryset()

        self.assertEqual(self.view_set.model.objects.all.call_count, 0)

    @patch.object(ApiViewSet, 'filter_query')
    def test_get_queryset_form_valid(self, filter_query_mock, queryset_mock,
                                     get_form_mock, get_form_class_mock):
        filter_form = MagicMock()
        filter_form.is_valid.return_value = True
        get_form_mock.return_value = filter_form

        self.view_set.get_queryset()

        self.assertEqual(filter_query_mock.call_count, 1)

    @patch.object(ApiViewSet, 'form_invalid')
    def test_get_queryset_form_invalid(self, form_invalid_mock, queryset_mock,
                                       get_form_mock, get_form_class_mock):
        filter_form = MagicMock()
        filter_form.is_valid.return_value = False
        get_form_mock.return_value = filter_form

        self.view_set.get_queryset()

        self.assertEqual(form_invalid_mock.call_count, 1)

    @patch.object(ApiViewSet, 'order_query')
    def test_get_queryset_order_by(self, order_query_mock, queryset_mock,
                                   get_form_mock, get_form_class_mock):
        self.view_set.order_by = 'id'

        self.view_set.get_queryset()

        self.assertEqual(order_query_mock.call_count, 1)

    def tearDown(self):
        pass
示例#2
0
class ApiViewSetTestCase(TestCase):
    def setUp(self):
        self.view_set = ApiViewSet()

    def test_get_queryset_model_defined(self, queryset_mock, get_form_mock, get_form_class_mock):
        self.view_set.queryset = None
        self.view_set.model = MagicMock()

        self.view_set.get_queryset()

        self.assertEqual(self.view_set.model.objects.all.call_count, 1)

    def test_get_queryset(self, queryset_mock, get_form_mock, get_form_class_mock):
        self.view_set.model = MagicMock()

        self.view_set.get_queryset()

        self.assertEqual(self.view_set.model.objects.all.call_count, 0)

    @patch.object(ApiViewSet, 'filter_query')
    def test_get_queryset_form_valid(self, filter_query_mock, queryset_mock, get_form_mock, get_form_class_mock):
        filter_form = MagicMock()
        filter_form.is_valid.return_value = True
        get_form_mock.return_value = filter_form

        self.view_set.get_queryset()

        self.assertEqual(filter_query_mock.call_count, 1)

    @patch.object(ApiViewSet, 'form_invalid')
    def test_get_queryset_form_invalid(self, form_invalid_mock, queryset_mock, get_form_mock, get_form_class_mock):
        filter_form = MagicMock()
        filter_form.is_valid.return_value = False
        get_form_mock.return_value = filter_form

        self.view_set.get_queryset()

        self.assertEqual(form_invalid_mock.call_count, 1)

    @patch.object(ApiViewSet, 'order_query')
    def test_get_queryset_order_by(self, order_query_mock, queryset_mock, get_form_mock, get_form_class_mock):
        self.view_set.order_by = 'id'

        self.view_set.get_queryset()

        self.assertEqual(order_query_mock.call_count, 1)

    def tearDown(self):
        pass
示例#3
0
 def setUp(self):
     self.view_set = ApiViewSet()
示例#4
0
 def setUp(self):
     self.view_set = ApiViewSet()