Exemplo n.º 1
0
    def test_get_filter_class_supplied_model_mismatch(self):
        class View(object):
            filter_class = PlaceFilterSet

        with pytest.raises(AssertionError):
            DjangoFilterBackend().get_filter_class(View(),
                                                   Restaurant.objects.all())
Exemplo n.º 2
0
    def test_get_filter_class_supplied(self):
        class View(object):
            filter_class = PlaceFilterSet

        filter_class = DjangoFilterBackend().get_filter_class(
            View(), Place.objects.all())

        assert filter_class is PlaceFilterSet
Exemplo n.º 3
0
    def test_get_filter_context(self):
        context = DjangoFilterBackend().get_filter_context(
            request='request', view='view',
        )

        assert context == {
            'request': 'request',
            'view': 'view',
        }
Exemplo n.º 4
0
    def test_get_filter_class_by_filter_fields(self):
        class View(object):
            filter_fields = ["name"]

        filter_class = DjangoFilterBackend().get_filter_class(
            View(), Place.objects.all())

        assert issubclass(filter_class, ModelFilterSet)
        assert filter_class.Meta.model is Place
        assert filter_class.Meta.fields == ["name"]
Exemplo n.º 5
0
    def test_get_filter_queryset(self, mock_filter, db, rf):
        class View(object):
            filter_fields = ["name"]

        request = rf.get("/")
        request.query_params = QueryDict()

        filtered = DjangoFilterBackend().filter_queryset(
            request=request, queryset=Place.objects.all(), view=View())

        assert filtered == mock_filter.return_value
Exemplo n.º 6
0
    def test_filter_queryset_supplied_model_mismatch(self, mock_filter, db, rf):
        class View(object):
            filter_class = PlaceFilterSet
            filter_fields = ['name']

        request = rf.get('/')
        request.query_params = QueryDict()

        with pytest.raises(AssertionError):
            DjangoFilterBackend().filter_queryset(
                request=request,
                queryset=Restaurant.objects.all(),
                view=View()
            )
Exemplo n.º 7
0
    def test_get_filter_queryset_invalid_query(self, mock_filter, db, rf):
        mock_filter.side_effect = DjangoValidationError({"foo": "bar"})

        class View(object):
            filter_fields = ["name"]

        request = rf.get("/")
        request.query_params = QueryDict()

        with pytest.raises(ValidationError) as e:
            DjangoFilterBackend().filter_queryset(request=request,
                                                  queryset=Place.objects.all(),
                                                  view=View())

        assert e.value.detail == {"foo": ["bar"]}
Exemplo n.º 8
0
    def test_get_filter_class_all_fields(self):
        class View(object):
            filter_fields = "__all__"

        filter_class = DjangoFilterBackend().get_filter_class(
            View(), Place.objects.all())

        assert issubclass(filter_class, ModelFilterSet)
        assert filter_class.Meta.model is Place
        assert set(filter_class().filters.keys()) == {
            "restaurant",
            "id",
            "name",
            "address",
        }
Exemplo n.º 9
0
 def test_get_filter_queryset_not_filtered(self):
     assert DjangoFilterBackend().filter_queryset(None, None, None) is None
Exemplo n.º 10
0
    def test_get_filter_context(self):
        context = DjangoFilterBackend().get_filter_context(request="request",
                                                           view="view")

        assert context == {"request": "request", "view": "view"}