athena_rs 3.23.0

Hyper performant polyglot Database driver
Documentation
//! Present-table status-row assembly for `/debug/schema`.
//!
//! This module owns construction of `LoggingSchemaExpectedTableStatus` entries
//! for expected tables that were discovered in observed schema state.

use super::debug_evaluation_contracts::LoggingSchemaExpectedTableStatus;
use super::debug_observed_tables::LoggingSchemaObservedTable;
use super::debug_table_comparison_contracts::LoggingSchemaTableComparison;
use super::logging_expectation_contracts::ExpectedLoggingTable;

/// Builds one status row for an expected table that was discovered.
pub(super) fn build_present_expected_table_status(
    expected: &ExpectedLoggingTable,
    expected_columns: Vec<String>,
    observed: &LoggingSchemaObservedTable,
    comparison: LoggingSchemaTableComparison,
) -> LoggingSchemaExpectedTableStatus {
    LoggingSchemaExpectedTableStatus {
        table_schema: expected.table_schema.to_string(),
        table_name: expected.table_name.to_string(),
        expected_relation_type: expected.relation_type.to_string(),
        required: expected.required,
        purpose: expected.purpose.to_string(),
        expected_columns,
        found: true,
        found_relation_type: Some(observed.relation_type.clone()),
        relation_type_matches: comparison.relation_type_matches,
        found_columns: observed.columns.clone(),
        missing_columns: comparison.missing_columns,
        unexpected_columns: comparison.unexpected_columns,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const REQUIRED_COLUMNS: &[&str] = &["request_id", "status_code"];

    /// Builds a reusable expected-table contract for present-status tests.
    fn expected_table(required: bool) -> ExpectedLoggingTable {
        ExpectedLoggingTable {
            table_schema: "public",
            table_name: "gateway_request_log",
            relation_type: "BASE TABLE",
            required,
            purpose: "test table",
            required_columns: REQUIRED_COLUMNS,
        }
    }

    #[test]
    /// Preserves discovered columns and comparison deltas on present rows.
    fn present_status_includes_found_columns_and_comparison_deltas() {
        let expected = expected_table(true);
        let observed = LoggingSchemaObservedTable {
            table_schema: "public".to_string(),
            table_name: "gateway_request_log".to_string(),
            relation_type: "BASE TABLE".to_string(),
            columns: vec!["request_id".to_string(), "extra_col".to_string()],
        };
        let comparison = LoggingSchemaTableComparison {
            relation_type_matches: true,
            missing_columns: vec!["status_code".to_string()],
            unexpected_columns: vec!["extra_col".to_string()],
        };

        let row = build_present_expected_table_status(
            &expected,
            vec!["request_id".to_string(), "status_code".to_string()],
            &observed,
            comparison,
        );

        assert!(row.found);
        assert_eq!(row.found_columns, observed.columns);
        assert_eq!(row.missing_columns, vec!["status_code".to_string()]);
        assert_eq!(row.unexpected_columns, vec!["extra_col".to_string()]);
    }
}