Skip to content

Commit 5d79ce2

Browse files
authored
feat(core): set count search parameter to its value in settings (#31)
This PR allows to run a query with a count request or not by setting `COUNT` environment variable. By default, its value is `False`.
1 parent 627ab00 commit 5d79ce2

6 files changed

Lines changed: 61 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Reach to [stac-fastapi documentation](https://stac-utils.github.io/stac-fastapi/
104104
| `DEBUG` | When set to `True`, set the EODAG logging level to `3`. Otherwise, set EODAG logging level to `2`. | False |
105105
| `KEEP_ORIGIN_URL` | Keep origin as alternate URL when data-download extension is enabled. | False |
106106
| `ORIGIN_URL_BLACKLIST` | Hide from clients items assets' origin URLs starting with URLs from the list. A string of comma separated values is expected. | "" |
107+
| `COUNT` | Whether to run a query with a count request or not. | False |
107108
| `FETCH_PROVIDERS` | Fetch additional collections from all EODAG providers. | False |
108109
| `DOWNLOAD_BASE_URL` | Useful to expose asset download URL in a separate domain name. | "" |
109110

stac_fastapi/eodag/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Settings(ApiSettings):
5252

5353
fetch_providers: bool = Field(default=False, description="Fetch additional collections from all providers.")
5454

55+
count: bool = Field(
56+
default=False,
57+
description=("Whether to run a query with a count request or not"),
58+
)
59+
5560
download_base_url: str = Field(
5661
default="",
5762
description="base url to be used for download link",

stac_fastapi/eodag/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def prepare_search_base_args(search_request: BaseSearchPostRequest, model: type[
520520
"page": search_request.page,
521521
"items_per_page": search_request.limit,
522522
"raise_errors": False,
523-
"count": True,
523+
"count": get_settings().count,
524524
}
525525
if search_request.ids is None
526526
else {}

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def mock_search_result():
266266
for p in search_result:
267267
p.downloader = Download("peps", config)
268268
p.downloader_auth = Authentication("peps", config)
269-
search_result.number_matched = len(search_result)
269+
search_result.number_matched = None
270270
return search_result
271271

272272

@@ -449,6 +449,7 @@ async def _assert_links_valid(element: Any):
449449
known_rel = [
450450
"self",
451451
"root",
452+
"next",
452453
"child",
453454
"items",
454455
"service-desc",

tests/test_order.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ async def test_order_product_wrong_downloader_ko(request_not_found, mock_search,
282282
assert "No downloader available" in caplog.messages[0]
283283

284284
# try to order a product which has a downloader but without order() method
285-
dl_config = config.PluginConfig.from_mapping({"priority": 1})
285+
dl_config = config.PluginConfig.from_mapping({"type": "HTTPDownload", "priority": 1})
286286
product.downloader = Download(federation_backend, dl_config)
287287
assert not hasattr(product.downloader, "order")
288288

tests/test_search.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,55 @@ async def test_request_params_valid(request_valid, defaults, input_bbox, expecte
4848
page=1,
4949
items_per_page=DEFAULT_ITEMS_PER_PAGE,
5050
raise_errors=False,
51-
count=True,
51+
count=False,
5252
**expected_kwargs,
5353
),
5454
)
5555

5656

57+
async def test_count_search(request_valid, defaults, mock_search, mock_search_result):
58+
"""
59+
Test the count setting during a search.
60+
"""
61+
count = get_settings().count
62+
qs = f"search?collections={defaults.product_type}"
63+
64+
assert count is False, "Default count setting should be False"
65+
response = await request_valid(
66+
qs,
67+
expected_search_kwargs=dict(
68+
productType=defaults.product_type,
69+
page=1,
70+
items_per_page=DEFAULT_ITEMS_PER_PAGE,
71+
raise_errors=False,
72+
count=False, # Ensure count is set to False
73+
),
74+
)
75+
assert response["numberMatched"] is None
76+
77+
# Reset search mock, set "number_matched" attribute of the search results mock for a counting search
78+
# and set count to True
79+
mock_search.reset_mock()
80+
search_result = mock_search_result
81+
search_result.number_matched = len(search_result)
82+
get_settings().count = True
83+
84+
response = await request_valid(
85+
qs,
86+
expected_search_kwargs=dict(
87+
productType=defaults.product_type,
88+
page=1,
89+
items_per_page=DEFAULT_ITEMS_PER_PAGE,
90+
raise_errors=False,
91+
count=True, # Ensure count is set to True
92+
),
93+
)
94+
assert response["numberMatched"] == 2
95+
96+
# Reset count setting to default
97+
get_settings().count = count
98+
99+
57100
async def test_items_response(request_valid, defaults):
58101
"""Returned items properties must be mapped as expected"""
59102
resp_json = await request_valid(
@@ -193,7 +236,7 @@ async def test_date_search(request_valid, defaults, input_start, input_end, expe
193236
items_per_page=DEFAULT_ITEMS_PER_PAGE,
194237
geom=defaults.bbox_wkt,
195238
raise_errors=False,
196-
count=True,
239+
count=False,
197240
**expected_kwargs,
198241
),
199242
)
@@ -213,7 +256,7 @@ async def test_date_search_from_items(request_valid, defaults, use_dates):
213256
items_per_page=DEFAULT_ITEMS_PER_PAGE,
214257
geom=defaults.bbox_wkt,
215258
raise_errors=False,
216-
count=True,
259+
count=False,
217260
**expected_kwargs,
218261
),
219262
)
@@ -240,7 +283,7 @@ async def test_sortby_items_parametrize(request_valid, defaults, sortby, expecte
240283
"page": 1,
241284
"items_per_page": 10,
242285
"raise_errors": False,
243-
"count": True,
286+
"count": False,
244287
},
245288
check_links=False,
246289
)
@@ -285,7 +328,7 @@ async def test_cloud_cover_post_search(request_valid, defaults):
285328
cloudCover=10,
286329
geom=defaults.bbox_wkt,
287330
raise_errors=False,
288-
count=True,
331+
count=False,
289332
),
290333
)
291334

@@ -305,7 +348,7 @@ async def test_intersects_post_search(request_valid, defaults):
305348
items_per_page=DEFAULT_ITEMS_PER_PAGE,
306349
geom=defaults.bbox_wkt,
307350
raise_errors=False,
308-
count=True,
351+
count=False,
309352
),
310353
)
311354

@@ -339,7 +382,7 @@ async def test_date_post_search(request_valid, defaults, input_start, input_end,
339382
page=1,
340383
items_per_page=DEFAULT_ITEMS_PER_PAGE,
341384
raise_errors=False,
342-
count=True,
385+
count=False,
343386
**expected_kwargs,
344387
),
345388
)
@@ -464,7 +507,7 @@ async def test_search_provider_in_downloadlink(request_valid, defaults, method,
464507
page=1,
465508
items_per_page=10,
466509
raise_errors=False,
467-
count=True,
510+
count=False,
468511
productType=defaults.product_type,
469512
**expected_kwargs,
470513
),

0 commit comments

Comments
 (0)