Skip to content

Commit 99db14d

Browse files
committed
test: add unit tests for register table
Signed-off-by: Alan Tang <[email protected]>
1 parent d831a19 commit 99db14d

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

crates/catalog/sql/src/catalog.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ impl SqlCatalog {
216216
}
217217
}
218218
}
219-
220-
/// Get the catalogs `FileIO`
221-
pub fn file_io(&self) -> FileIO {
222-
self.fileio.clone()
223-
}
224219
}
225220

226221
#[async_trait]
@@ -773,11 +768,11 @@ impl Catalog for SqlCatalog {
773768
table_ident: &TableIdent,
774769
metadata_location: String,
775770
) -> Result<Table> {
776-
if !self.table_exists(table_ident).await? {
777-
return no_such_table_err(table_ident);
771+
if self.table_exists(table_ident).await? {
772+
return table_already_exists_err(table_ident);
778773
}
779774

780-
let metadata = TableMetadata::read_from(&self.fileio, metadata_location.clone()).await?;
775+
let metadata = TableMetadata::read_from(&self.fileio, &metadata_location).await?;
781776

782777
let namespace = table_ident.namespace();
783778
let tbl_name = table_ident.name().to_string();
@@ -792,7 +787,7 @@ impl Catalog for SqlCatalog {
792787
.identifier(table_ident.clone())
793788
.metadata_location(metadata_location)
794789
.metadata(metadata)
795-
.file_io(self.file_io())
790+
.file_io(self.fileio.clone())
796791
.build()?)
797792
}
798793

@@ -1930,4 +1925,58 @@ mod tests {
19301925
"Unexpected => No such table: TableIdent { namespace: NamespaceIdent([\"a\"]), name: \"tbl1\" }"
19311926
);
19321927
}
1928+
1929+
#[tokio::test]
1930+
async fn test_register_table_throws_error_if_table_with_same_name_already_exists() {
1931+
let warehouse_loc = temp_path();
1932+
let catalog = new_sql_catalog(warehouse_loc.clone()).await;
1933+
let namespace_ident = NamespaceIdent::new("a".into());
1934+
create_namespace(&catalog, &namespace_ident).await;
1935+
let table_name = "tbl1";
1936+
let table_ident = TableIdent::new(namespace_ident.clone(), table_name.into());
1937+
create_table(&catalog, &table_ident).await;
1938+
1939+
assert_eq!(
1940+
catalog
1941+
.register_table(&table_ident, warehouse_loc)
1942+
.await
1943+
.unwrap_err()
1944+
.to_string(),
1945+
format!("Unexpected => Table {:?} already exists.", &table_ident)
1946+
);
1947+
}
1948+
1949+
#[tokio::test]
1950+
async fn test_register_table() {
1951+
let warehouse_loc = temp_path();
1952+
let catalog = new_sql_catalog(warehouse_loc.clone()).await;
1953+
let namespace_ident = NamespaceIdent::new("a".into());
1954+
create_namespace(&catalog, &namespace_ident).await;
1955+
1956+
let table_name = "abc";
1957+
let location = warehouse_loc.clone();
1958+
let table_creation = TableCreation::builder()
1959+
.name(table_name.into())
1960+
.location(location.clone())
1961+
.schema(simple_table_schema())
1962+
.build();
1963+
1964+
let table_ident = TableIdent::new(namespace_ident.clone(), table_name.into());
1965+
let expected_table = catalog
1966+
.create_table(&namespace_ident, table_creation)
1967+
.await
1968+
.unwrap();
1969+
1970+
assert_table_eq(&expected_table, &table_ident, &simple_table_schema());
1971+
1972+
let _ = catalog.drop_table(&table_ident).await;
1973+
1974+
let table = catalog
1975+
.register_table(&table_ident, location.clone())
1976+
.await
1977+
.unwrap();
1978+
1979+
assert_eq!(table.identifier(), expected_table.identifier());
1980+
assert_eq!(table.metadata_location(), Some(location.as_str()));
1981+
}
19331982
}

0 commit comments

Comments
 (0)