athena_rs 3.26.2

Hyper performant polyglot Database driver
Documentation
//! Shared `/debug/schema` snapshot-query test helpers.
//!
//! These helpers centralize relation/column row fixtures plus success/error
//! assertions so snapshot-query stage, handoff, and result tests can focus on
//! boundary behavior, while canonical snapshot-error fixtures are delegated to
//! `debug_snapshot_error_test_fixtures`.

use super::debug_snapshot_contracts::LoggingSchemaSnapshotRows;
use super::debug_snapshot_error::LoggingSchemaSnapshotError;
use super::debug_snapshot_error_assertions_test_helpers::{
    assert_fetch_columns_snapshot_error_variant_for_tests,
    assert_fetch_relations_snapshot_error_variant_for_tests,
};
use super::debug_snapshot_error_test_fixtures::{
    build_fetch_columns_snapshot_error_for_tests, build_fetch_relations_snapshot_error_for_tests,
};
use super::debug_snapshot_queries_output_input_contracts::LoggingSchemaSnapshotRowsOutputInput;
use super::debug_snapshot_queries_result_assembly_input_contracts::LoggingSchemaSnapshotRowsResultAssemblyInput;
use super::debug_snapshot_queries_result_output_handoff_input_contracts::LoggingSchemaSnapshotRowsResultOutputHandoffInput;
use super::debug_snapshot_queries_stage_contracts::LoggingSchemaSnapshotRowsStage;
use super::service::{SchemaColumnRecord, SchemaRelationRecord};

/// Builds a canonical relation-row fixture for snapshot-query tests.
#[cfg(test)]
pub(crate) fn build_snapshot_query_relation_row_for_tests() -> SchemaRelationRecord {
    SchemaRelationRecord {
        table_schema: "public".to_string(),
        table_name: "gateway_request_log".to_string(),
        relation_type: "BASE TABLE".to_string(),
    }
}

/// Builds a canonical column-row fixture for snapshot-query tests.
#[cfg(test)]
pub(crate) fn build_snapshot_query_column_row_for_tests() -> SchemaColumnRecord {
    SchemaColumnRecord {
        table_schema: "public".to_string(),
        table_name: "gateway_request_log".to_string(),
        column_name: "request_id".to_string(),
        data_type: Some("uuid".to_string()),
        column_default: None,
        is_nullable: Some("NO".to_string()),
    }
}

/// Builds canonical relation-row collection fixtures for snapshot-query tests.
#[cfg(test)]
pub(crate) fn build_snapshot_query_relations_for_tests() -> Vec<SchemaRelationRecord> {
    vec![build_snapshot_query_relation_row_for_tests()]
}

/// Builds canonical column-row collection fixtures for snapshot-query tests.
#[cfg(test)]
pub(crate) fn build_snapshot_query_columns_for_tests() -> Vec<SchemaColumnRecord> {
    vec![build_snapshot_query_column_row_for_tests()]
}

/// Builds staged snapshot-query payload fixture with successful relation/column query results.
#[cfg(test)]
pub(crate) fn build_successful_snapshot_rows_stage_for_tests() -> LoggingSchemaSnapshotRowsStage {
    LoggingSchemaSnapshotRowsStage {
        relations_result: Ok(build_snapshot_query_relations_for_tests()),
        columns_result: Ok(build_snapshot_query_columns_for_tests()),
    }
}

/// Builds result-assembly input fixture with successful relation/column query results.
#[cfg(test)]
pub(crate) fn build_successful_snapshot_rows_result_input_for_tests()
-> LoggingSchemaSnapshotRowsResultAssemblyInput {
    LoggingSchemaSnapshotRowsResultAssemblyInput {
        relations_result: Ok(build_snapshot_query_relations_for_tests()),
        columns_result: Ok(build_snapshot_query_columns_for_tests()),
    }
}

/// Builds output-assembly input fixture with successful relation/column query rows.
#[cfg(test)]
pub(crate) fn build_successful_snapshot_rows_output_input_for_tests()
-> LoggingSchemaSnapshotRowsOutputInput {
    LoggingSchemaSnapshotRowsOutputInput {
        relations: build_snapshot_query_relations_for_tests(),
        columns: build_snapshot_query_columns_for_tests(),
    }
}

/// Builds result-output handoff input fixture with successful relation/column query rows.
#[cfg(test)]
pub(crate) fn build_successful_snapshot_rows_result_output_handoff_input_for_tests()
-> LoggingSchemaSnapshotRowsResultOutputHandoffInput {
    LoggingSchemaSnapshotRowsResultOutputHandoffInput {
        relations: build_snapshot_query_relations_for_tests(),
        columns: build_snapshot_query_columns_for_tests(),
    }
}

/// Builds staged snapshot-query payload fixture with a `FetchRelations` query failure.
#[cfg(test)]
pub(crate) fn build_relation_error_snapshot_rows_stage_for_tests(
    source_message: &str,
) -> LoggingSchemaSnapshotRowsStage {
    LoggingSchemaSnapshotRowsStage {
        relations_result: Err(build_fetch_relations_snapshot_error_for_tests(
            source_message,
        )),
        columns_result: Ok(Vec::new()),
    }
}

