Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Apr 30, 2024
1 parent 9ea8af4 commit 09eef56
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/common/meta/src/key/catalog_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/meta/src/key/schema_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions src/common/meta/src/key/table_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 6 additions & 25 deletions src/common/meta/src/key/table_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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<'_> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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"");
Expand All @@ -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");
Expand Down
9 changes: 6 additions & 3 deletions src/common/meta/src/key/table_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub type RegionDistribution = BTreeMap<DatanodeId, Vec<RegionNumber>>;
since = "0.4.0",
note = "Please use the TableRouteManager's get_region_distribution method instead"
)]
#[derive(Debug, PartialEq)]
pub struct TableRegionKey {
table_id: TableId,
}
Expand Down Expand Up @@ -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])]),
Expand Down
15 changes: 15 additions & 0 deletions src/common/meta/src/key/table_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 09eef56

Please sign in to comment.