mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
Documentation
//! How much a listing returns, and how a caller asks for more.
//!
//! # Why these proofs sit apart from their neighbours
//!
//! `coverage_structure_catalog.rs` proves that each listing tells the truth
//! about the objects it names. These prove something else about the same
//! commands: how wide the answer is, and whether the caller has any way of
//! changing that. The two questions were together and the file outgrew the
//! ceiling, so they are split along the seam the file's own opening declares.
//!
//! The depth used to be a truth value the manual never published, reachable on
//! six listings and on no other, while three listings carried extras in the
//! catalogue that no argument could ask for. What is proved here is the level
//! the manual does publish, on every listing that has one.

// Each file here uses the part of the shared module that it needs, so a helper
// unused by this one is not dead code.
#[allow(dead_code)]
mod support;

use support::server::{destination_or_skip, on_server};
use support::Sandbox;

/// The database every fixture of this file lives in.
const FIXTURE_DATABASE: &str = "shop";

/// FR-019. The search reaches a column and a database as well as a table, and
/// the detailed form of a match carries more than the short one.
///
/// The argv written here is the one the manual publishes: the pattern as
/// `--like` and the kind as `--type`. The build served a bare word and `--kind`
/// while the manual said otherwise, so a reader who copied the manual was
/// refused. The older spelling is kept as an alias and is exercised below, so
/// neither reader is left out.
///
/// # Why the reach is asserted and not only the pattern
///
/// Half of what a caller half-remembers is a column name, and a search that
/// silently looks at tables only answers an empty list for every one of those.
/// An empty list is indistinguishable from an absent object, so a kind left out
/// of the sweep is a wrong answer wearing the shape of a right one.
#[test]
fn the_search_reaches_a_column_and_a_database_and_deepens_on_request() {
    let Some(dsn) = destination_or_skip("the reach of the search by name") else {
        return;
    };
    let sandbox = Sandbox::new();

    let column = on_server(
        &sandbox,
        &dsn,
        &[
            "schema",
            "search",
            "--database",
            FIXTURE_DATABASE,
            "--type",
            "column",
            "--like",
            "amount%",
        ],
    );
    assert_eq!(column.code, 0, "the search over columns: {}", column.err);
    let short = column.rows();
    assert!(
        short.iter().any(|row| row["kind"] == "column"),
        "no column was reached by the search: {short:?}"
    );

    let database = on_server(
        &sandbox,
        &dsn,
        &["schema", "search", "--kind", "database", FIXTURE_DATABASE],
    );
    assert_eq!(
        database.code, 0,
        "the search over databases: {}",
        database.err
    );
    assert!(
        database
            .rows()
            .iter()
            .any(|row| row["name"] == FIXTURE_DATABASE),
        "the database the fixtures live in was not reached: {:?}",
        database.rows()
    );

    let deep = on_server(
        &sandbox,
        &dsn,
        &[
            "schema",
            "search",
            "--database",
            FIXTURE_DATABASE,
            "--type",
            "column",
            "--detail",
            "full",
            "--like",
            "amount%",
        ],
    );
    assert_eq!(deep.code, 0, "the detailed search: {}", deep.err);
    let deep_rows = deep.rows();
    assert!(
        !deep_rows.is_empty(),
        "the detailed search found nothing to compare"
    );
    let widened = deep_rows[0]
        .as_object()
        .expect("a match is an object")
        .len();
    let narrow = short[0].as_object().expect("a match is an object").len();
    assert!(
        widened > narrow,
        "asking for more returned the same {narrow} fields: {deep_rows:?}"
    );
}

/// FR-015 and FR-091. The depth a caller asks for reaches the listings that had
/// no way of being asked, and a level outside the published domain is refused
/// rather than read as the default.
///
/// # Why this is worth a proof of its own
///
/// The catalogue entry for an index has always carried its method, its
/// cardinality, its prefix length and its comment. Nothing published a way of
/// asking for them, so the projection existed, was correct, and was unreachable
/// from the command line — which is indistinguishable, from outside, from not
/// existing at all.
#[test]
fn the_depth_reaches_the_listings_that_could_not_be_asked() {
    let Some(dsn) = destination_or_skip("the depth of the index and link listings") else {
        return;
    };
    let sandbox = Sandbox::new();

    let short = on_server(
        &sandbox,
        &dsn,
        &[
            "schema",
            "indexes",
            "--database",
            FIXTURE_DATABASE,
            "--table",
            "orders",
        ],
    );
    assert_eq!(short.code, 0, "the short listing: {}", short.err);
    let deep = on_server(
        &sandbox,
        &dsn,
        &[
            "schema",
            "indexes",
            "--database",
            FIXTURE_DATABASE,
            "--table",
            "orders",
            "--detail",
            "full",
        ],
    );
    assert_eq!(deep.code, 0, "the deep listing: {}", deep.err);

    let narrow = short.rows();
    let widened = deep.rows();
    assert!(!narrow.is_empty(), "this table carries no index to deepen");
    assert_eq!(
        narrow.len(),
        widened.len(),
        "asking for depth changed which indexes exist"
    );
    let narrow_fields = narrow[0].as_object().expect("a row is an object");
    let widened_fields = widened[0].as_object().expect("a row is an object");
    assert!(
        widened_fields.len() > narrow_fields.len(),
        "asking for more returned the same fields: {widened_fields:?}"
    );
    for name in narrow_fields.keys() {
        assert!(
            widened_fields.contains_key(name),
            "the deeper form dropped `{name}`, so asking for more returned less"
        );
    }

    // The two listings that carry no fixture here are asserted to accept the
    // level at all, which is the half that was missing from them.
    for listing in ["keys", "partitions"] {
        let taken = on_server(
            &sandbox,
            &dsn,
            &[
                "schema",
                listing,
                "--database",
                FIXTURE_DATABASE,
                "--table",
                "orders",
                "--detail",
                "full",
            ],
        );
        assert_eq!(
            taken.code, 0,
            "`schema {listing}` must accept a level: {}",
            taken.err
        );
    }

    let refused = on_server(
        &sandbox,
        &dsn,
        &[
            "schema",
            "indexes",
            "--database",
            FIXTURE_DATABASE,
            "--detail",
            "inexistente",
        ],
    );
    assert_eq!(
        refused.code, 2,
        "a level outside the domain must be refused: {}",
        refused.out
    );
}