Skip to content

Commit 7bd5d9e

Browse files
authored
table_exists unit/integration test for NoSuchTableError (#678)
1 parent da313d0 commit 7bd5d9e

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

tests/catalog/integration_test_dynamodb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,9 @@ def test_update_namespace_properties(test_catalog: Catalog, database_name: str)
262262
else:
263263
assert k in update_report.removed
264264
assert "updated test description" == test_catalog.load_namespace_properties(database_name)["comment"]
265+
266+
267+
def test_table_exists(test_catalog: Catalog, table_schema_nested: Schema, database_name: str, table_name: str) -> None:
268+
test_catalog.create_namespace(database_name)
269+
test_catalog.create_table((database_name, table_name), table_schema_nested)
270+
assert test_catalog.table_exists((database_name, table_name)) is True

tests/catalog/integration_test_glue.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,9 @@ def test_create_table_transaction(
558558
]
559559
},
560560
]
561+
562+
563+
def test_table_exists(test_catalog: Catalog, table_schema_nested: Schema, table_name: str, database_name: str) -> None:
564+
test_catalog.create_namespace(database_name)
565+
test_catalog.create_table((database_name, table_name), table_schema_nested)
566+
assert test_catalog.table_exists((database_name, table_name)) is True

tests/catalog/test_dynamodb.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,17 @@ def test_passing_provided_profile() -> None:
562562
assert test_catalog.dynamodb is mock_client
563563
mock_session.assert_called_with(**session_props)
564564
assert test_catalog.dynamodb is mock_session().client()
565+
566+
567+
@mock_aws
568+
def test_table_exists(
569+
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
570+
) -> None:
571+
identifier = (database_name, table_name)
572+
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
573+
test_catalog.create_namespace(namespace=database_name)
574+
test_catalog.create_table(identifier, table_schema_nested)
575+
# Act and Assert for an existing table
576+
assert test_catalog.table_exists(identifier) is True
577+
# Act and Assert for an non-existing table
578+
assert test_catalog.table_exists(('non', 'exist')) is False

tests/catalog/test_glue.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,18 @@ def test_create_table_transaction(
817817
assert table.spec().fields_by_source_id(2)[0].name == "bar"
818818
assert table.spec().fields_by_source_id(2)[0].field_id == 1001
819819
assert table.spec().fields_by_source_id(2)[0].transform == IdentityTransform()
820+
821+
822+
@mock_aws
823+
def test_table_exists(
824+
_bucket_initialize: None, moto_endpoint_url: str, table_schema_simple: Schema, database_name: str, table_name: str
825+
) -> None:
826+
catalog_name = "glue"
827+
identifier = (database_name, table_name)
828+
test_catalog = GlueCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
829+
test_catalog.create_namespace(namespace=database_name)
830+
test_catalog.create_table(identifier=identifier, schema=table_schema_simple)
831+
# Act and Assert for an existing table
832+
assert test_catalog.table_exists(identifier) is True
833+
# Act and Assert for a non-existing table
834+
assert test_catalog.table_exists(('non', 'exist')) is False

tests/catalog/test_sql.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,3 +982,22 @@ def test_table_properties_raise_for_none_value(
982982
with pytest.raises(ValidationError) as exc_info:
983983
_ = catalog.create_table(random_identifier, table_schema_simple, properties=property_with_none)
984984
assert "None type is not a supported value in properties: property_name" in str(exc_info.value)
985+
986+
987+
@pytest.mark.parametrize(
988+
'catalog',
989+
[
990+
lazy_fixture('catalog_memory'),
991+
lazy_fixture('catalog_sqlite'),
992+
],
993+
)
994+
def test_table_exists(catalog: SqlCatalog, table_schema_simple: Schema, random_identifier: Identifier) -> None:
995+
database_name, _table_name = random_identifier
996+
catalog.create_namespace(database_name)
997+
catalog.create_table(random_identifier, table_schema_simple, properties={"format-version": "2"})
998+
existing_table = random_identifier
999+
# Act and Assert for an existing table
1000+
assert catalog.table_exists(existing_table) is True
1001+
1002+
# Act and Assert for a non-existing table
1003+
assert catalog.table_exists(('non', 'exist')) is False

0 commit comments

Comments
 (0)