|
12 | 12 | count_collections, |
13 | 13 | get_all_collection_items, |
14 | 14 | get_collection, |
| 15 | + get_item, |
15 | 16 | ) |
16 | 17 | from pypgstac.db import PgstacDB |
17 | 18 | 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 |
202 | 203 | ) |
203 | 204 |
|
204 | 205 |
|
| 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 | + |
205 | 252 | def test_handler_with_mixed_items(mock_aws_context, mock_pgstac_dsn, database_url): |
206 | 253 | """Test handler with a mix of valid and invalid items""" |
207 | 254 | collection_id = TEST_COLLECTION_IDS[0] |
@@ -735,6 +782,52 @@ def test_handler_with_multiple_collections( |
735 | 782 | ) |
736 | 783 |
|
737 | 784 |
|
| 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 | + |
738 | 831 | def test_handler_with_invalid_collection(mock_aws_context, mock_pgstac_dsn): |
739 | 832 | """Test handler with an invalid STAC collection (missing required fields)""" |
740 | 833 | # Create an invalid STAC collection (missing extent) |
|
0 commit comments