|
| 1 | +""" |
| 2 | +Test suite for STAC (SpatioTemporal Asset Catalog) Transactions API endpoints. |
| 3 | +
|
| 4 | +This module contains tests for the collection and item endpoints of the STAC API. |
| 5 | +It verifies the behavior of the API when posting valid and invalid STAC collections and items, |
| 6 | +as well as bulk items. |
| 7 | +
|
| 8 | +Endpoints tested: |
| 9 | +- /collections |
| 10 | +- /collections/{}/items |
| 11 | +- /collections/{}/bulk_items |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +collections_endpoint = "/collections" |
| 16 | +items_endpoint = "/collections/{}/items" |
| 17 | +bulk_endpoint = "/collections/{}/bulk_items" |
| 18 | + |
| 19 | + |
| 20 | +class TestList: |
| 21 | + """ |
| 22 | + Test cases for STAC API's collection and item endpoints. |
| 23 | +
|
| 24 | + This class contains tests to ensure that the STAC API correctly handles |
| 25 | + posting valid and invalid STAC collections and items, both individually |
| 26 | + and in bulk. It uses pytest fixtures to set up the test environment with |
| 27 | + necessary data. |
| 28 | + """ |
| 29 | + |
| 30 | + async def test_post_invalid_collection(self, api_client, invalid_stac_collection): |
| 31 | + """ |
| 32 | + Test the API's response to posting an invalid STAC collection. |
| 33 | +
|
| 34 | + Asserts that the response status code is 422 and the detail |
| 35 | + is "Validation Error". |
| 36 | + """ |
| 37 | + response = await api_client.post( |
| 38 | + collections_endpoint, json=invalid_stac_collection |
| 39 | + ) |
| 40 | + assert response.json()["detail"] == "Validation Error" |
| 41 | + assert response.status_code == 422 |
| 42 | + |
| 43 | + async def test_post_valid_collection(self, api_client, valid_stac_collection): |
| 44 | + """ |
| 45 | + Test the API's response to posting a valid STAC collection. |
| 46 | +
|
| 47 | + Asserts that the response status code is 200. |
| 48 | + """ |
| 49 | + response = await api_client.post( |
| 50 | + collections_endpoint, json=valid_stac_collection |
| 51 | + ) |
| 52 | + assert response.status_code == 201 |
| 53 | + |
| 54 | + async def test_post_invalid_item(self, api_client, invalid_stac_item): |
| 55 | + """ |
| 56 | + Test the API's response to posting an invalid STAC item. |
| 57 | +
|
| 58 | + Asserts that the response status code is 422 and the detail |
| 59 | + is "Validation Error". |
| 60 | + """ |
| 61 | + collection_id = invalid_stac_item["collection"] |
| 62 | + response = await api_client.post( |
| 63 | + items_endpoint.format(collection_id), json=invalid_stac_item |
| 64 | + ) |
| 65 | + assert response.json()["detail"] == "Validation Error" |
| 66 | + assert response.status_code == 422 |
| 67 | + |
| 68 | + async def test_post_valid_item(self, api_client, valid_stac_item, collection_in_db): |
| 69 | + """ |
| 70 | + Test the API's response to posting a valid STAC item. |
| 71 | +
|
| 72 | + Asserts that the response status code is 200. |
| 73 | + """ |
| 74 | + collection_id = valid_stac_item["collection"] |
| 75 | + response = await api_client.post( |
| 76 | + items_endpoint.format(collection_id), json=valid_stac_item |
| 77 | + ) |
| 78 | + assert response.status_code == 201 |
| 79 | + |
| 80 | + async def test_post_invalid_bulk_items(self, api_client, invalid_stac_item): |
| 81 | + """ |
| 82 | + Test the API's response to posting invalid bulk STAC items. |
| 83 | +
|
| 84 | + Asserts that the response status code is 422. |
| 85 | + """ |
| 86 | + item_id = invalid_stac_item["id"] |
| 87 | + collection_id = invalid_stac_item["collection"] |
| 88 | + invalid_request = {"items": {item_id: invalid_stac_item}, "method": "upsert"} |
| 89 | + |
| 90 | + response = await api_client.post( |
| 91 | + bulk_endpoint.format(collection_id), json=invalid_request |
| 92 | + ) |
| 93 | + assert response.status_code == 422 |
| 94 | + |
| 95 | + async def test_post_valid_bulk_items( |
| 96 | + self, api_client, valid_stac_item, collection_in_db |
| 97 | + ): |
| 98 | + """ |
| 99 | + Test the API's response to posting valid bulk STAC items. |
| 100 | +
|
| 101 | + Asserts that the response status code is 200. |
| 102 | + """ |
| 103 | + item_id = valid_stac_item["id"] |
| 104 | + collection_id = valid_stac_item["collection"] |
| 105 | + valid_request = {"items": {item_id: valid_stac_item}, "method": "upsert"} |
| 106 | + |
| 107 | + response = await api_client.post( |
| 108 | + bulk_endpoint.format(collection_id), json=valid_request |
| 109 | + ) |
| 110 | + assert response.status_code == 200 |
| 111 | + |
| 112 | + async def test_get_collection_by_id(self, api_client, collection_in_db): |
| 113 | + """ |
| 114 | + Test searching for a specific collection by its ID. |
| 115 | + """ |
| 116 | + # The `collection_in_db` fixture ensures the collection exists and provides its ID. |
| 117 | + collection_id = collection_in_db |
| 118 | + |
| 119 | + # Perform a GET request to the /collections endpoint with an "ids" query |
| 120 | + response = await api_client.get( |
| 121 | + collections_endpoint, params={"ids": collection_id} |
| 122 | + ) |
| 123 | + |
| 124 | + assert response.status_code == 200 |
| 125 | + |
| 126 | + response_data = response.json() |
| 127 | + |
| 128 | + assert response_data["collections"][0]["id"] == collection_id |
| 129 | + |
| 130 | + async def test_collection_freetext_search_by_title( |
| 131 | + self, api_client, valid_stac_collection, collection_in_db |
| 132 | + ): |
| 133 | + """ |
| 134 | + Test free-text search for a collection using a word from its title. |
| 135 | + """ |
| 136 | + |
| 137 | + # The `collection_in_db` fixture ensures the collection exists. |
| 138 | + collection_id = collection_in_db |
| 139 | + |
| 140 | + # Use a unique word from the collection's title for the query. |
| 141 | + search_term = "precipitation" |
| 142 | + |
| 143 | + # Perform a GET request with the `q` free-text search parameter. |
| 144 | + response = await api_client.get(collections_endpoint, params={"q": search_term}) |
| 145 | + |
| 146 | + assert response.status_code == 200 |
| 147 | + response_data = response.json() |
| 148 | + |
| 149 | + assert len(response_data["collections"]) > 0 |
| 150 | + |
| 151 | + returned_ids = [col["id"] for col in response_data["collections"]] |
| 152 | + assert collection_id in returned_ids |
0 commit comments