|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from hotdata.exceptions import ApiException |
| 8 | +from hotdata.models.database_default_table_decl import DatabaseDefaultTableDecl |
8 | 9 |
|
9 | 10 | from hotdata_framework.client import HotdataClient |
10 | 11 | from hotdata_framework.databases import ( |
|
13 | 14 | ) |
14 | 15 |
|
15 | 16 |
|
| 17 | +def _decl_key_supported() -> bool: |
| 18 | + # `key` ships with the regenerated client; the key tests activate once it does. |
| 19 | + try: |
| 20 | + return DatabaseDefaultTableDecl(name="t", key=["k"]).key == ["k"] |
| 21 | + except Exception: |
| 22 | + return False |
| 23 | + |
| 24 | + |
| 25 | +requires_key_field = pytest.mark.skipif( |
| 26 | + not _decl_key_supported(), |
| 27 | + reason="hotdata client without `key` on managed-table decls", |
| 28 | +) |
| 29 | + |
| 30 | + |
16 | 31 | def _client() -> HotdataClient: |
17 | 32 | return HotdataClient("k", "ws", host="https://api.hotdata.dev") |
18 | 33 |
|
@@ -230,6 +245,69 @@ def test_load_managed_table_requires_exactly_one_source(): |
230 | 245 | ) |
231 | 246 |
|
232 | 247 |
|
| 248 | +def _load_and_capture_request(client, **kwargs): |
| 249 | + db = managed_database_from_detail(_detail()) |
| 250 | + loaded = SimpleNamespace( |
| 251 | + connection_id="conn_1", schema_name="public", table_name="orders", row_count=1 |
| 252 | + ) |
| 253 | + with ( |
| 254 | + patch.object(client, "resolve_managed_database", return_value=db), |
| 255 | + patch.object(client, "connections") as connections, |
| 256 | + ): |
| 257 | + connections.return_value.load_managed_table.return_value = loaded |
| 258 | + client.load_managed_table("db_1", "orders", upload_id="upl_1", **kwargs) |
| 259 | + return connections.return_value.load_managed_table.call_args.args[3] |
| 260 | + |
| 261 | + |
| 262 | +def test_load_managed_table_defaults_to_replace(): |
| 263 | + assert _load_and_capture_request(_client()).mode == "replace" |
| 264 | + |
| 265 | + |
| 266 | +@pytest.mark.parametrize("mode", ["append", "delete", "update", "upsert"]) |
| 267 | +def test_load_managed_table_forwards_mode(mode: str): |
| 268 | + assert _load_and_capture_request(_client(), mode=mode).mode == mode |
| 269 | + |
| 270 | + |
| 271 | +@requires_key_field |
| 272 | +def test_create_managed_database_declares_keys(): |
| 273 | + client = _client() |
| 274 | + with patch.object(client, "_databases_api") as dbs: |
| 275 | + dbs.return_value.create_database.return_value = _detail(id="db_new") |
| 276 | + client.create_managed_database( |
| 277 | + "mydb", tables=["orders", "events"], keys={"orders": ["id"]} |
| 278 | + ) |
| 279 | + req = dbs.return_value.create_database.call_args.args[0] |
| 280 | + declared = {t.name: list(t.key) for t in req.schemas[0].tables} |
| 281 | + assert declared == {"orders": ["id"], "events": []} |
| 282 | + |
| 283 | + |
| 284 | +@requires_key_field |
| 285 | +def test_add_managed_table_declares_key(): |
| 286 | + client = _client() |
| 287 | + db = managed_database_from_detail(_detail()) |
| 288 | + with ( |
| 289 | + patch.object(client, "resolve_managed_database", return_value=db), |
| 290 | + patch.object(client, "_databases_api") as dbs, |
| 291 | + ): |
| 292 | + client.add_managed_table("db_1", "line_items", key=["order_id", "sku"]) |
| 293 | + req = dbs.return_value.add_database_table.call_args.args[2] |
| 294 | + assert req.name == "line_items" |
| 295 | + assert list(req.key) == ["order_id", "sku"] |
| 296 | + |
| 297 | + |
| 298 | +@requires_key_field |
| 299 | +def test_add_managed_table_keyless_by_default(): |
| 300 | + client = _client() |
| 301 | + db = managed_database_from_detail(_detail()) |
| 302 | + with ( |
| 303 | + patch.object(client, "resolve_managed_database", return_value=db), |
| 304 | + patch.object(client, "_databases_api") as dbs, |
| 305 | + ): |
| 306 | + client.add_managed_table("db_1", "orders") |
| 307 | + req = dbs.return_value.add_database_table.call_args.args[2] |
| 308 | + assert list(req.key) == [] |
| 309 | + |
| 310 | + |
233 | 311 | def test_delete_managed_table_uses_default_connection_id(): |
234 | 312 | client = _client() |
235 | 313 | db = managed_database_from_detail(_detail()) |
|
0 commit comments