#[allow(dead_code)]
mod support;
use mariadb_cli::output::envelope::Row;
use mariadb_cli::types::{Family, Shape};
use serde_json::{json, Value};
use support::Sandbox;
#[test]
fn the_same_call_twice_under_full_diagnostics_gives_identical_bytes() {
let sandbox = Sandbox::new();
let first = sandbox.run(&["-vvv", "meta", "schema", "--types"]);
let second = sandbox.run(&["-vvv", "meta", "schema", "--types"]);
assert_eq!(first.code, 0, "{}", first.err);
assert_eq!(
first.out, second.out,
"two runs of the same call must carry the same bytes"
);
assert!(
!first.err.is_empty() || first.out.contains("family"),
"the diagnostics were asked for and the answer still stands alone"
);
}
#[test]
fn the_answer_parses_without_editing_under_full_diagnostics() {
let sandbox = Sandbox::new();
let outcome = sandbox.run(&["-vvv", "meta", "commands"]);
let answer = outcome.answer();
assert_eq!(answer["command"], "meta commands");
assert!(answer["data"]["rows"].is_array());
}
#[test]
fn the_primary_channel_carries_no_carriage_return_and_no_byte_order_mark() {
let sandbox = Sandbox::new();
let written = sandbox.run(&["meta", "schema", "--types"]).out;
assert!(
!written.contains('\r'),
"a carriage return reached the answer"
);
assert!(
!written.starts_with('\u{feff}'),
"a byte order mark reached the answer"
);
assert!(
written.ends_with('\n'),
"the answer ends with one line ending"
);
}
#[test]
fn every_published_family_emits_the_form_the_requirement_names() {
let published: Vec<(&str, &str)> = vec![
("integer", "number"),
("wide_integer", "text"),
("exact_decimal", "text"),
("floating_point", "number"),
("text", "text"),
("absent", "absent"),
("bits", "number"),
("date", "text"),
("date_time", "text"),
("timestamp", "text"),
("time", "text"),
("year", "number"),
("single_choice", "text"),
("choice_set", "text"),
("document", "structure"),
("binary", "description"),
("geometry", "text"),
("unknown", "text"),
("uuid", "text"),
("inet4", "text"),
("inet6", "text"),
];
let sandbox = Sandbox::new();
let rows = sandbox.run(&["meta", "schema", "--types"]).rows();
assert_eq!(
rows.len(),
published.len(),
"the published table and the requirement must name the same families"
);
for (family, emitted) in published {
let row = rows
.iter()
.find(|row| row["family"] == family)
.unwrap_or_else(|| panic!("the map publishes no family `{family}`"));
assert_eq!(
row["emitted"], emitted,
"`{family}` must be emitted as {emitted}"
);
}
}
#[test]
fn the_type_map_is_fixed_and_cannot_be_configured() {
for entry in mariadb_cli::types::published() {
assert_eq!(
entry.shape,
entry.family.shape(),
"{}",
entry.family.as_str()
);
}
let governing: Vec<&str> = mariadb_cli::config::keys::all()
.iter()
.map(|entry| entry.name)
.filter(|name| name.starts_with("types."))
.collect();
assert!(
governing.is_empty(),
"the map is configurable through {governing:?}"
);
assert_eq!(Family::Document.shape(), Shape::Structure);
assert_eq!(Family::ExactDecimal.shape(), Shape::Text);
}
#[test]
fn the_compact_map_is_at_least_ten_times_smaller_than_the_full_description() {
let (tables, columns, links) = a_database_of_many_tables(300);
let (map, gaps) = mariadb_cli::introspect::map::compose(
&tables,
&columns,
&links,
false,
mariadb_cli::introspect::map::Budget {
tables: 1000,
rows: 100_000,
},
);
assert!(gaps.is_empty(), "nothing was left out: {gaps:?}");
let compact = serde_json::to_string(&map).expect("serialises").len();
let full = serde_json::to_string(&json!({"rows": columns}))
.expect("serialises")
.len();
assert!(
full >= compact * 10,
"the map is {compact} bytes and the full description {full}; \
the map must be at least ten times smaller"
);
}
#[test]
fn no_binary_or_vector_column_swells_the_map_with_content() {
let (tables, mut columns, links) = a_database_of_many_tables(1);
columns.push(catalog_column("t0", "picture", "longblob", false));
columns.push(catalog_column("t0", "embedding", "vector(1536)", false));
let budget = mariadb_cli::introspect::map::Budget {
tables: 10,
rows: 100,
};
let (map, _) = mariadb_cli::introspect::map::compose(&tables, &columns, &links, false, budget);
let written = serde_json::to_string(&map).expect("serialises");
assert!(
!written.contains("longblob") && !written.contains("vector(1536)"),
"the map carries the full type of a large column: {written}"
);
let described = mariadb_cli::types::binary::describe(
&[0xFFu8; 4096],
&mariadb_cli::types::Conversion::stable().binary,
)
.expect("a description is always available");
assert_eq!(described["bytes"], 4096);
assert!(
described.get("content").is_none(),
"the content left without being asked for: {described}"
);
}
fn a_database_of_many_tables(count: usize) -> (Vec<Row>, Vec<Row>, Vec<Row>) {
let mut tables = Vec::new();
let mut columns = Vec::new();
for index in 0..count {
let name = format!("t{index}");
let mut table = Row::new();
table.insert("name".to_owned(), json!(name));
table.insert("database".to_owned(), json!("loja"));
tables.push(table);
columns.push(catalog_column(&name, "id", "bigint(20) unsigned", true));
columns.push(catalog_column(&name, "descricao", "varchar(255)", false));
columns.push(catalog_column(&name, "criado_em", "datetime(6)", false));
}
(tables, columns, Vec::new())
}
fn catalog_column(table: &str, name: &str, declared: &str, key: bool) -> Row {
let mut row = Row::new();
row.insert("table".to_owned(), json!(table));
row.insert("database".to_owned(), json!("loja"));
row.insert("name".to_owned(), json!(name));
row.insert("type".to_owned(), json!(declared));
row.insert(
"base_type".to_owned(),
json!(declared.split('(').next().unwrap_or(declared)),
);
row.insert("nullable".to_owned(), json!(!key));
row.insert("default".to_owned(), Value::Null);
row.insert("comment".to_owned(), json!(""));
row.insert("key".to_owned(), json!(if key { "PRI" } else { "" }));
row
}