6161 NamespaceNotEmptyError ,
6262 NoSuchNamespaceError ,
6363 NoSuchTableError ,
64+ TableAlreadyExistsError ,
6465 WaitingForLockException ,
6566)
6667from pyiceberg .partitioning import PartitionField , PartitionSpec
@@ -883,6 +884,7 @@ def test_load_table_from_self_identifier(hive_table: HiveTable) -> None:
883884
884885def test_rename_table (hive_table : HiveTable ) -> None :
885886 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
887+ catalog .table_exists = MagicMock (return_value = False ) # type: ignore[method-assign]
886888
887889 renamed_table = copy .deepcopy (hive_table )
888890 renamed_table .dbName = "default"
@@ -910,6 +912,7 @@ def test_rename_table(hive_table: HiveTable) -> None:
910912
911913def test_rename_table_from_self_identifier (hive_table : HiveTable ) -> None :
912914 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
915+ catalog .table_exists = MagicMock (return_value = False ) # type: ignore[method-assign]
913916
914917 catalog ._client = MagicMock ()
915918 catalog ._client .__enter__ ().get_table .return_value = hive_table
@@ -941,6 +944,7 @@ def test_rename_table_from_self_identifier(hive_table: HiveTable) -> None:
941944
942945def test_rename_table_from_does_not_exists () -> None :
943946 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
947+ catalog .table_exists = MagicMock (return_value = False ) # type: ignore[method-assign]
944948
945949 catalog ._client = MagicMock ()
946950 catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = NoSuchObjectException (
@@ -955,6 +959,7 @@ def test_rename_table_from_does_not_exists() -> None:
955959
956960def test_rename_table_to_namespace_does_not_exists () -> None :
957961 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
962+ catalog .table_exists = MagicMock (return_value = False ) # type: ignore[method-assign]
958963
959964 catalog ._client = MagicMock ()
960965 catalog ._client .__enter__ ().alter_table_with_environment_context .side_effect = InvalidOperationException (
@@ -967,6 +972,14 @@ def test_rename_table_to_namespace_does_not_exists() -> None:
967972 assert "Database does not exists: default_does_not_exists" in str (exc_info .value )
968973
969974
975+ def test_rename_table_to_table_already_exists (hive_table : HiveTable ) -> None :
976+ catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
977+ catalog .load_table = MagicMock (return_value = hive_table ) # type: ignore[method-assign]
978+
979+ with pytest .raises (TableAlreadyExistsError ):
980+ catalog .rename_table (("default" , "some_table" ), ("default" , "new_tabl2e" ))
981+
982+
970983def test_drop_database_does_not_empty () -> None :
971984 catalog = HiveCatalog (HIVE_CATALOG_NAME , uri = HIVE_METASTORE_FAKE_URL )
972985
0 commit comments