test_request_query.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from typing import List
  2. from typing import Optional
  3. import pytest
  4. from pydantic import ValidationError
  5. from clean_python import Filter
  6. from clean_python import PageOptions
  7. from clean_python.fastapi import RequestQuery
  8. class SomeQuery(RequestQuery):
  9. foo: Optional[int] = None
  10. class SomeListQuery(RequestQuery):
  11. foo: Optional[List[int]]
  12. @pytest.mark.parametrize(
  13. "query,expected",
  14. [
  15. (
  16. RequestQuery(),
  17. PageOptions(limit=50, offset=0, order_by="id", ascending=True),
  18. ),
  19. (
  20. RequestQuery(limit=10, offset=20, order_by="-id"),
  21. PageOptions(limit=10, offset=20, order_by="id", ascending=False),
  22. ),
  23. ],
  24. )
  25. def test_as_page_options(query, expected):
  26. assert query.as_page_options() == expected
  27. @pytest.mark.parametrize(
  28. "query,expected",
  29. [
  30. (SomeQuery(), []),
  31. (SomeQuery(foo=None), []),
  32. (SomeQuery(foo=3), [Filter(field="foo", values=[3])]),
  33. (SomeListQuery(foo=[3, 4]), [Filter(field="foo", values=[3, 4])]),
  34. ],
  35. )
  36. def test_filters(query, expected):
  37. assert query.filters() == expected
  38. def test_validate_order_by():
  39. with pytest.raises(ValidationError):
  40. RequestQuery(limit=10, offset=0, order_by="foo")