use rbs::{from_value, to_value, Value, VecStruct};
use rbs::value::map::ValueMap;
#[test]
fn test_vec_struct_from_array_of_maps() {
let map1 = {
let mut m = ValueMap::new();
m.insert("type".into(), "table".into());
m.insert("name".into(), "activity".into());
m.insert("tbl_name".into(), "activity".into());
m.insert("sql".into(), "CREATE TABLE...".into());
Value::Map(m)
};
let value = Value::Array(vec![map1]);
let result: VecStruct = from_value(value).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0]["type"].as_str().unwrap(), "table");
assert_eq!(result.0[0]["name"].as_str().unwrap(), "activity");
}
#[test]
fn test_vec_struct_from_csv_format() {
let headers = Value::Array(vec![
"type".into(),
"name".into(),
"tbl_name".into(),
"sql".into(),
]);
let row1 = Value::Array(vec![
"table".into(),
"activity".into(),
"activity".into(),
"CREATE TABLE activity...".into(),
]);
let value = Value::Array(vec![headers, row1]);
let result: VecStruct = from_value(value).unwrap();
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0]["type"].as_str().unwrap(), "table");
assert_eq!(result.0[0]["name"].as_str().unwrap(), "activity");
assert_eq!(result.0[0]["tbl_name"].as_str().unwrap(), "activity");
assert_eq!(result.0[0]["sql"].as_str().unwrap(), "CREATE TABLE activity...");
}
#[test]
fn test_vec_struct_roundtrip() {
let map1 = {
let mut m = ValueMap::new();
m.insert("type".into(), "table".into());
m.insert("name".into(), "activity".into());
Value::Map(m)
};
let value = Value::Array(vec![map1]);
let result: VecStruct = from_value(value).unwrap();
let output: Value = to_value(result).unwrap();
assert!(output.is_array());
let arr = output.as_array().unwrap();
assert!(arr[0].is_map());
}
#[test]
fn test_vec_struct_empty() {
let value = Value::Array(vec![]);
let result: VecStruct = from_value(value).unwrap();
assert!(result.0.is_empty());
}
#[test]
fn test_vec_struct_to_csv_value() {
let map1 = {
let mut m = ValueMap::new();
m.insert("type".into(), "table".into());
m.insert("name".into(), "activity".into());
Value::Map(m)
};
let value = Value::Array(vec![map1]);
let result: VecStruct = from_value(value).unwrap();
let csv = result.to_csv_value();
let arr = csv.as_array().unwrap();
assert!(arr[0].is_array()); let headers = arr[0].as_array().unwrap();
assert_eq!(headers.len(), 2); }
#[test]
fn test_vec_struct_multiple_rows() {
let headers = Value::Array(vec!["type".into(), "name".into()]);
let row1 = Value::Array(vec!["table".into(), "activity".into()]);
let row2 = Value::Array(vec!["index".into(), "idx_activity".into()]);
let value = Value::Array(vec![headers, row1, row2]);
let result: VecStruct = from_value(value).unwrap();
assert_eq!(result.0.len(), 2);
assert_eq!(result.0[0]["type"].as_str().unwrap(), "table");
assert_eq!(result.0[1]["type"].as_str().unwrap(), "index");
}