Skip to content

Commit 2e14974

Browse files
committed
add tests
1 parent e516bd5 commit 2e14974

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

stac_fastapi/tests/api/test_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
"POST /collections/{collection_id}/aggregate",
5151
"GET /collections-search",
5252
"POST /collections-search",
53+
"GET /catalogs",
54+
"POST /catalogs",
55+
"GET /catalogs/{catalog_id}",
56+
"GET /catalogs/{catalog_id}/collections",
5357
}
5458

5559

stac_fastapi/tests/conftest.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from stac_fastapi.types.config import Settings
4040

4141
os.environ.setdefault("ENABLE_COLLECTIONS_SEARCH_ROUTE", "true")
42+
os.environ.setdefault("ENABLE_CATALOG_ROUTE", "false")
4243

4344
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch":
4445
from stac_fastapi.opensearch.app import app_config
@@ -396,3 +397,63 @@ def build_test_app():
396397
# Create and return the app
397398
api = StacApi(**test_config)
398399
return api.app
400+
401+
402+
def build_test_app_with_catalogs():
403+
"""Build a test app with catalogs extension enabled."""
404+
from stac_fastapi.core.extensions.catalogs import CatalogsExtension
405+
406+
# Get the base config
407+
test_config = app_config.copy()
408+
409+
# Get database and settings (already imported above)
410+
test_database = DatabaseLogic()
411+
test_settings = AsyncSettings()
412+
413+
# Add catalogs extension
414+
catalogs_extension = CatalogsExtension(
415+
client=CoreClient(
416+
database=test_database,
417+
session=None,
418+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
419+
),
420+
settings=test_settings,
421+
conformance_classes=[
422+
"https://api.stacspec.org/v1.0.0-beta.1/catalogs",
423+
],
424+
)
425+
426+
# Add to extensions if not already present
427+
if not any(isinstance(ext, CatalogsExtension) for ext in test_config["extensions"]):
428+
test_config["extensions"].append(catalogs_extension)
429+
430+
# Update client with new extensions
431+
test_config["client"] = CoreClient(
432+
database=test_database,
433+
session=None,
434+
extensions=test_config["extensions"],
435+
post_request_model=test_config["search_post_request_model"],
436+
landing_page_id=os.getenv("STAC_FASTAPI_LANDING_PAGE_ID", "stac-fastapi"),
437+
)
438+
439+
# Create and return the app
440+
api = StacApi(**test_config)
441+
return api.app
442+
443+
444+
@pytest_asyncio.fixture(scope="session")
445+
async def catalogs_app():
446+
"""Fixture to get the FastAPI app with catalogs extension enabled."""
447+
return build_test_app_with_catalogs()
448+
449+
450+
@pytest_asyncio.fixture(scope="session")
451+
async def catalogs_app_client(catalogs_app):
452+
"""Fixture to get an async client for the app with catalogs extension enabled."""
453+
await create_index_templates()
454+
await create_collection_index()
455+
456+
async with AsyncClient(
457+
transport=ASGITransport(app=catalogs_app), base_url="http://test-server"
458+
) as c:
459+
yield c
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"id": "test-catalog",
3+
"type": "Catalog",
4+
"stac_version": "1.0.0",
5+
"title": "Test Catalog",
6+
"description": "A test catalog for STAC API testing",
7+
"links": [
8+
{
9+
"rel": "self",
10+
"href": "http://test-server/catalogs/test-catalog",
11+
"type": "application/json"
12+
},
13+
{
14+
"rel": "root",
15+
"href": "http://test-server/catalogs",
16+
"type": "application/json"
17+
},
18+
{
19+
"rel": "child",
20+
"href": "http://test-server/collections/test-collection-1",
21+
"type": "application/json",
22+
"title": "Test Collection 1"
23+
},
24+
{
25+
"rel": "child",
26+
"href": "http://test-server/collections/test-collection-2",
27+
"type": "application/json",
28+
"title": "Test Collection 2"
29+
},
30+
{
31+
"rel": "child",
32+
"href": "http://test-server/collections/test-collection-3",
33+
"type": "application/json",
34+
"title": "Test Collection 3"
35+
}
36+
]
37+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import uuid
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_get_root_catalog(catalogs_app_client, load_test_data):
8+
"""Test getting the root catalog."""
9+
resp = await catalogs_app_client.get("/catalogs")
10+
assert resp.status_code == 200
11+
12+
catalog = resp.json()
13+
assert catalog["type"] == "Catalog"
14+
assert catalog["id"] == "root"
15+
assert catalog["stac_version"] == "1.0.0"
16+
assert "links" in catalog
17+
18+
# Check for required links
19+
links = catalog["links"]
20+
link_rels = [link["rel"] for link in links]
21+
assert "self" in link_rels
22+
assert "root" in link_rels
23+
assert "parent" in link_rels
24+
25+
26+
@pytest.mark.asyncio
27+
async def test_create_catalog(catalogs_app_client, load_test_data):
28+
"""Test creating a new catalog."""
29+
test_catalog = load_test_data("test_catalog.json")
30+
test_catalog["id"] = f"test-catalog-{uuid.uuid4()}"
31+
32+
resp = await catalogs_app_client.post("/catalogs", json=test_catalog)
33+
assert resp.status_code == 201
34+
35+
created_catalog = resp.json()
36+
assert created_catalog["id"] == test_catalog["id"]
37+
assert created_catalog["type"] == "Catalog"
38+
assert created_catalog["title"] == test_catalog["title"]
39+
40+
41+
@pytest.mark.asyncio
42+
async def test_get_catalog(catalogs_app_client, load_test_data):
43+
"""Test getting a specific catalog."""
44+
# First create a catalog
45+
test_catalog = load_test_data("test_catalog.json")
46+
test_catalog["id"] = f"test-catalog-{uuid.uuid4()}"
47+
48+
create_resp = await catalogs_app_client.post("/catalogs", json=test_catalog)
49+
assert create_resp.status_code == 201
50+
51+
# Now get it back
52+
resp = await catalogs_app_client.get(f"/catalogs/{test_catalog['id']}")
53+
assert resp.status_code == 200
54+
55+
catalog = resp.json()
56+
assert catalog["id"] == test_catalog["id"]
57+
assert catalog["title"] == test_catalog["title"]
58+
assert catalog["description"] == test_catalog["description"]
59+
60+
61+
@pytest.mark.asyncio
62+
async def test_get_nonexistent_catalog(catalogs_app_client):
63+
"""Test getting a catalog that doesn't exist."""
64+
resp = await catalogs_app_client.get("/catalogs/nonexistent-catalog")
65+
assert resp.status_code == 404
66+
67+
68+
@pytest.mark.asyncio
69+
async def test_get_catalog_collections(catalogs_app_client, load_test_data, ctx):
70+
"""Test getting collections linked from a catalog."""
71+
# First create a catalog with a link to the test collection
72+
test_catalog = load_test_data("test_catalog.json")
73+
test_catalog["id"] = f"test-catalog-{uuid.uuid4()}"
74+
75+
# Update the catalog links to point to the actual test collection
76+
for link in test_catalog["links"]:
77+
if link["rel"] == "child":
78+
link["href"] = f"http://test-server/collections/{ctx.collection['id']}"
79+
80+
create_resp = await catalogs_app_client.post("/catalogs", json=test_catalog)
81+
assert create_resp.status_code == 201
82+
83+
# Now get collections from the catalog
84+
resp = await catalogs_app_client.get(f"/catalogs/{test_catalog['id']}/collections")
85+
assert resp.status_code == 200
86+
87+
collections_response = resp.json()
88+
assert "collections" in collections_response
89+
assert "links" in collections_response
90+
91+
# Should contain the test collection
92+
collection_ids = [col["id"] for col in collections_response["collections"]]
93+
assert ctx.collection["id"] in collection_ids
94+
95+
96+
@pytest.mark.asyncio
97+
async def test_get_catalog_collections_nonexistent_catalog(catalogs_app_client):
98+
"""Test getting collections from a catalog that doesn't exist."""
99+
resp = await catalogs_app_client.get("/catalogs/nonexistent-catalog/collections")
100+
assert resp.status_code == 404
101+
102+
103+
@pytest.mark.asyncio
104+
async def test_root_catalog_with_multiple_catalogs(catalogs_app_client, load_test_data):
105+
"""Test that root catalog includes links to multiple catalogs."""
106+
# Create multiple catalogs
107+
catalog_ids = []
108+
for i in range(3):
109+
test_catalog = load_test_data("test_catalog.json")
110+
test_catalog["id"] = f"test-catalog-{uuid.uuid4()}-{i}"
111+
test_catalog["title"] = f"Test Catalog {i}"
112+
113+
resp = await catalogs_app_client.post("/catalogs", json=test_catalog)
114+
assert resp.status_code == 201
115+
catalog_ids.append(test_catalog["id"])
116+
117+
# Get root catalog
118+
resp = await catalogs_app_client.get("/catalogs")
119+
assert resp.status_code == 200
120+
121+
catalog = resp.json()
122+
child_links = [link for link in catalog["links"] if link["rel"] == "child"]
123+
124+
# Should have child links for all created catalogs
125+
child_hrefs = [link["href"] for link in child_links]
126+
for catalog_id in catalog_ids:
127+
assert any(catalog_id in href for href in child_hrefs)

0 commit comments

Comments
 (0)