Skip to content

Commit

Permalink
test: add compatible test for OpenRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
niebayes committed Dec 19, 2023
1 parent 9a3a967 commit 53bf3a5
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/common/meta/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,26 @@ impl Display for OpenRegion {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OpenRegion {
pub region_ident: RegionIdent,
pub region_storage_path: String,
pub options: HashMap<String, String>,
pub region_options: HashMap<String, String>,
#[serde(default)]
pub region_wal_options: HashMap<String, String>,
}

impl OpenRegion {
pub fn new(
region_ident: RegionIdent,
path: &str,
options: HashMap<String, String>,
region_options: HashMap<String, String>,
region_wal_options: HashMap<String, String>,
) -> Self {
Self {
region_ident,
region_storage_path: path.to_string(),
options,
region_options,
region_wal_options,
}
}
Expand Down Expand Up @@ -231,7 +232,7 @@ mod tests {
let serialized = serde_json::to_string(&open_region).unwrap();

assert_eq!(
r#"{"OpenRegion":{"region_ident":{"cluster_id":1,"datanode_id":2,"table_id":1024,"region_number":1,"engine":"mito2"},"region_storage_path":"test/foo","options":{},"region_wal_options":{}}}"#,
r#"{"OpenRegion":{"region_ident":{"cluster_id":1,"datanode_id":2,"table_id":1024,"region_number":1,"engine":"mito2"},"region_storage_path":"test/foo","region_options":{},"region_wal_options":{}}}"#,
serialized
);

Expand All @@ -250,4 +251,45 @@ mod tests {
serialized
);
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct LegacyOpenRegion {
pub region_ident: RegionIdent,
pub region_storage_path: String,
pub region_options: HashMap<String, String>,
}

#[test]
fn test_compatible_serialize_open_region() {
let region_ident = RegionIdent {
cluster_id: 1,
datanode_id: 2,
table_id: 1024,
region_number: 1,
engine: "mito2".to_string(),
};
let region_storage_path = "test/foo".to_string();
let region_options = HashMap::from([
("a".to_string(), "aa".to_string()),
("b".to_string(), "bb".to_string()),
]);

// Serialize a legacy OpenRegion.
let legacy_open_region = LegacyOpenRegion {
region_ident: region_ident.clone(),
region_storage_path: region_storage_path.clone(),
region_options: region_options.clone(),
};
let serialized = serde_json::to_string(&legacy_open_region).unwrap();

// Deserialize to OpenRegion.
let deserialized = serde_json::from_str(&serialized).unwrap();
let expected = OpenRegion {
region_ident: region_ident.clone(),
region_storage_path: region_storage_path.clone(),
region_options: region_options.clone(),
region_wal_options: HashMap::new(),
};
assert_eq!(expected, deserialized);
}
}

0 comments on commit 53bf3a5

Please sign in to comment.