Skip to content

Commit a04d866

Browse files
test sort extension (#906)
* test sort extension * add pr number --------- Co-authored-by: vincentsarago <vincent.sarago@gmail.com>
1 parent be8600d commit a04d866

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
### Added
66

7-
- Added tests for the fields extension, which were previously missing ([#902](https://github.com/stac-utils/stac-fastapi/pull/902))
7+
- Add tests for the `sort` extension ([#906](https://github.com/stac-utils/stac-fastapi/pull/906))
8+
- Add tests for the `fields` extension ([#902](https://github.com/stac-utils/stac-fastapi/pull/902))
89
- Simply add a list of present stac-fastapi extensions to the Readme ([#898](https://github.com/stac-utils/stac-fastapi/pull/898))
910

1011
### Changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Tests for the Sort extension."""
2+
3+
import pytest
4+
from fastapi import FastAPI
5+
6+
# Adjust the import path based on your project structure
7+
from stac_fastapi.extensions.sort import (
8+
SortConformanceClasses,
9+
SortExtension,
10+
)
11+
from stac_fastapi.extensions.sort.request import (
12+
SortExtensionGetRequest,
13+
SortExtensionPostRequest,
14+
)
15+
16+
17+
def test_sort_conformance_classes():
18+
"""Test that the enum values match the official STAC API spec URIs."""
19+
assert (
20+
SortConformanceClasses.SEARCH
21+
== "https://api.stacspec.org/v1.0.0/item-search#sort"
22+
)
23+
assert (
24+
SortConformanceClasses.ITEMS
25+
== "https://api.stacspec.org/v1.0.0/ogcapi-features#sort"
26+
)
27+
assert (
28+
SortConformanceClasses.COLLECTIONS
29+
== "https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort"
30+
)
31+
32+
33+
def test_sort_extension_defaults():
34+
"""Test the default instantiation of the SortExtension."""
35+
ext = SortExtension()
36+
37+
assert ext.schema_href is None
38+
assert ext.conformance_classes == [SortConformanceClasses.SEARCH]
39+
40+
# Ensure the GET/POST request models are properly assigned
41+
assert ext.GET == SortExtensionGetRequest
42+
assert ext.POST == SortExtensionPostRequest
43+
44+
45+
def test_sort_extension_customization():
46+
"""Test instantiating SortExtension with custom arguments."""
47+
custom_conformance = [
48+
SortConformanceClasses.SEARCH,
49+
SortConformanceClasses.ITEMS,
50+
SortConformanceClasses.COLLECTIONS,
51+
]
52+
custom_schema = "https://example.com/sort-schema.json"
53+
54+
ext = SortExtension(
55+
conformance_classes=custom_conformance,
56+
schema_href=custom_schema,
57+
)
58+
59+
assert ext.conformance_classes == custom_conformance
60+
assert ext.schema_href == custom_schema
61+
62+
63+
def test_sort_extension_register():
64+
"""Test the register method with a dummy FastAPI app."""
65+
ext = SortExtension()
66+
app = FastAPI()
67+
68+
try:
69+
ext.register(app)
70+
except Exception as e:
71+
pytest.fail(f"SortExtension.register() raised an exception: {e}")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
from stac_pydantic.api.extensions.sort import SortExtension as PostSortModel
4+
5+
from stac_fastapi.extensions.sort.request import (
6+
SortExtensionGetRequest,
7+
SortExtensionPostRequest,
8+
)
9+
10+
## GET Request Tests
11+
12+
13+
def test_sort_extension_get_request_parsing():
14+
"""Test that the GET request properly converts comma-separated strings to a list."""
15+
# Single field
16+
req = SortExtensionGetRequest(sortby="-gsd")
17+
assert req.sortby == ["-gsd"]
18+
19+
# Multiple fields
20+
req = SortExtensionGetRequest(sortby="-gsd,+datetime,id")
21+
assert req.sortby == ["-gsd", "+datetime", "id"]
22+
23+
24+
def test_sort_extension_get_request_empty():
25+
"""Test GET request with null or empty values."""
26+
req_none = SortExtensionGetRequest(sortby=None)
27+
assert req_none.sortby is None
28+
29+
30+
## POST Request Tests
31+
32+
33+
def test_sort_extension_post_request_defaults():
34+
"""Test POST request model default values."""
35+
req = SortExtensionPostRequest()
36+
assert req.sortby is None
37+
38+
39+
def test_sort_extension_post_request_valid_data():
40+
"""Test POST request with valid structured sort objects."""
41+
data = {
42+
"sortby": [
43+
{"field": "properties.created", "direction": "asc"},
44+
{"field": "id", "direction": "desc"},
45+
]
46+
}
47+
req = SortExtensionPostRequest(**data)
48+
49+
assert len(req.sortby) == 2
50+
assert isinstance(req.sortby[0], PostSortModel)
51+
assert req.sortby[0].field == "properties.created"
52+
assert req.sortby[0].direction == "asc"
53+
54+
55+
def test_sort_extension_post_request_invalid_direction():
56+
"""
57+
Test that pydantic validation catches invalid directions.
58+
The PostSortModel restricts direction to 'asc' or 'desc'.
59+
"""
60+
invalid_data = {"sortby": [{"field": "properties.created", "direction": "sideways"}]}
61+
with pytest.raises(ValidationError):
62+
SortExtensionPostRequest(**invalid_data)

0 commit comments

Comments
 (0)