athena_rs 3.26.1

Hyper performant polyglot Database driver
Documentation
//! Snapshot-result assembly for `/debug/schema`.
//!
//! This module maps snapshot-row results into snapshot payload results while
//! delegating success payload shaping to `debug_snapshot_output_assembly`.

use super::debug_snapshot_contracts::{LoggingSchemaSnapshot, LoggingSchemaSnapshotRows};
use super::debug_snapshot_error::LoggingSchemaSnapshotError;
use super::debug_snapshot_output_assembly::build_logging_schema_snapshot_output;

/// Builds one snapshot payload result from a snapshot-row query result.
pub(super) fn build_logging_schema_snapshot_result(
    rows_result: Result<LoggingSchemaSnapshotRows, LoggingSchemaSnapshotError>,
) -> Result<LoggingSchemaSnapshot, LoggingSchemaSnapshotError> {
    rows_result.map(build_logging_schema_snapshot_output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::schema::service::{SchemaColumnRecord, SchemaRelationRecord};

    /// Maps successful snapshot-row results into snapshot payloads.
    #[test]
    fn snapshot_result_assembly_maps_rows_success_to_snapshot() {
        let result = build_logging_schema_snapshot_result(Ok(LoggingSchemaSnapshotRows {
            relations: vec![SchemaRelationRecord {
                table_schema: "public".to_string(),
                table_name: "gateway_request_log".to_string(),
                relation_type: "BASE TABLE".to_string(),
            }],
            columns: vec![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()),
            }],
        }));

        match result {
            Ok(snapshot) => {
                assert_eq!(snapshot.relations.len(), 1);
                assert_eq!(snapshot.columns.len(), 1);
                assert_eq!(snapshot.relations[0].table_name, "gateway_request_log");
                assert_eq!(snapshot.columns[0].column_name, "request_id");
            }
            Err(_) => panic!("expected successful snapshot result"),
        }
    }

    /// Preserves snapshot-row fetch errors when assembling snapshot results.
    #[test]
    fn snapshot_result_assembly_preserves_rows_error() {
        let result =
            build_logging_schema_snapshot_result(Err(LoggingSchemaSnapshotError::FetchRelations(
                sqlx::Error::Protocol("relation query failed".to_string()),
            )));

        match result {
            Err(LoggingSchemaSnapshotError::FetchRelations(_)) => {}
            _ => panic!("expected FetchRelations snapshot error"),
        }
    }
}