#[allow(dead_code)]
mod support;
use serde_json::json;
use support::server::{
aimed_at, confirm_removed, destination_or_skip, on_server, own_identifier_base, unique_mark,
};
use support::Sandbox;
struct Built<'a> {
sandbox: &'a Sandbox,
dsn: String,
table: String,
twin: String,
}
impl Drop for Built<'_> {
fn drop(&mut self) {
for (statement, what) in [
(
format!("DROP DATABASE IF EXISTS {}", self.twin),
format!("the database {}", self.twin),
),
(
format!("DROP TABLE IF EXISTS shop.{}", self.table),
format!("the table shop.{}", self.table),
),
] {
let removed = on_server(
self.sandbox,
&self.dsn,
&["--yes", "query", "run", &statement],
);
confirm_removed(&removed, &what);
}
}
}
fn seeded_source(sandbox: &Sandbox, dsn: &str, scratch: &std::path::Path, table: &str) {
let built = on_server(
sandbox,
dsn,
&[
"--yes",
"query",
"run",
&format!(
"CREATE TABLE shop.{table} (id INT NOT NULL PRIMARY KEY, \
customer VARCHAR(80) NOT NULL, total DECIMAL(10,2) NOT NULL, \
placed_at DATETIME NOT NULL, note TEXT NULL)"
),
],
);
assert_eq!(built.code, 0, "the source must be created: {}", built.err);
let seed = scratch.join("semente.csv");
std::fs::write(
&seed,
"id,customer,total,placed_at,note\n\
1,ana,10.50,2026-01-01 10:00:00,com nota\n\
2,bea,0.00,2026-01-02 11:30:00,sem nota\n\
3,cid,999999.99,2026-01-03 12:45:00,acentuação e cedilha\n\
4,dan,-7.25,2026-01-04 23:59:59,fim\n",
)
.expect("the seed is written");
let filled = on_server(
sandbox,
dsn,
&[
"--yes",
"data",
"import",
"--file",
seed.to_str().expect("a utf8 path"),
"--table",
table,
"--header",
],
);
assert_eq!(filled.code, 0, "the source must be filled: {}", filled.err);
assert_eq!(
filled.answer()["data"]["rows_written"],
json!(4),
"every seeded row must be kept: {}",
filled.out
);
}
#[test]
fn rows_written_out_match_the_rows_read_back() {
let Some(dsn) = destination_or_skip("writing a table out and loading it back") else {
return;
};
let sandbox = Sandbox::new();
let scratch = tempfile::tempdir().expect("a scratch directory");
let copy = scratch.path().join("copia.sql");
let copy_path = copy.to_str().expect("a utf8 path").to_owned();
let mark = unique_mark();
let table = format!("round_trip_{mark}");
let twin = format!("shop_twin_{mark}");
let _built = Built {
sandbox: &sandbox,
dsn: dsn.clone(),
table: table.clone(),
twin: twin.clone(),
};
seeded_source(&sandbox, &dsn, scratch.path(), &table);
let source = on_server(
&sandbox,
&dsn,
&[
"query",
"run",
&format!("SELECT * FROM shop.{table} ORDER BY id"),
],
);
assert_eq!(
source.code, 0,
"the source must be readable: {}",
source.err
);
let original = source.rows();
assert_eq!(
original.len(),
4,
"the source must hold every row it was given"
);
let written = on_server(
&sandbox,
&dsn,
&["data", "export", "--table", &table, "--to", ©_path],
);
assert_eq!(written.code, 0, "the copy must be written: {}", written.err);
assert_eq!(
written.answer()["data"]["rows"],
json!(original.len()),
"the copy declares a different number of rows than the table holds"
);
let made = on_server(
&sandbox,
&dsn,
&["--yes", "query", "run", &format!("CREATE DATABASE {twin}")],
);
assert_eq!(made.code, 0, "the empty twin must be created: {}", made.err);
let inside_twin = aimed_at(&dsn, &twin);
let loaded = on_server(
&sandbox,
&inside_twin,
&["--yes", "query", "file", ©_path],
);
let read_back = on_server(
&sandbox,
&inside_twin,
&[
"query",
"run",
&format!("SELECT * FROM {twin}.{table} ORDER BY id"),
],
);
assert_eq!(loaded.code, 0, "the copy must load: {}", loaded.err);
assert_eq!(
read_back.code, 0,
"the twin must be readable: {}",
read_back.err
);
let kept = read_back.rows();
assert_eq!(
kept.len(),
original.len(),
"the twin holds a different number of rows"
);
assert_eq!(
kept, original,
"a row changed between going out and coming back"
);
}
#[test]
fn a_load_into_a_missing_table_is_refused_the_way_a_read_of_one_is() {
let Some(dsn) = destination_or_skip("refusing a load into a table that is absent") else {
return;
};
let sandbox = Sandbox::new();
let scratch = tempfile::tempdir().expect("a scratch directory");
let file = scratch.path().join("linhas.csv");
std::fs::write(&file, "id\n1\n2\n").expect("the file is written");
let absent = format!("nao_existe_{}", own_identifier_base());
let loaded = on_server(
&sandbox,
&dsn,
&[
"--yes",
"data",
"import",
"--file",
file.to_str().expect("a utf8 path"),
"--table",
&absent,
"--header",
"--on-error",
"skip",
],
);
assert_eq!(
loaded.code, 8,
"a load into a table that is absent is the absence class and ends in 8; \
anything else means this proof never reached the condition it exists \
for: {}",
loaded.out
);
assert_eq!(
loaded.failure()["error"]["category"],
"absence",
"the load must fail because the table is not there, and not because \
something else went wrong on the way: {}",
loaded.out
);
let causes = loaded.failure()["error"]["causes"].to_string();
assert!(
causes.contains(&absent),
"the refusal must name the absent table, which is what the condition \
for a missing table carries; a refusal that never names it came from \
somewhere earlier than the load, and the row policy was never reached: \
{causes}"
);
let read = on_server(
&sandbox,
&dsn,
&[
"data",
"export",
"--to",
scratch
.path()
.join("saida.tsv")
.to_str()
.expect("a utf8 path"),
"--table",
&absent,
],
);
assert_eq!(
loaded.code, read.code,
"loading into a missing table and reading from one are the same \
condition, so the two commands must end the same way; load gave {} \
and read gave {}",
loaded.code, read.code
);
assert_eq!(
loaded.failure()["error"]["category"],
read.failure()["error"]["category"],
"the prose here promises the two answer the same thing, and the number \
alone is the part that was already fixed; the class has to agree too"
);
}