|
16 | 16 | # under the License. |
17 | 17 | # pylint: disable=redefined-outer-name,unused-argument |
18 | 18 | import os |
19 | | -from typing import Any, Dict, cast |
| 19 | +from typing import Any, Callable, Dict, cast |
20 | 20 | from unittest import mock |
21 | 21 |
|
22 | 22 | import pytest |
@@ -560,6 +560,64 @@ def test_create_table_409(rest_mock: Mocker, table_schema_simple: Schema) -> Non |
560 | 560 | assert "Table already exists" in str(e.value) |
561 | 561 |
|
562 | 562 |
|
| 563 | +def test_create_table_if_not_exists_200( |
| 564 | + rest_mock: Mocker, table_schema_simple: Schema, example_table_metadata_no_snapshot_v1_rest_json: Dict[str, Any] |
| 565 | +) -> None: |
| 566 | + def json_callback() -> Callable[[Any, Any], Dict[str, Any]]: |
| 567 | + call_count = 0 |
| 568 | + |
| 569 | + def callback(request: Any, context: Any) -> Dict[str, Any]: |
| 570 | + nonlocal call_count |
| 571 | + call_count += 1 |
| 572 | + |
| 573 | + if call_count == 1: |
| 574 | + context.status_code = 200 |
| 575 | + return example_table_metadata_no_snapshot_v1_rest_json |
| 576 | + else: |
| 577 | + context.status_code = 409 |
| 578 | + return { |
| 579 | + "error": { |
| 580 | + "message": "Table already exists: fokko.already_exists in warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e", |
| 581 | + "type": "AlreadyExistsException", |
| 582 | + "code": 409, |
| 583 | + } |
| 584 | + } |
| 585 | + |
| 586 | + return callback |
| 587 | + |
| 588 | + rest_mock.post( |
| 589 | + f"{TEST_URI}v1/namespaces/fokko/tables", |
| 590 | + json=json_callback(), |
| 591 | + request_headers=TEST_HEADERS, |
| 592 | + ) |
| 593 | + rest_mock.get( |
| 594 | + f"{TEST_URI}v1/namespaces/fokko/tables/fokko2", |
| 595 | + json=example_table_metadata_no_snapshot_v1_rest_json, |
| 596 | + status_code=200, |
| 597 | + request_headers=TEST_HEADERS, |
| 598 | + ) |
| 599 | + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) |
| 600 | + table1 = catalog.create_table( |
| 601 | + identifier=("fokko", "fokko2"), |
| 602 | + schema=table_schema_simple, |
| 603 | + location=None, |
| 604 | + partition_spec=PartitionSpec( |
| 605 | + PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=3), name="id"), spec_id=1 |
| 606 | + ), |
| 607 | + sort_order=SortOrder(SortField(source_id=2, transform=IdentityTransform())), |
| 608 | + properties={"owner": "fokko"}, |
| 609 | + ) |
| 610 | + table2 = catalog.create_table_if_not_exists( |
| 611 | + identifier=("fokko", "fokko2"), |
| 612 | + schema=table_schema_simple, |
| 613 | + location=None, |
| 614 | + partition_spec=PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=3), name="id")), |
| 615 | + sort_order=SortOrder(SortField(source_id=2, transform=IdentityTransform())), |
| 616 | + properties={"owner": "fokko"}, |
| 617 | + ) |
| 618 | + assert table1 == table2 |
| 619 | + |
| 620 | + |
563 | 621 | def test_create_table_419(rest_mock: Mocker, table_schema_simple: Schema) -> None: |
564 | 622 | rest_mock.post( |
565 | 623 | f"{TEST_URI}v1/namespaces/fokko/tables", |
|
0 commit comments