|
1 | 1 | import re |
2 | | -from datetime import datetime |
| 2 | +from datetime import datetime, timezone |
3 | 3 | from unittest.mock import AsyncMock, MagicMock, patch |
4 | 4 |
|
5 | 5 | import pytest |
@@ -130,6 +130,87 @@ async def test_execute_internal_legacy_kwargs_passthrough(self): |
130 | 130 | cache_expiration_time=100, |
131 | 131 | ) |
132 | 132 |
|
| 133 | + async def test_cache_size_different_schema(self): |
| 134 | + """A cached result is only reused when it ran against the same schema (#739). |
| 135 | +
|
| 136 | + Mirrors the synchronous cursor test: identical SQL can resolve to different |
| 137 | + tables depending on the database it runs against, so a prior execution from |
| 138 | + another schema must not be a cache hit. |
| 139 | + """ |
| 140 | + query = "SELECT * FROM one_row" |
| 141 | + |
| 142 | + def execution(schema): |
| 143 | + return AthenaQueryExecution( |
| 144 | + { |
| 145 | + "QueryExecution": { |
| 146 | + "QueryExecutionId": f"query_id_{schema}", |
| 147 | + "Query": query, |
| 148 | + "StatementType": AthenaQueryExecution.STATEMENT_TYPE_DML, |
| 149 | + "QueryExecutionContext": {"Database": schema}, |
| 150 | + "Status": { |
| 151 | + "State": AthenaQueryExecution.STATE_SUCCEEDED, |
| 152 | + "CompletionDateTime": datetime.now(timezone.utc), |
| 153 | + }, |
| 154 | + } |
| 155 | + } |
| 156 | + ) |
| 157 | + |
| 158 | + cursor = AioCursor.__new__(AioCursor) # bypass __init__ to avoid AWS calls |
| 159 | + cursor._catalog_name = None |
| 160 | + |
| 161 | + with patch.object( |
| 162 | + AioCursor, |
| 163 | + "_list_query_executions", |
| 164 | + new_callable=AsyncMock, |
| 165 | + return_value=(None, [execution("other_schema")]), |
| 166 | + ): |
| 167 | + cursor._schema_name = "this_schema" |
| 168 | + assert await cursor._find_previous_query_id(query, None, cache_size=100) is None |
| 169 | + cursor._schema_name = "other_schema" |
| 170 | + assert ( |
| 171 | + await cursor._find_previous_query_id(query, None, cache_size=100) |
| 172 | + == "query_id_other_schema" |
| 173 | + ) |
| 174 | + |
| 175 | + async def test_cache_size_different_catalog(self): |
| 176 | + query = "SELECT * FROM one_row" |
| 177 | + schema = "this_schema" |
| 178 | + |
| 179 | + def execution(catalog): |
| 180 | + return AthenaQueryExecution( |
| 181 | + { |
| 182 | + "QueryExecution": { |
| 183 | + "QueryExecutionId": f"query_id_{catalog}", |
| 184 | + "Query": query, |
| 185 | + "StatementType": AthenaQueryExecution.STATEMENT_TYPE_DML, |
| 186 | + "QueryExecutionContext": {"Database": schema, "Catalog": catalog}, |
| 187 | + "Status": { |
| 188 | + "State": AthenaQueryExecution.STATE_SUCCEEDED, |
| 189 | + "CompletionDateTime": datetime.now(timezone.utc), |
| 190 | + }, |
| 191 | + } |
| 192 | + } |
| 193 | + ) |
| 194 | + |
| 195 | + cursor = AioCursor.__new__(AioCursor) |
| 196 | + cursor._schema_name = schema |
| 197 | + |
| 198 | + with patch.object( |
| 199 | + AioCursor, |
| 200 | + "_list_query_executions", |
| 201 | + new_callable=AsyncMock, |
| 202 | + return_value=(None, [execution("awsdatacatalog")]), |
| 203 | + ): |
| 204 | + # A different catalog must not be a cache hit. |
| 205 | + cursor._catalog_name = "other_catalog" |
| 206 | + assert await cursor._find_previous_query_id(query, None, cache_size=100) is None |
| 207 | + # The same catalog, differing only in case, must still be a cache hit. |
| 208 | + cursor._catalog_name = "AwsDataCatalog" |
| 209 | + assert ( |
| 210 | + await cursor._find_previous_query_id(query, None, cache_size=100) |
| 211 | + == "query_id_awsdatacatalog" |
| 212 | + ) |
| 213 | + |
133 | 214 | async def test_no_result_set_raises(self, aio_cursor): |
134 | 215 | with pytest.raises(ProgrammingError): |
135 | 216 | await aio_cursor.fetchone() |
|
0 commit comments