Skip to content

Commit e0e7707

Browse files
committed
refactor: simplify tests via parametrize, specifiy test origin
1 parent c7376a6 commit e0e7707

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

tests/api/test_links_with_root_path.py

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db
77

8-
root_path_value = "/stac/v1"
9-
10-
# Append the root path to the base URL, this is key to reproducing the issue where the root path appears twice in some links
11-
base_url = f"http://api.acme.com{root_path_value}"
8+
BASE_URL = "http://api.acme.com"
9+
ROOT_PATH = "/stac/v1"
1210

1311

1412
@pytest.fixture(scope="function")
@@ -18,7 +16,7 @@ async def app_with_root_path(database, monkeypatch):
1816
specific ROOT_PATH environment variable and connected to the test database.
1917
"""
2018

21-
monkeypatch.setenv("ROOT_PATH", root_path_value)
19+
monkeypatch.setenv("ROOT_PATH", ROOT_PATH)
2220
monkeypatch.setenv("PGUSER", database.user)
2321
monkeypatch.setenv("PGPASSWORD", database.password)
2422
monkeypatch.setenv("PGHOST", database.host)
@@ -35,8 +33,8 @@ async def app_with_root_path(database, monkeypatch):
3533

3634
# Ensure the app's root_path is configured as expected
3735
assert (
38-
app.root_path == root_path_value
39-
), f"app_with_root_path fixture: app.root_path is '{app.root_path}', expected '{root_path_value}'"
36+
app.root_path == ROOT_PATH
37+
), f"app_with_root_path fixture: app.root_path is '{app.root_path}', expected '{ROOT_PATH}'"
4038

4139
await connect_to_db(app, add_write_connection_pool=with_transactions)
4240
yield app
@@ -47,7 +45,8 @@ async def app_with_root_path(database, monkeypatch):
4745
def client_with_root_path(app_with_root_path):
4846
with TestClient(
4947
app_with_root_path,
50-
root_path=root_path_value,
48+
base_url=BASE_URL,
49+
root_path=ROOT_PATH,
5150
) as c:
5251
yield c
5352

@@ -75,66 +74,48 @@ def loaded_client(client_with_root_path, load_test_data):
7574
yield client_with_root_path
7675

7776

78-
def test_search_links_are_valid(loaded_client):
79-
resp = loaded_client.get("/search?limit=1")
80-
assert resp.status_code == 200
81-
response_json = resp.json()
82-
assert_links_href(response_json.get("links", []), base_url)
83-
84-
85-
def test_collection_links_are_valid(loaded_client):
86-
resp = loaded_client.get("/collections?limit=1")
87-
assert resp.status_code == 200
88-
response_json = resp.json()
89-
assert_links_href(response_json.get("links", []), base_url)
90-
91-
92-
def test_items_collection_links_are_valid(loaded_client):
93-
resp = loaded_client.get("/collections/test-collection/items?limit=1")
77+
@pytest.mark.parametrize(
78+
"path",
79+
[
80+
"/search?limit=1",
81+
"/collections?limit=1",
82+
"/collections/test-collection/items?limit=1",
83+
],
84+
)
85+
def test_search_links_are_valid(loaded_client, path):
86+
resp = loaded_client.get(path)
9487
assert resp.status_code == 200
9588
response_json = resp.json()
96-
assert_links_href(response_json.get("links", []), base_url)
97-
98-
99-
def assert_links_href(links, url_prefix):
100-
"""
101-
Ensure all links start with the expected URL prefix and check that
102-
there is no root_path duplicated in the URL.
103-
104-
Args:
105-
links: List of link dictionaries with 'href' keys
106-
url_prefix: Expected URL prefix (e.g., 'http://test/stac/v1')
107-
"""
108-
from urllib.parse import urlparse
10989

90+
# Ensure all links start with the expected URL prefix and check that
91+
# there is no root_path duplicated in the URL.
11092
failed_links = []
111-
parsed_prefix = urlparse(url_prefix)
112-
root_path = parsed_prefix.path # e.g., '/stac/v1'
93+
expected_prefix = f"{BASE_URL}{ROOT_PATH}"
11394

114-
for link in links:
95+
for link in response_json.get("links", []):
11596
href = link["href"]
11697
rel = link.get("rel", "unknown")
11798

11899
# Check if link starts with the expected prefix
119-
if not href.startswith(url_prefix):
100+
if not href.startswith(expected_prefix):
120101
failed_links.append(
121102
{
122103
"rel": rel,
123104
"href": href,
124-
"error": f"does not start with expected prefix '{url_prefix}'",
105+
"error": f"does not start with expected prefix '{expected_prefix}'",
125106
}
126107
)
127108
continue
128109

129110
# Check for duplicated root path
130-
if root_path and root_path != "/":
131-
remainder = href[len(url_prefix) :]
132-
if remainder.startswith(root_path):
111+
if ROOT_PATH and ROOT_PATH != "/":
112+
remainder = href[len(expected_prefix) :]
113+
if remainder.startswith(ROOT_PATH):
133114
failed_links.append(
134115
{
135116
"rel": rel,
136117
"href": href,
137-
"error": f"contains duplicated root path '{root_path}'",
118+
"error": f"contains duplicated root path '{ROOT_PATH}'",
138119
}
139120
)
140121

0 commit comments

Comments
 (0)