Skip to content

Commit 4bacf50

Browse files
committed
add failing tests
1 parent f49852c commit 4bacf50

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

lib/stac-loader/runtime/tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ def check_item_exists(database_url, collection_id, item_id):
120120
return result[0][0] > 0
121121

122122

123+
def get_item(database_url, collection_id, item_id):
124+
"""
125+
Get the item with the given ID in the specified collection.
126+
127+
Args:
128+
database_url: Connection string for the database
129+
collection_id: The collection ID to check
130+
item_id: The item ID to check
131+
132+
Returns:
133+
dict: STAC item dict
134+
"""
135+
with PgstacDB(dsn=database_url) as db:
136+
# Direct SQL query to check if the item exists
137+
query = """
138+
SELECT id, collection, content
139+
FROM pgstac.items
140+
WHERE collection = %s AND id = %s
141+
"""
142+
143+
result = list(db.query(query, (collection_id, item_id)))[0]
144+
return {"id": result[0], "collection": result[1], "content": result[2]}
145+
146+
123147
def get_all_collection_items(database_url, collection_id):
124148
"""
125149
Get all items in a collection from the database.

lib/stac-loader/runtime/tests/test_stac_loader_handler.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
count_collections,
1313
get_all_collection_items,
1414
get_collection,
15+
get_item,
1516
)
1617
from pypgstac.db import PgstacDB
1718
from stac_loader.handler import get_pgstac_dsn, handler
@@ -202,6 +203,52 @@ def test_handler_with_multiple_items(mock_aws_context, mock_pgstac_dsn, database
202203
)
203204

204205

206+
def test_handler_with_identical_ids(mock_aws_context, mock_pgstac_dsn, database_url):
207+
"""Test handler with multiple valid STAC items"""
208+
collection_id = TEST_COLLECTION_IDS[0]
209+
210+
# Create multiple valid items with the same id
211+
item_id = "new-item"
212+
items = [
213+
create_valid_stac_item(item_id=item_id, collection_id=collection_id)
214+
for _ in range(2)
215+
]
216+
217+
items[0]["properties"]["priority"] = 2
218+
items[-1]["properties"]["priority"] = 1
219+
220+
# Create event with multiple records
221+
event = {
222+
"Records": [
223+
create_sqs_record(item, message_id=f"test-message-{i}")
224+
for i, item in enumerate(items)
225+
]
226+
}
227+
228+
# Get initial count of items in the collection
229+
initial_count = count_collection_items(database_url, collection_id)
230+
231+
# Call handler
232+
result = handler(event, mock_aws_context)
233+
234+
# All should succeed
235+
assert result is None
236+
237+
# Verify the item was added
238+
assert check_item_exists(database_url, collection_id, item_id), (
239+
f"Item {item_id} was not found in the database"
240+
)
241+
242+
# Verify the count increased by the expected amount
243+
new_count = count_collection_items(database_url, collection_id)
244+
assert new_count == initial_count + 1, (
245+
f"Expected {initial_count + 1} items, but found {new_count}"
246+
)
247+
248+
ingested_item = get_item(database_url, collection_id, item_id)
249+
assert ingested_item["content"]["properties"]["priority"] == 1
250+
251+
205252
def test_handler_with_mixed_items(mock_aws_context, mock_pgstac_dsn, database_url):
206253
"""Test handler with a mix of valid and invalid items"""
207254
collection_id = TEST_COLLECTION_IDS[0]
@@ -735,6 +782,52 @@ def test_handler_with_multiple_collections(
735782
)
736783

737784

785+
def test_handler_with_identical_collections(
786+
mock_aws_context, mock_pgstac_dsn, database_url
787+
):
788+
"""Test handler with multiple collections with the same ID"""
789+
collection_id = "new-collection"
790+
collections = [
791+
create_valid_stac_collection(collection_id=collection_id) for _ in range(2)
792+
]
793+
794+
collections[0]["priority"] = 2
795+
collections[-1]["priority"] = 1
796+
797+
# Create event with multiple collection records
798+
event = {
799+
"Records": [
800+
create_sqs_record(collection, message_id=f"test-collection-message-{i}")
801+
for i, collection in enumerate(collections)
802+
]
803+
}
804+
805+
# Get initial count of collections
806+
initial_count = count_collections(database_url)
807+
808+
# Call handler
809+
result = handler(event, mock_aws_context)
810+
811+
# All should succeed
812+
assert result is None
813+
814+
# Verify the collection was added
815+
assert check_collection_exists(database_url, collection_id), (
816+
f"Collection {collection_id} was not found in the database"
817+
)
818+
819+
# Verify the count increased by the expected amount
820+
new_count = count_collections(database_url)
821+
assert new_count == initial_count + 1, (
822+
f"Expected {initial_count + 1} collections, but found {new_count}"
823+
)
824+
825+
# Make sure the last entry for the collection was ingested
826+
ingested_collection = get_collection(database_url, collection_id)
827+
828+
assert ingested_collection["content"]["priority"] == 1
829+
830+
738831
def test_handler_with_invalid_collection(mock_aws_context, mock_pgstac_dsn):
739832
"""Test handler with an invalid STAC collection (missing required fields)"""
740833
# Create an invalid STAC collection (missing extent)

0 commit comments

Comments
 (0)