#[allow(dead_code)]
mod support;
use support::server::{destination_or_skip, on_server};
use support::Sandbox;
const ROUTINE: &str = "count_orders";
fn orders_reaching(sandbox: &Sandbox, dsn: &str, floor: &str) -> i64 {
let counted = on_server(
sandbox,
dsn,
&[
"query",
"run",
&format!("SELECT COUNT(*) AS reaching FROM orders WHERE total >= {floor}"),
],
);
assert_eq!(counted.code, 0, "the count must be served: {}", counted.err);
counted.rows()[0]["reaching"].as_i64().expect("a count")
}
#[test]
fn a_call_is_treated_as_a_change_and_an_unconfirmed_one_is_never_sent() {
let Some(dsn) = destination_or_skip("refusing an unconfirmed call to a routine") else {
return;
};
let sandbox = Sandbox::new();
let refused = on_server(
&sandbox,
&dsn,
&["query", "call", ROUTINE, "--param", "0", "--param", "0"],
);
assert_eq!(
refused.code, 3,
"an unconfirmed call must be refused: {} {}",
refused.out, refused.err
);
assert_eq!(refused.code(), "safety.unconfirmed");
assert!(
refused.out.trim().is_empty(),
"a refused call left something on the channel that carries data: {}",
refused.out
);
}
#[test]
fn a_call_answers_with_its_result_sets_each_one_kept_apart() {
let Some(dsn) = destination_or_skip("calling a routine and reading its result sets") else {
return;
};
let sandbox = Sandbox::new();
let listed = on_server(&sandbox, &dsn, &["schema", "routines"]);
assert_eq!(
listed.code, 0,
"the routines must be listed: {}",
listed.err
);
assert!(
listed
.rows()
.iter()
.any(|row| row["name"].as_str() == Some(ROUTINE)),
"the declared destination does not carry the routine `{ROUTINE}` this proof calls"
);
let called = on_server(
&sandbox,
&dsn,
&[
"--yes", "query", "call", ROUTINE, "--param", "0", "--param", "0",
],
);
assert_eq!(called.code, 0, "the call must be served: {}", called.err);
let data = called.answer()["data"].clone();
assert_eq!(data["routine"], ROUTINE, "the answer names another routine");
let sets = data["sets"].as_array().expect("the sets, in order").clone();
assert_eq!(
data["set_count"],
serde_json::json!(sets.len()),
"the number of sets reported is not the number of sets carried: {data}"
);
assert!(
!sets.is_empty(),
"the call answered with nothing at all: {data}"
);
for (position, set) in sets.iter().enumerate() {
assert_eq!(
set["set"],
serde_json::json!(position + 1),
"a set is not in the position it claims: {set}"
);
assert!(
set["columns"]
.as_array()
.is_some_and(|held| !held.is_empty()),
"a set arrived with no columns of its own: {set}"
);
}
let missing = on_server(
&sandbox,
&dsn,
&[
"--yes",
"query",
"call",
"rotina_que_ninguem_guardou",
"--param",
"1",
],
);
assert_eq!(
missing.code, 8,
"calling a routine nobody stored must be reported as an absence: {}",
missing.out
);
assert_eq!(missing.code(), "absence.unknown_object");
let mismatched = on_server(
&sandbox,
&dsn,
&["--yes", "query", "call", ROUTINE, "--param", "0"],
);
assert_ne!(
mismatched.code, 0,
"a call whose values do not match the signature must not succeed: {}",
mismatched.out
);
assert!(
mismatched.failure()["error"]["message"]
.as_str()
.is_some_and(|text| text.contains("arguments")),
"the refusal does not say the number of values was wrong: {}",
mismatched.failure()
);
}
#[test]
fn a_routine_that_answers_only_through_an_output_value_still_answers() {
let Some(dsn) = destination_or_skip("reading the value a routine returns by parameter") else {
return;
};
let sandbox = Sandbox::new();
let answered = |floor: &str| {
let called = on_server(
&sandbox,
&dsn,
&[
"--yes", "query", "call", ROUTINE, "--param", floor, "--param", "0",
],
);
assert_eq!(called.code, 0, "the call must be served: {}", called.err);
let sets = called.answer()["data"]["sets"].clone();
let held = sets
.as_array()
.and_then(|sets| sets.first().cloned())
.unwrap_or_else(|| panic!("the call returned no value at all: {}", called.answer()));
held["rows"][0]["how_many"]
.as_i64()
.unwrap_or_else(|| panic!("the returned value is not a number: {held}"))
};
let reachable = orders_reaching(&sandbox, &dsn, "0");
assert!(
reachable > 0,
"the destination carries no orders, so this proof cannot tell a real answer \
from a zero"
);
assert_eq!(
answered("0"),
reachable,
"the value returned by parameter does not agree with the direct count"
);
let unreachable = orders_reaching(&sandbox, &dsn, "99999999");
assert_eq!(
answered("99999999"),
unreachable,
"the value returned by parameter did not follow the argument it was given"
);
assert_ne!(
reachable, unreachable,
"both floors give the same count, so the value could be ignoring the argument"
);
}