/// Builds staged snapshot-query payload fixture with a `FetchColumns` query failure.
#[cfg(test)]
pub(crate) fn build_column_error_snapshot_rows_stage_for_tests(
    source_message: &str,
) -> LoggingSchemaSnapshotRowsStage {
    LoggingSchemaSnapshotRowsStage {
        relations_result: Ok(Vec::new()),
        columns_result: Err(build_fetch_columns_snapshot_error_for_tests(source_message)),
    }
}

/// Builds staged snapshot-query payload fixture with both relation and column query failures.
#[cfg(test)]
pub(crate) fn build_relation_and_column_error_snapshot_rows_stage_for_tests(
    relation_source_message: &str,
    column_source_message: &str,
) -> LoggingSchemaSnapshotRowsStage {
    LoggingSchemaSnapshotRowsStage {
        relations_result: Err(build_fetch_relations_snapshot_error_for_tests(
            relation_source_message,
        )),
        columns_result: Err(build_fetch_columns_snapshot_error_for_tests(
            column_source_message,
        )),
    }
}

/// Builds result-assembly input fixture with a `FetchRelations` query failure.
#[cfg(test)]
pub(crate) fn build_relation_error_snapshot_rows_result_input_for_tests(
    source_message: &str,
) -> LoggingSchemaSnapshotRowsResultAssemblyInput {
    LoggingSchemaSnapshotRowsResultAssemblyInput {
        relations_result: Err(build_fetch_relations_snapshot_error_for_tests(
            source_message,
        )),
        columns_result: Ok(Vec::new()),
    }
}

/// Builds result-assembly input fixture with a `FetchColumns` query failure.
#[cfg(test)]
pub(crate) fn build_column_error_snapshot_rows_result_input_for_tests(
    source_message: &str,
) -> LoggingSchemaSnapshotRowsResultAssemblyInput {
    LoggingSchemaSnapshotRowsResultAssemblyInput {
        relations_result: Ok(Vec::new()),
        columns_result: Err(build_fetch_columns_snapshot_error_for_tests(source_message)),
    }
}

/// Asserts snapshot rows preserve expected relation/column counts and canonical field values.
#[cfg(test)]
pub(crate) fn assert_snapshot_rows_output_for_tests(
    rows: LoggingSchemaSnapshotRows,
    context: &str,
) {
    assert_eq!(
        rows.relations.len(),
        1,
        "unexpected {context} relation row count"
    );
    assert_eq!(
        rows.columns.len(),
        1,
        "unexpected {context} column row count"
    );
    assert_eq!(
        rows.relations[0].table_name, "gateway_request_log",
        "unexpected {context} relation table name"
    );
    assert_eq!(
        rows.columns[0].column_name, "request_id",
        "unexpected {context} column name"
    );
}

/// Asserts a snapshot-row result is successful and preserves canonical row output.
#[cfg(test)]
pub(crate) fn assert_successful_snapshot_rows_result_for_tests(
    result: Result<LoggingSchemaSnapshotRows, LoggingSchemaSnapshotError>,
    context: &str,
) {
    match result {
        Ok(rows) => assert_snapshot_rows_output_for_tests(rows, context),
        Err(_) => panic!("expected successful snapshot rows result for {context}"),
    }
}

/// Asserts a staged snapshot-query payload preserves successful relation/column query outputs.
#[cfg(test)]
pub(crate) fn assert_successful_snapshot_rows_stage_for_tests(
    stage: LoggingSchemaSnapshotRowsStage,
    context: &str,
) {
    match stage.relations_result {
        Ok(relations) => assert_eq!(
            relations.len(),
            1,
            "unexpected {context} relations result row count"
        ),
        Err(_) => panic!("expected successful relations result for {context}"),
    }
    match stage.columns_result {
        Ok(columns) => assert_eq!(
            columns.len(),
            1,
            "unexpected {context} columns result row count"
        ),
        Err(_) => panic!("expected successful columns result for {context}"),
    }
}

/// Asserts a staged snapshot-query payload preserves `FetchRelations` and `FetchColumns` variants.
#[cfg(test)]
pub(crate) fn assert_relation_and_column_error_snapshot_rows_stage_for_tests(
    stage: LoggingSchemaSnapshotRowsStage,
    relation_source_fragment: Option<&str>,
    column_source_fragment: Option<&str>,
    context: &str,
) {
    assert_fetch_relations_snapshot_error_variant_for_tests(
        stage.relations_result,
        relation_source_fragment,
        context,
    );
    assert_fetch_columns_snapshot_error_variant_for_tests(
        stage.columns_result,
        column_source_fragment,
        context,
    );
}

/// Asserts snapshot-row result maps to the `FetchRelations` variant.
#[cfg(test)]
pub(crate) fn assert_fetch_relations_snapshot_error_for_tests(
    result: Result<LoggingSchemaSnapshotRows, LoggingSchemaSnapshotError>,
    expected_source_fragment: Option<&str>,
    context: &str,
) {
    assert_fetch_relations_snapshot_error_variant_for_tests(
        result,
        expected_source_fragment,
        context,
    );
}

/// Asserts snapshot-row result maps to the `FetchColumns` variant.
#[cfg(test)]
pub(crate) fn assert_fetch_columns_snapshot_error_for_tests(
    result: Result<LoggingSchemaSnapshotRows, LoggingSchemaSnapshotError>,
    expected_source_fragment: Option<&str>,
    context: &str,
) {
    assert_fetch_columns_snapshot_error_variant_for_tests(
        result,
        expected_source_fragment,
        context,
    );
}