Skip to content
Snippets Groups Projects
Commit 5b4db296 authored by Georgy Moshkin's avatar Georgy Moshkin :speech_balloon:
Browse files

test: add tests to check instance & replicaset structs match their space formats

parent c60ddaf2
No related branches found
No related tags found
1 merge request!735Gmoshkin/minor fixes
......@@ -603,5 +603,22 @@ mod tests {
"tier \"noexistent_tier\" for current instance should exists",
);
}
}
#[cfg(test)]
mod test {
use super::*;
use tarantool::tuple::ToTupleBuffer;
#[test]
fn matches_format() {
let i = Instance::default();
let tuple_data = i.to_tuple_buffer().unwrap();
let format = crate::storage::instance_format();
crate::util::check_tuple_matches_format(
tuple_data.as_ref(),
&format,
"define_instance_fields",
);
}
}
......@@ -114,3 +114,17 @@ impl std::fmt::Display for Replicaset {
UpToDate = "up-to-date",
}
}
#[cfg(test)]
mod tests {
use super::*;
use tarantool::tuple::ToTupleBuffer;
#[test]
fn matches_format() {
let r = Replicaset::replicaset_for_tests();
let tuple_data = r.to_tuple_buffer().unwrap();
let format = Replicaset::format();
crate::util::check_tuple_matches_format(tuple_data.as_ref(), &format, "Replicaset::format");
}
}
......@@ -1572,7 +1572,7 @@ macro_rules! define_instance_fields {
)+
}
fn instance_format() -> Vec<::tarantool::space::Field> {
pub fn instance_format() -> Vec<::tarantool::space::Field> {
vec![
$( ::tarantool::space::Field::from(($name, $tt_ty)), )+
]
......
......@@ -598,6 +598,48 @@ pub(crate) fn effective_user_id() -> UserId {
session::euid().expect("infallible in picodata")
}
#[cfg(test)]
use tarantool::space::Field;
#[cfg(test)]
#[track_caller]
pub fn check_tuple_matches_format(tuple: &[u8], format: &[Field], what_to_fix: &str) {
use tarantool::space::FieldType;
use tarantool::tuple::Decode;
let value = rmpv::Value::decode(tuple).unwrap();
let fields = value.as_array().unwrap();
assert_eq!(
fields.len(),
format.len(),
"don't forget to update {what_to_fix}!"
);
for i in 0..fields.len() {
let field = &fields[i];
let field_type = format[i].field_type;
let field_name = &format[i].name;
let ok = match field_type {
FieldType::Any => true,
FieldType::Unsigned => field.is_u64(),
FieldType::String => field.is_str(),
FieldType::Number => field.is_number(),
FieldType::Double => field.is_f32() || field.is_f64(),
FieldType::Integer => field.is_i64(),
FieldType::Boolean => field.is_bool(),
FieldType::Varbinary => todo!(),
FieldType::Scalar => todo!(),
FieldType::Decimal | FieldType::Uuid | FieldType::Datetime | FieldType::Interval => {
field.is_ext()
}
FieldType::Array => field.is_array(),
FieldType::Map => field.is_map(),
};
if !ok {
panic!("expected field '{field_name}' to be {field_type:?}, but got {field:?}");
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// tests
#[cfg(test)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment