pgdumpx 0.2.0

Read-only, bounded inspection, extraction, and row scanning for PostgreSQL custom-format dumps
Documentation
use crate::{Archive, FieldRef, OwnedField};
use std::{fs::File, io::BufReader, path::PathBuf};

#[test]
fn integrated_none_and_gzip_paths_preserve_parser_consumed_byte_accounting() {
    let first_physical_row = b"1\tEARLY-100\tcustomer-a\tplain\t\n";

    for fixture_name in ["pg18-none-copy-basic.dump", "pg18-gzip-copy-basic.dump"] {
        let file = File::open(fixture_path(fixture_name)).unwrap();
        let mut archive = Archive::open(BufReader::new(file)).unwrap();
        let mut rows = archive.table_rows(b"public", b"orders").unwrap();

        {
            let row = rows.next_row().unwrap().unwrap();
            assert_eq!(row.field(1), Some(FieldRef::Bytes(b"EARLY-100")));
        }
        assert_eq!(
            rows.consumed_input_bytes(),
            u64::try_from(first_physical_row.len()).unwrap(),
            "decoder or BufRead read-ahead must not affect parser-consumed accounting for {fixture_name}"
        );
    }
}

fn fixture_path(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../tests/fixtures/archives")
        .join(name)
}
#[test]
fn find_first_preserves_parser_consumed_byte_accounting_and_stops_at_match() {
    let first_physical_row = b"1\tEARLY-100\tcustomer-a\tplain\t\n";

    for fixture_name in ["pg18-none-copy-basic.dump", "pg18-gzip-copy-basic.dump"] {
        let file = File::open(fixture_path(fixture_name)).unwrap();
        let mut archive = Archive::open(BufReader::new(file)).unwrap();
        let mut rows = archive.table_rows(b"public", b"orders").unwrap();
        let order_number = rows.column_index(b"order_number").unwrap().unwrap();

        let found = rows
            .find_first(|row| row.field(order_number) == Some(FieldRef::Bytes(b"EARLY-100")))
            .unwrap()
            .unwrap();
        assert_eq!(found.field(0), Some(&OwnedField::Bytes(b"1".to_vec())));
        assert_eq!(
            rows.consumed_input_bytes(),
            u64::try_from(first_physical_row.len()).unwrap(),
            "decoder or BufRead read-ahead must not affect find_first accounting for {fixture_name}"
        );
    }
}