athena_rs 3.18.0

Hyper performant polyglot Database driver
Documentation
//! Shared `/debug/schema` response-test helpers.
//!
//! These helpers centralize response-body parsing and stable error-envelope
//! assertions so route/assembly tests can focus on handoff behavior.

use actix_web::HttpResponse;
use serde_json::Value;

/// Parses one HTTP response body into JSON for schema-debug response tests.
#[cfg(test)]
pub(crate) async fn response_json_for_tests(response: HttpResponse, context: &str) -> Value {
    let bytes = actix_web::body::to_bytes(response.into_body())
        .await
        .unwrap_or_else(|error| panic!("{context} response body should serialize: {error}"));

    serde_json::from_slice(&bytes)
        .unwrap_or_else(|error| panic!("{context} response body should be valid JSON: {error}"))
}

/// Asserts the stable schema-debug error envelope with expected message and fragments.
#[cfg(test)]
pub(crate) fn assert_error_payload_for_tests(
    body: &Value,
    expected_message: &str,
    expected_error_fragments: &[&str],
    context: &str,
) {
    assert_eq!(
        body.get("status").and_then(Value::as_str),
        Some("error"),
        "unexpected {context} status payload: {body:?}"
    );
    assert_eq!(
        body.get("message").and_then(Value::as_str),
        Some(expected_message),
        "unexpected {context} message payload: {body:?}"
    );

    let error = body
        .get("error")
        .and_then(Value::as_str)
        .unwrap_or_else(|| panic!("missing {context} error payload: {body:?}"));

    for fragment in expected_error_fragments {
        assert!(
            error.contains(fragment),
            "expected {context} error payload to contain '{fragment}': {body:?}"
        );
    }
}

/// Asserts a successful schema-debug payload preserves the canonical
/// `logging_client` field value.
#[cfg(test)]
pub(crate) fn assert_success_logging_client_for_tests(body: &Value, context: &str) {
    assert_eq!(
        body.get("logging_client").and_then(Value::as_str),
        Some("athena_logging"),
        "unexpected {context} logging_client payload: {body:?}"
    );
}

/// Asserts a successful schema-debug payload contains one required top-level
/// field and the canonical logging-client value.
#[cfg(test)]
pub(crate) fn assert_success_payload_field_for_tests(
    body: &Value,
    required_field: &str,
    context: &str,
) {
    assert_success_logging_client_for_tests(body, context);
    assert!(
        body.get(required_field).is_some(),
        "expected {required_field} field in {context} payload"
    );
}