Skip to content

Commit b522e83

Browse files
author
Grzegorz Pustulka
committed
fixed tests - part 1
1 parent 506f170 commit b522e83

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ async def execute_search(
546546
search_after = json.loads(urlsafe_b64decode(token).decode())
547547

548548
query = search.query.to_dict() if search.query else None
549+
549550
index_param = await self.async_index_selector.select_indexes(
550551
collection_ids, datetime_search
551552
)

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/index.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ def filter_indexes_by_datetime(
8383

8484
def parse_datetime(dt_str: str) -> datetime:
8585
"""Parse datetime string, handling both with and without 'Z' suffix."""
86-
if dt_str.endswith("Z"):
87-
dt_str = dt_str[:-1]
88-
return datetime.fromisoformat(dt_str.replace("Z", ""))
86+
return datetime.fromisoformat(dt_str.rstrip('Z'))
8987

9088
def extract_date_range_from_index(index_name: str) -> tuple:
9189
"""Extract start and end dates from index name."""
@@ -101,23 +99,18 @@ def extract_date_range_from_index(index_name: str) -> tuple:
10199
end_date = datetime.strptime(dates[1], "%Y-%m-%d")
102100
return start_date, end_date
103101

104-
gte_dt = parse_datetime(gte)
105-
lte_dt = parse_datetime(lte)
102+
def is_index_in_range(start_date: datetime, end_date: datetime, gte_dt: datetime, lte_dt: datetime) -> bool:
103+
"""Check if index date range overlaps with filter range."""
104+
return not (end_date.date() < gte_dt.date() or start_date.date() > lte_dt.date())
105+
106+
gte_dt = parse_datetime(gte) if gte else datetime.min.replace(microsecond=0)
107+
lte_dt = parse_datetime(lte) if lte else datetime.max.replace(microsecond=0)
106108

107109
filtered_indexes = []
108110

109111
for index in indexes:
110112
start_date, end_date = extract_date_range_from_index(index)
111-
112-
include_index = True
113-
114-
if gte_dt and end_date.date() < gte_dt.date():
115-
include_index = False
116-
117-
if lte_dt and start_date.date() > lte_dt.date():
118-
include_index = False
119-
120-
if include_index:
113+
if is_index_in_range(start_date, end_date, gte_dt, lte_dt):
121114
filtered_indexes.append(index)
122115

123116
return filtered_indexes

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/index_insertion_strategies.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from abc import ABC, abstractmethod
55
from datetime import timedelta
66
from enum import Enum
7+
from fastapi import HTTPException, status
78
from typing import Any, Dict, List, Union
89

910
from stac_fastapi.core.utilities import get_bool_env
@@ -241,6 +242,13 @@ async def _get_target_index_base(
241242
check_size: bool = True,
242243
) -> str:
243244
product_datetime = product["properties"]["datetime"]
245+
246+
if not product_datetime:
247+
raise HTTPException(
248+
status_code=400,
249+
detail="Product datetime is required for indexing"
250+
)
251+
244252
datetime_range = {"gte": product_datetime, "lte": product_datetime}
245253
target_index = await index_selector.select_indexes(
246254
[collection_id], datetime_range
@@ -381,6 +389,13 @@ def _get_target_index_base(
381389
check_size: bool = True,
382390
) -> str:
383391
product_datetime = product["properties"]["datetime"]
392+
393+
if not product_datetime:
394+
raise HTTPException(
395+
status_code=400,
396+
detail="Product datetime is required for indexing"
397+
)
398+
384399
datetime_range = {"gte": product_datetime, "lte": product_datetime}
385400
target_index = index_selector.select_indexes([collection_id], datetime_range)
386401
all_indexes = index_selector.get_collection_indexes(collection_id)

stac_fastapi/tests/api/test_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ async def test_search_point_does_not_intersect(app_client, ctx):
427427

428428
@pytest.mark.asyncio
429429
async def test_datetime_response_format(app_client, txn_client, ctx):
430+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
431+
pytest.skip()
432+
430433
first_item = dict(ctx.item)
431434

432435
second_item = deepcopy(first_item)
@@ -464,6 +467,9 @@ async def test_datetime_response_format(app_client, txn_client, ctx):
464467

465468
@pytest.mark.asyncio
466469
async def test_datetime_non_interval(app_client, txn_client, ctx):
470+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
471+
pytest.skip()
472+
467473
first_item = dict(ctx.item)
468474

469475
second_item = deepcopy(first_item)
@@ -500,6 +506,9 @@ async def test_datetime_non_interval(app_client, txn_client, ctx):
500506

501507
@pytest.mark.asyncio
502508
async def test_datetime_interval(app_client, txn_client, ctx):
509+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
510+
pytest.skip()
511+
503512
first_item = dict(ctx.item)
504513

505514
second_item = deepcopy(first_item)
@@ -536,6 +545,9 @@ async def test_datetime_interval(app_client, txn_client, ctx):
536545

537546
@pytest.mark.asyncio
538547
async def test_datetime_bad_non_interval(app_client, txn_client, ctx):
548+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
549+
pytest.skip()
550+
539551
first_item = dict(ctx.item)
540552

541553
second_item = deepcopy(first_item)
@@ -572,6 +584,9 @@ async def test_datetime_bad_non_interval(app_client, txn_client, ctx):
572584

573585
@pytest.mark.asyncio
574586
async def test_datetime_bad_interval(app_client, txn_client, ctx):
587+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
588+
pytest.skip()
589+
575590
first_item = dict(ctx.item)
576591

577592
second_item = deepcopy(first_item)
@@ -777,6 +792,18 @@ async def test_create_new_index_when_size_limit_exceeded(app_client, load_test_d
777792
assert expected_index in indices.keys()
778793

779794

795+
@pytest.mark.asyncio
796+
async def test_bulk_create_item_fails_without_datetime(app_client, load_test_data, txn_client, ctx):
797+
if not os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
798+
pytest.skip()
799+
800+
item = deepcopy(load_test_data)
801+
item["id"] = "second-item"
802+
item["properties"]["datetime"] = None
803+
response = await app_client.post(f"/collections/{item['collection']}/items", json=item)
804+
assert response.status_code == 400
805+
806+
780807
@pytest.mark.asyncio
781808
async def test_bulk_create_items_with_same_date_range(app_client, load_test_data, txn_client, ctx):
782809
if not os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):

stac_fastapi/tests/database/test_database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import uuid
23

34
import pytest
@@ -27,6 +28,9 @@ async def test_index_mapping_collections(ctx):
2728

2829
@pytest.mark.asyncio
2930
async def test_index_mapping_items(txn_client, load_test_data):
31+
if os.getenv("ENABLE_DATETIME_INDEX_FILTERING"):
32+
pytest.skip()
33+
3034
collection = load_test_data("test_collection.json")
3135
collection["id"] = str(uuid.uuid4())
3236
await txn_client.create_collection(

0 commit comments

Comments
 (0)