athena_rs 3.22.1

Hyper performant polyglot Database driver
Documentation
//! Shared `/debug/schema` service-loader fixture and assertion helpers.
//!
//! These helpers centralize reusable staged fixture construction and typed
//! report/snapshot assertions so service-loader stage and handoff modules can
//! share one baseline helper surface.

use super::debug_contracts::LoggingSchemaDebugReport;
use super::debug_service_loader_stage_contracts::LoggingSchemaDebugReportForClientStage;
use super::debug_snapshot_contracts::LoggingSchemaSnapshot;
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,
};

/// Builds an empty snapshot fixture used by service-loader stage and handoff tests.
#[cfg(test)]
pub(crate) fn build_empty_logging_schema_snapshot_for_tests() -> LoggingSchemaSnapshot {
    LoggingSchemaSnapshot {
        relations: Vec::new(),
        columns: Vec::new(),
    }
}

/// Builds a staged service-loader payload with a successful empty snapshot fixture.
#[cfg(test)]
pub(crate) fn build_successful_service_loader_stage_for_tests()
-> LoggingSchemaDebugReportForClientStage {
    LoggingSchemaDebugReportForClientStage {
        logging_client_name: "athena_logging".to_string(),
        snapshot_result: Ok(build_empty_logging_schema_snapshot_for_tests()),
    }
}

/// Builds a staged service-loader payload fixture with a `FetchRelations`
/// snapshot error.
#[cfg(test)]
pub(crate) fn build_fetch_relations_service_loader_stage_for_tests(
    source_message: &str,
) -> LoggingSchemaDebugReportForClientStage {
    LoggingSchemaDebugReportForClientStage {
        logging_client_name: "athena_logging".to_string(),
        snapshot_result: Err(LoggingSchemaSnapshotError::FetchRelations(
            sqlx::Error::Protocol(source_message.to_string()),
        )),
    }
}

/// Builds a staged service-loader payload fixture with a `FetchColumns`
/// snapshot error.
#[cfg(test)]
pub(crate) fn build_fetch_columns_service_loader_stage_for_tests(
    source_message: &str,
) -> LoggingSchemaDebugReportForClientStage {
    LoggingSchemaDebugReportForClientStage {
        logging_client_name: "athena_logging".to_string(),
        snapshot_result: Err(LoggingSchemaSnapshotError::FetchColumns(
            sqlx::Error::Protocol(source_message.to_string()),
        )),
    }
}

/// Asserts a service-loader report result is successful and preserves base report fields.
#[cfg(test)]
pub(crate) fn assert_successful_service_loader_report_result_for_tests(
    result: Result<LoggingSchemaDebugReport, LoggingSchemaSnapshotError>,
    context: &str,
) {
    match result {
        Ok(report) => {
            assert_eq!(
                report.logging_client, "athena_logging",
                "unexpected {context} logging_client"
            );
            assert!(
                report.observed_tables.is_empty(),
                "expected {context} observed tables to be empty"
            );
        }
        Err(_) => panic!("expected successful report result for {context}"),
    }
}

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

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