|
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 | import src.collection as collection
|
| 6 | +from fastapi import HTTPException |
6 | 7 | from pypgstac.load import Methods
|
7 | 8 | from src.utils import DbCreds
|
8 | 9 |
|
@@ -31,6 +32,56 @@ def test_load_collections(stac_collection, loader, pgstacdb):
|
31 | 32 | collection.ingest(stac_collection)
|
32 | 33 |
|
33 | 34 | loader.return_value.load_collections.assert_called_once_with(
|
34 |
| - file=[stac_collection.to_dict()], |
| 35 | + file=[stac_collection.model_dump(mode="json")], |
35 | 36 | insert_mode=Methods.upsert,
|
36 | 37 | )
|
| 38 | + |
| 39 | + |
| 40 | +def test_ingest_loader_error(stac_collection, pgstacdb): |
| 41 | + """Test handling of errors during the loading process.""" |
| 42 | + with patch( |
| 43 | + "src.collection.get_db_credentials", |
| 44 | + return_value=DbCreds( |
| 45 | + username="", password="", host="", port=1, dbname="", engine="" |
| 46 | + ), |
| 47 | + ): |
| 48 | + os.environ["DB_SECRET_ARN"] = "" |
| 49 | + |
| 50 | + with patch("src.collection.Loader") as mock_loader: |
| 51 | + mock_loader_instance = Mock() |
| 52 | + mock_loader.return_value = mock_loader_instance |
| 53 | + mock_loader_instance.load_collections.side_effect = Exception( |
| 54 | + "Invalid collection data" |
| 55 | + ) |
| 56 | + |
| 57 | + with pytest.raises(HTTPException) as excinfo: |
| 58 | + collection.ingest(stac_collection) |
| 59 | + |
| 60 | + assert excinfo.value.status_code == 500 |
| 61 | + assert "Invalid collection data" in str(excinfo.value.detail) |
| 62 | + |
| 63 | + |
| 64 | +def test_ingest_missing_environment_variable(stac_collection): |
| 65 | + """Test handling when the required environment variable is missing.""" |
| 66 | + if "DB_SECRET_ARN" in os.environ: |
| 67 | + del os.environ["DB_SECRET_ARN"] |
| 68 | + |
| 69 | + with pytest.raises(HTTPException) as excinfo: |
| 70 | + collection.ingest(stac_collection) |
| 71 | + |
| 72 | + assert "DB_SECRET_ARN" in str(excinfo.value) |
| 73 | + |
| 74 | + |
| 75 | +def test_ingest_credentials_error(stac_collection): |
| 76 | + """Test handling of errors during credential retrieval.""" |
| 77 | + with patch( |
| 78 | + "src.collection.get_db_credentials", |
| 79 | + side_effect=Exception("Failed to retrieve credentials"), |
| 80 | + ): |
| 81 | + os.environ["DB_SECRET_ARN"] = "" |
| 82 | + |
| 83 | + with pytest.raises(HTTPException) as excinfo: |
| 84 | + collection.ingest(stac_collection) |
| 85 | + |
| 86 | + assert excinfo.value.status_code == 500 |
| 87 | + assert "Failed to retrieve credentials" in str(excinfo.value.detail) |
0 commit comments