Skip to content

Commit

Permalink
Add ordering schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acuriel committed Sep 2, 2024
1 parent 726cc46 commit e3f3ed8
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/test_ordering_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import pytest
from django.db.models import QuerySet

from ninja import OrderingSchema


class FakeQS(QuerySet):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.is_ordered = False

def order_by(self, *args, **kwargs):
self.is_ordered = True
self.order_by_args = args
self.order_by_kwargs = kwargs
return self


def test_validate_order_by_field__should_pass_when_all_field_allowed():
test_field = "test_field"

class DummyOrderingSchema(OrderingSchema):
pass

order_by_value = [test_field]
validation_result = DummyOrderingSchema.validate_order_by_field(order_by_value)
assert validation_result == order_by_value


def test_validate_order_by_field__should_pass_when_value_in_allowed_fields_and_asc():
test_field = "test_field"

class DummyOrderingSchema(OrderingSchema):
class Config(OrderingSchema.Config):
allowed_fields = [test_field]

order_by_value = [test_field]
validation_result = DummyOrderingSchema.validate_order_by_field(order_by_value)
assert validation_result == order_by_value


def test_validate_order_by_field__should_pass_when_value_in_allowed_fields_and_desc():
test_field = "test_field"

class DummyOrderingSchema(OrderingSchema):
class Config(OrderingSchema.Config):
allowed_fields = [test_field]

order_by_value = [f"-{test_field}"]
validation_result = DummyOrderingSchema.validate_order_by_field(order_by_value)
assert validation_result == order_by_value


def test_validate_order_by_field__should_raise_validation_error_when_value_asc_not_in_allowed_fields():
test_field = "allowed_field"

class DummyOrderingSchema(OrderingSchema):
class Config(OrderingSchema.Config):
allowed_fields = [test_field]

order_by_value = ["not_allowed_field"]
with pytest.raises(ValueError):
DummyOrderingSchema.validate_order_by_field(order_by_value)


def test_validate_order_by_field__should_raise_validation_error_when_value_desc_not_in_allowed_fields():
test_field = "allowed_field"

class DummyOrderingSchema(OrderingSchema):
class Config(OrderingSchema.Config):
allowed_fields = [test_field]

order_by_value = ["-not_allowed_field"]
with pytest.raises(ValueError):
DummyOrderingSchema.validate_order_by_field(order_by_value)


def test_sort__should_call_order_by_on_queryset_with_expected_args():
order_by_value = ["test_field_1", "-test_field_2"]
ordering_schema = OrderingSchema(order_by=order_by_value)

queryset = FakeQS()
queryset = ordering_schema.sort(queryset)
assert queryset.is_ordered
assert queryset.order_by_args == tuple(order_by_value)

0 comments on commit e3f3ed8

Please sign in to comment.