#[allow(dead_code)]
mod support;
use serde_json::Value;
use support::server::{destination_or_skip, on_server, unique_mark};
use support::{Outcome, Sandbox};
const FIXTURE_DATABASE: &str = "shop";
const TABLE_COLLATION: &str = "utf8mb4_general_ci";
const COLUMN_COLLATION: &str = "utf8mb4_bin";
struct Fixture<'a> {
sandbox: &'a Sandbox,
dsn: String,
name: String,
}
impl<'a> Fixture<'a> {
fn build(sandbox: &'a Sandbox, dsn: &str) -> Self {
let made = Self {
sandbox,
dsn: dsn.to_owned(),
name: format!("structure_probe_{}", unique_mark()),
};
let statement = format!(
"CREATE TABLE {name} (\
id int NOT NULL AUTO_INCREMENT, \
tag varchar(20) COLLATE {COLUMN_COLLATION} NOT NULL, \
hidden_col int INVISIBLE, \
PRIMARY KEY (id)\
) CHARACTER SET utf8mb4 COLLATE {TABLE_COLLATION}",
name = made.name
);
let built = made.statement(&statement);
assert_eq!(built.code, 0, "the fixture must be built: {}", built.err);
made
}
fn statement(&self, written: &str) -> Outcome {
on_server(self.sandbox, &self.dsn, &["--yes", "query", "run", written])
}
fn read(&self, command: &[&str]) -> Outcome {
let mut whole: Vec<&str> = command.to_vec();
whole.extend_from_slice(&["--database", FIXTURE_DATABASE]);
on_server(self.sandbox, &self.dsn, &whole)
}
}
impl Drop for Fixture<'_> {
fn drop(&mut self) {
let removed = self.statement(&format!("DROP TABLE IF EXISTS {}", self.name));
support::server::confirm_removed(&removed, &format!("the table {}", self.name));
}
}
#[test]
fn the_definition_the_server_emits_recreates_the_object_it_describes() {
let Some(dsn) = destination_or_skip("the textual definition of an object") else {
return;
};
let sandbox = Sandbox::new();
let fixture = Fixture::build(&sandbox, &dsn);
let before = fixture.read(&["schema", "columns", "--table", &fixture.name]);
assert_eq!(before.code, 0, "the description: {}", before.err);
let definition = fixture.read(&["schema", "ddl", &fixture.name]);
assert_eq!(definition.code, 0, "the definition: {}", definition.err);
let written = definition.answer()["data"]["definition"]
.as_str()
.unwrap_or_else(|| panic!("no definition was returned: {}", definition.out))
.to_owned();
assert!(
written.to_uppercase().contains("CREATE TABLE"),
"the definition must be the statement that builds it: {written}"
);
let removed = fixture.statement(&format!("DROP TABLE {}", fixture.name));
assert_eq!(
removed.code, 0,
"the table must be removable: {}",
removed.err
);
let rebuilt = fixture.statement(&written);
assert_eq!(
rebuilt.code, 0,
"the definition the server emitted was refused by the server: {}",
rebuilt.err
);
let after = fixture.read(&["schema", "columns", "--table", &fixture.name]);
assert_eq!(
after.code, 0,
"the description of the rebuilt table: {}",
after.err
);
assert_eq!(
before.rows(),
after.rows(),
"the object was rebuilt from its own definition and came back different"
);
}
#[test]
fn a_sample_never_returns_more_rows_than_were_asked_for() {
let Some(dsn) = destination_or_skip("the sample of a table") else {
return;
};
let sandbox = Sandbox::new();
let counted = on_server(
&sandbox,
&dsn,
&[
"schema",
"count",
"--database",
FIXTURE_DATABASE,
"--table",
"orders",
],
);
assert_eq!(counted.code, 0, "the count: {}", counted.err);
let held = counted.answer()["data"]["rows"]
.as_u64()
.unwrap_or_else(|| panic!("the count carries no number: {}", counted.out));
assert!(
held >= 2,
"this table holds {held} rows and cannot fill a sample"
);
for asked in ["1", "2"] {
let sampled = on_server(
&sandbox,
&dsn,
&[
"query",
"sample",
"--database",
FIXTURE_DATABASE,
"--table",
"orders",
"--limit",
asked,
],
);
assert_eq!(sampled.code, 0, "the sample of {asked}: {}", sampled.err);
let answer = sampled.answer();
let returned = answer["data"]["rows"]
.as_array()
.unwrap_or_else(|| panic!("the sample carries no rows: {answer}"))
.len();
let ceiling: usize = asked.parse().expect("a written number");
assert!(
returned <= ceiling,
"a sample of {ceiling} returned {returned} rows"
);
assert_eq!(
answer["data"]["row_count"], returned,
"the declared count and the rows themselves disagree: {answer}"
);
}
}
#[test]
fn the_count_that_reads_the_rows_declares_itself_exact() {
let Some(dsn) = destination_or_skip("the exact count") else {
return;
};
let sandbox = Sandbox::new();
let counted = on_server(
&sandbox,
&dsn,
&[
"schema",
"count",
"--database",
FIXTURE_DATABASE,
"--table",
"orders",
],
);
assert_eq!(counted.code, 0, "the count must be served: {}", counted.err);
let answer = counted.answer();
assert_eq!(
answer["data"]["exact"],
Value::Bool(true),
"a count that read the rows must say so: {answer}"
);
assert_eq!(
answer["data"]["estimated"],
Value::Bool(false),
"an exact count must never also be published as an estimate: {answer}"
);
assert!(
answer["data"]["rows"].is_number(),
"the count itself must be a number: {answer}"
);
}
#[test]
fn a_column_that_overrides_the_character_rules_reads_back_as_different() {
let Some(dsn) = destination_or_skip("the character rules of an object") else {
return;
};
let sandbox = Sandbox::new();
let fixture = Fixture::build(&sandbox, &dsn);
let databases = on_server(&sandbox, &dsn, &["schema", "databases"]);
assert_eq!(databases.code, 0, "the databases: {}", databases.err);
let database = databases
.rows()
.into_iter()
.find(|row| row["name"] == FIXTURE_DATABASE)
.unwrap_or_else(|| panic!("the fixture's database is missing: {}", databases.out));
assert!(
database["character_set"].is_string() && database["collation"].is_string(),
"a database must declare its character rules: {database}"
);
let tables = fixture.read(&["schema", "tables", "--detail", "full"]);
assert_eq!(tables.code, 0, "the tables: {}", tables.err);
let table = tables
.rows()
.into_iter()
.find(|row| row["name"] == fixture.name.as_str())
.unwrap_or_else(|| panic!("the fixture is missing from the listing: {}", tables.out));
assert_eq!(
table["collation"], TABLE_COLLATION,
"the table's own rules were not read: {table}"
);
let columns = fixture.read(&["schema", "columns", "--table", &fixture.name]);
assert_eq!(columns.code, 0, "the columns: {}", columns.err);
let overriding = columns
.rows()
.into_iter()
.find(|row| row["name"] == "tag")
.unwrap_or_else(|| panic!("the overriding column is missing: {}", columns.out));
assert_eq!(
overriding["collation"], COLUMN_COLLATION,
"the column's own rules were not read: {overriding}"
);
assert_ne!(
overriding["collation"], table["collation"],
"a column that overrides its table came back agreeing with it"
);
}
#[test]
fn a_hidden_column_is_listed_and_is_never_returned_by_reading_them_all() {
let Some(dsn) = destination_or_skip("the marks a column carries") else {
return;
};
let sandbox = Sandbox::new();
let fixture = Fixture::build(&sandbox, &dsn);
let columns = fixture.read(&["schema", "columns", "--table", &fixture.name]);
assert_eq!(columns.code, 0, "the columns: {}", columns.err);
let rows = columns.rows();
let hidden = rows
.iter()
.find(|row| row["name"] == "hidden_col")
.unwrap_or_else(|| panic!("the hidden column was not listed at all: {rows:?}"));
assert_eq!(
hidden["invisible"], 1,
"the hidden column is not marked: {hidden}"
);
let automatic = rows
.iter()
.find(|row| row["name"] == "id")
.unwrap_or_else(|| panic!("the automatic column is missing: {rows:?}"));
assert_eq!(
automatic["server_filled"], 1,
"a column the server fills must say so: {automatic}"
);
assert_eq!(
automatic["invisible"], 0,
"being filled by the server is not being hidden: {automatic}"
);
let read = on_server(
&sandbox,
&dsn,
&[
"query",
"run",
&format!("SELECT * FROM {}.{}", FIXTURE_DATABASE, fixture.name),
],
);
assert_eq!(read.code, 0, "reading every column: {}", read.err);
let returned: Vec<String> = read.answer()["data"]["columns"]
.as_array()
.unwrap_or_else(|| panic!("the reading names no columns: {}", read.out))
.iter()
.filter_map(|name| name.as_str().map(ToOwned::to_owned))
.collect();
assert!(
returned.iter().any(|name| name == "tag"),
"the ordinary columns must still arrive: {returned:?}"
);
assert!(
!returned.iter().any(|name| name == "hidden_col"),
"the hidden column was returned by reading them all: {returned:?}"
);
}
#[test]
fn the_restriction_a_table_imposes_comes_back_with_its_condition() {
let Some(dsn) = destination_or_skip("the value restrictions of a table") else {
return;
};
let sandbox = Sandbox::new();
let listed = on_server(
&sandbox,
&dsn,
&[
"schema",
"checks",
"--database",
FIXTURE_DATABASE,
"--table",
"strict_rows",
],
);
assert_eq!(
listed.code, 0,
"the restrictions must be served: {}",
listed.err
);
let rows = listed.rows();
assert!(
!rows.is_empty(),
"this table declares a restriction and none came back"
);
let condition = rows[0]["condition"]
.as_str()
.unwrap_or_else(|| panic!("the restriction carries no condition: {}", listed.out));
assert!(
condition.contains("amount"),
"the condition must name what it guards: {condition}"
);
assert!(
rows[0]["level"].is_string(),
"the level says where the restriction was written: {rows:?}"
);
let none = on_server(
&sandbox,
&dsn,
&[
"schema",
"checks",
"--database",
FIXTURE_DATABASE,
"--table",
"orders",
],
);
assert_eq!(
none.code, 0,
"no restriction is not a failure: {}",
none.err
);
assert!(
none.rows().is_empty(),
"a table imposing nothing answered with something: {:?}",
none.rows()
);
}