From 09eef561a0d50616043bafd3f2b5355e9822bdee Mon Sep 17 00:00:00 2001 From: WenyXu Date: Mon, 29 Apr 2024 09:17:20 +0000 Subject: [PATCH] test: add tests --- src/common/meta/src/key/catalog_name.rs | 2 +- src/common/meta/src/key/schema_name.rs | 3 ++- src/common/meta/src/key/table_info.rs | 12 ++++++++-- src/common/meta/src/key/table_name.rs | 31 +++++-------------------- src/common/meta/src/key/table_region.rs | 9 ++++--- src/common/meta/src/key/table_route.rs | 15 ++++++++++++ 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/common/meta/src/key/catalog_name.rs b/src/common/meta/src/key/catalog_name.rs index 5ff4ee46f9ae..fd67acf34800 100644 --- a/src/common/meta/src/key/catalog_name.rs +++ b/src/common/meta/src/key/catalog_name.rs @@ -164,7 +164,7 @@ mod tests { assert_eq!(key.to_string(), "__catalog_name/my-catalog"); - let parsed: CatalogNameKey = "__catalog_name/my-catalog".try_into().unwrap(); + let parsed = CatalogNameKey::from_bytes(b"__catalog_name/my-catalog").unwrap(); assert_eq!(key, parsed); } diff --git a/src/common/meta/src/key/schema_name.rs b/src/common/meta/src/key/schema_name.rs index db33b969aa2f..91c4c74bc104 100644 --- a/src/common/meta/src/key/schema_name.rs +++ b/src/common/meta/src/key/schema_name.rs @@ -238,7 +238,8 @@ mod tests { let key = SchemaNameKey::new("my-catalog", "my-schema"); assert_eq!(key.to_string(), "__schema_name/my-catalog/my-schema"); - let parsed: SchemaNameKey<'_> = "__schema_name/my-catalog/my-schema".try_into().unwrap(); + let parsed = SchemaNameKey::from_bytes(b"__schema_name/my-catalog/my-schema").unwrap(); + assert_eq!(key, parsed); let value = SchemaNameValue { diff --git a/src/common/meta/src/key/table_info.rs b/src/common/meta/src/key/table_info.rs index 28a7855699af..7283281c14ab 100644 --- a/src/common/meta/src/key/table_info.rs +++ b/src/common/meta/src/key/table_info.rs @@ -34,6 +34,7 @@ use crate::table_name::TableName; /// The key stores the metadata of the table. /// /// The layout: `__table_info/{table_id}`. +#[derive(Debug, PartialEq)] pub struct TableInfoKey { table_id: TableId, } @@ -281,14 +282,21 @@ mod tests { } #[test] - fn test_key_serde() { + fn test_key_serialization() { let key = TableInfoKey::new(42); let raw_key = key.to_bytes(); assert_eq!(raw_key, b"__table_info/42"); } #[test] - fn test_value_serde() { + fn test_key_deserialization() { + let expected = TableInfoKey::new(42); + let key = TableInfoKey::from_bytes(b"__table_info/42").unwrap(); + assert_eq!(key, expected); + } + + #[test] + fn test_value_serialization() { let value = TableInfoValue { table_info: new_table_info(42), version: 1, diff --git a/src/common/meta/src/key/table_name.rs b/src/common/meta/src/key/table_name.rs index 52947227b802..fd52ac95d715 100644 --- a/src/common/meta/src/key/table_name.rs +++ b/src/common/meta/src/key/table_name.rs @@ -50,26 +50,6 @@ impl<'a> TableNameKey<'a> { pub fn prefix_to_table(catalog: &str, schema: &str) -> String { format!("{}/{}/{}", TABLE_NAME_KEY_PREFIX, catalog, schema) } - - fn strip_table_name(raw_key: &[u8]) -> Result { - let key = String::from_utf8(raw_key.to_vec()).map_err(|e| { - InvalidTableMetadataSnafu { - err_msg: format!( - "TableNameKey '{}' is not a valid UTF8 string: {e}", - String::from_utf8_lossy(raw_key) - ), - } - .build() - })?; - let captures = - TABLE_NAME_KEY_PATTERN - .captures(&key) - .context(InvalidTableMetadataSnafu { - err_msg: format!("Invalid TableNameKey '{key}'"), - })?; - // Safety: pass the regex check above - Ok(captures[3].to_string()) - } } impl Display for TableNameKey<'_> { @@ -116,10 +96,10 @@ impl<'a> MetaKey<'a, TableNameKey<'a>> for TableNameKey<'_> { /// Decodes `KeyValue` to ({table_name}, TableNameValue) pub fn table_decoder(kv: KeyValue) -> Result<(String, TableNameValue)> { - let table_name = TableNameKey::strip_table_name(kv.key())?; + let table_name_key = TableNameKey::from_bytes(&kv.key)?; let table_name_value = TableNameValue::try_from_raw_value(&kv.value)?; - Ok((table_name, table_name_value)) + Ok((table_name_key.table.to_string(), table_name_value)) } impl<'a> From<&'a TableName> for TableNameKey<'a> { @@ -290,8 +270,8 @@ mod tests { #[test] fn test_strip_table_name() { - fn test_err(raw_key: &[u8]) { - assert!(TableNameKey::strip_table_name(raw_key).is_err()); + fn test_err(bytes: &[u8]) { + assert!(TableNameKey::from_bytes(bytes).is_err()); } test_err(b""); @@ -308,10 +288,11 @@ mod tests { fn test_ok(table_name: &str) { assert_eq!( table_name, - TableNameKey::strip_table_name( + TableNameKey::from_bytes( format!("__table_name/my_catalog/my_schema/{}", table_name).as_bytes() ) .unwrap() + .table ); } test_ok("my_table"); diff --git a/src/common/meta/src/key/table_region.rs b/src/common/meta/src/key/table_region.rs index 044161a6ccb5..4ccc99ba513d 100644 --- a/src/common/meta/src/key/table_region.rs +++ b/src/common/meta/src/key/table_region.rs @@ -32,6 +32,7 @@ pub type RegionDistribution = BTreeMap>; since = "0.4.0", note = "Please use the TableRouteManager's get_region_distribution method instead" )] +#[derive(Debug, PartialEq)] pub struct TableRegionKey { table_id: TableId, } @@ -107,10 +108,12 @@ mod tests { use crate::key::TableMetaValue; #[test] - fn test_serde() { - let key = TableRegionKey::new(1); + fn test_serialization() { + let key = TableRegionKey::new(24); let raw_key = key.to_bytes(); - assert_eq!(raw_key, b"__table_region/1"); + assert_eq!(raw_key, b"__table_region/24"); + let deserialized = TableRegionKey::from_bytes(b"__table_region/24").unwrap(); + assert_eq!(key, deserialized); let value = TableRegionValue { region_distribution: RegionDistribution::from([(1, vec![1, 2, 3]), (2, vec![4, 5, 6])]), diff --git a/src/common/meta/src/key/table_route.rs b/src/common/meta/src/key/table_route.rs index effdd4bfe632..eca8d702ef09 100644 --- a/src/common/meta/src/key/table_route.rs +++ b/src/common/meta/src/key/table_route.rs @@ -37,6 +37,7 @@ use crate::rpc::store::BatchGetRequest; /// The key stores table routes /// /// The layout: `__table_route/{table_id}`. +#[derive(Debug, PartialEq)] pub struct TableRouteKey { pub table_id: TableId, } @@ -628,6 +629,20 @@ mod tests { ); } + #[test] + fn test_key_serialization() { + let key = TableRouteKey::new(42); + let raw_key = key.to_bytes(); + assert_eq!(raw_key, b"__table_route/42"); + } + + #[test] + fn test_key_deserialization() { + let expected = TableRouteKey::new(42); + let key = TableRouteKey::from_bytes(b"__table_route/42").unwrap(); + assert_eq!(key, expected); + } + #[tokio::test] async fn test_table_route_storage_get_raw_empty() { let kv = Arc::new(MemoryKvBackend::default());