#[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 WRITTEN_COMMENT: &str = "uma nota escrita de proposito";
const LISTINGS_WITH_A_COMMENT: &[&str] = &[
"schema tables",
"schema views",
"schema indexes",
"schema routines",
];
struct Fixture<'a> {
sandbox: &'a Sandbox,
dsn: String,
undo: Vec<String>,
}
impl<'a> Fixture<'a> {
fn new(sandbox: &'a Sandbox, dsn: &str) -> Self {
Self {
sandbox,
dsn: dsn.to_owned(),
undo: Vec::new(),
}
}
fn statement(&self, written: &str) -> Outcome {
on_server(self.sandbox, &self.dsn, &["--yes", "query", "run", written])
}
fn fill(&self, written: &str) {
let done = on_server(
self.sandbox,
&self.dsn,
&["--yes", "query", "tx", "--statement", written],
);
assert_eq!(done.code, 0, "writing rows: {}", done.err);
}
fn build(&mut self, statement: &str, undo: String) {
let built = self.statement(statement);
assert_eq!(built.code, 0, "the fixture must be built: {}", built.err);
self.undo.push(undo);
}
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) {
for undo in self.undo.iter().rev() {
let removed = self.statement(undo);
support::server::confirm_removed(&removed, undo);
}
}
}
fn row_named<'a>(rows: &'a [Value], name: &str) -> &'a Value {
rows.iter()
.find(|row| row["name"] == name)
.unwrap_or_else(|| panic!("the listing carries no `{name}`"))
}
#[test]
fn fr_122_a_table_without_a_comment_is_told_apart_from_one_with_an_empty_comment() {
let Some(dsn) = destination_or_skip("the mark of absence on a comment") else {
return;
};
let sandbox = Sandbox::new();
let mut fixture = Fixture::new(&sandbox, &dsn);
let silent = format!("comment_silent_{}", unique_mark());
let speaking = format!("comment_speaking_{}", unique_mark());
fixture.build(
&format!("CREATE TABLE {silent} (n int)"),
format!("DROP TABLE IF EXISTS {silent}"),
);
fixture.build(
&format!("CREATE TABLE {speaking} (n int) COMMENT '{WRITTEN_COMMENT}'"),
format!("DROP TABLE IF EXISTS {speaking}"),
);
let listed = fixture.read(&["schema", "tables", "--detail", "full"]);
assert_eq!(listed.code, 0, "the listing must be served: {}", listed.err);
let rows = listed.rows();
let without = &row_named(&rows, &silent)["comment"];
assert!(
without.is_null(),
"a table with no comment must carry the mark of absence, and carried {without}"
);
assert_ne!(
without,
&Value::from(""),
"empty text is never the mark of absence"
);
assert_eq!(
row_named(&rows, &speaking)["comment"],
Value::from(WRITTEN_COMMENT),
"a comment somebody wrote must arrive as the text they wrote"
);
}
#[test]
fn fr_122_every_listing_the_requirement_names_marks_an_absent_comment() {
let Some(dsn) = destination_or_skip("the mark of absence across the four listings") else {
return;
};
let sandbox = Sandbox::new();
let fixture = Fixture::new(&sandbox, &dsn);
for command in LISTINGS_WITH_A_COMMENT {
let contract = sandbox.run(&["meta", "schema", "--command", command]);
assert_eq!(
contract.code, 0,
"the contract of `{command}`: {}",
contract.err
);
let marked = contract.answer()["data"]["absent_when_missing"].clone();
assert_eq!(
marked,
Value::from(vec!["comment"]),
"`{command}` must publish that its comment carries the mark of absence"
);
let words: Vec<&str> = command.split(' ').collect();
let mut asked: Vec<&str> = words.clone();
asked.extend_from_slice(&["--detail", "full"]);
let listed = fixture.read(&asked);
assert_eq!(listed.code, 0, "`{command}` must be served: {}", listed.err);
let rows = listed.rows();
assert!(!rows.is_empty(), "`{command}` returned nothing to read");
for row in &rows {
let comment = &row["comment"];
assert_ne!(
comment,
&Value::from(""),
"`{command}` handed back empty text where the mark of absence belongs: {row}"
);
assert!(
comment.is_null() || comment.is_string(),
"`{command}` published a comment that is neither text nor the mark: {row}"
);
}
}
}
const SERVER_ONLY: &[(&str, &str, &str)] = &[
("uuid", "u", "123e4567-e89b-12d3-a456-426614174000"),
("inet4", "a", "192.0.2.7"),
("inet6", "b", "2001:0db8:0000:0000:0000:0000:0000:0001"),
];
#[test]
fn fr_130_the_published_map_names_every_server_only_family() {
let sandbox = Sandbox::new();
let answer = sandbox.run(&["meta", "schema", "--types"]).answer();
let rows = answer["data"]["rows"]
.as_array()
.unwrap_or_else(|| panic!("the map carries no rows: {answer}"))
.clone();
for (family, _, _) in SERVER_ONLY {
let row = rows
.iter()
.find(|row| row["family"] == *family)
.unwrap_or_else(|| panic!("the map publishes no family `{family}`"));
assert_eq!(
row["emitted"], "text",
"`{family}` is rendered by the server already, so it leaves as text"
);
assert!(
row["reason"].as_str().is_some_and(|note| !note.is_empty()),
"`{family}` must say why it takes the form it takes"
);
}
let notes = answer["data"]["notes"]
.as_array()
.unwrap_or_else(|| panic!("the map carries no notes: {answer}"))
.iter()
.filter_map(|note| note.as_str())
.collect::<Vec<_>>()
.join(" ");
for stated in ["catalogue declares", "validates", "guessing"] {
assert!(
notes.contains(stated),
"the map never states `{stated}`: {notes}"
);
}
}
#[test]
fn fr_130_a_value_of_each_server_only_family_takes_the_published_form() {
let Some(dsn) = destination_or_skip("a value of each server-only family") else {
return;
};
let sandbox = Sandbox::new();
let mut fixture = Fixture::new(&sandbox, &dsn);
let table = format!("server_only_{}", unique_mark());
fixture.build(
&format!("CREATE TABLE {table} (u uuid, a inet4, b inet6)"),
format!("DROP TABLE IF EXISTS {table}"),
);
let written: Vec<&str> = SERVER_ONLY.iter().map(|(_, _, value)| *value).collect();
fixture.fill(&format!(
"INSERT INTO {table} VALUES ('{}', '{}', '{}')",
written[0], written[1], written[2]
));
let described = fixture.read(&["schema", "columns", "--table", &table]);
assert_eq!(described.code, 0, "the description: {}", described.err);
let described_rows = described.rows();
for (family, column, _) in SERVER_ONLY {
assert_eq!(
row_named(&described_rows, column)["type"],
Value::from(*family),
"the catalogue must declare `{column}` as `{family}`"
);
}
let read = on_server(
&sandbox,
&dsn,
&[
"query",
"run",
&format!("SELECT u, a, b FROM {FIXTURE_DATABASE}.{table}"),
],
);
assert_eq!(read.code, 0, "reading the row back: {}", read.err);
let rows = read.rows();
assert_eq!(rows.len(), 1, "one row was written: {}", read.out);
let row = &rows[0];
for (family, column, _) in SERVER_ONLY {
assert!(
row[*column].is_string(),
"`{family}` is published as text and arrived as {}",
row[*column]
);
}
let identifier = row["u"].as_str().expect("text");
assert!(
is_canonical_identifier(identifier),
"the identifier must be canonical text with its hyphens, and was `{identifier}`"
);
let fourth = row["a"].as_str().expect("text");
assert_eq!(
fourth.parse::<std::net::Ipv4Addr>().ok(),
written[1].parse().ok(),
"the fourth-version address must be four numbers separated by dots"
);
assert_eq!(
fourth.split('.').count(),
4,
"`{fourth}` is not four numbers"
);
let sixth = row["b"].as_str().expect("text");
assert_eq!(
sixth.parse::<std::net::Ipv6Addr>().ok(),
written[2].parse().ok(),
"the sixth-version address must be the address that was written"
);
assert_ne!(
sixth, written[2],
"the sixth-version address must arrive in the server's shortened form"
);
assert!(
sixth.contains("::"),
"the shortened form collapses its run of zeroes, and `{sixth}` did not"
);
}
fn is_canonical_identifier(text: &str) -> bool {
let separators = [8, 13, 18, 23];
text.len() == 36
&& text.char_indices().all(|(at, letter)| {
if separators.contains(&at) {
letter == '-'
} else {
letter.is_ascii_hexdigit()
}
})
}