rjapi 0.0.1

A framework-agnostic JSON:API 1.1 implementation for Rust
Documentation
//! Basic tests for the JSON:API library

use rjapi::{JsonApiError, JsonApiErrorResponse, JsonApiResponse, Resource};
use serde_json::json;

#[test]
fn test_success_response() {
    // Create a JSON:API resource
    let attributes = json!({
        "title": "Example Post",
        "content": "This is an example post."
    });

    let resource = Resource {
        resource_type: "posts".to_string(),
        id: "1".to_string(),
        attributes: Some(attributes),
        relationships: None,
        links: None,
        meta: None,
    };

    // Create a JSON:API response
    let response = JsonApiResponse::new(resource).build();

    // Verify the response has the correct structure
    assert_eq!(response.jsonapi.as_ref().unwrap().version, "1.1");
    assert!(response.data.is_some());
    assert!(response.errors.is_none());
}

#[test]
fn test_error_response() {
    let error = JsonApiError::builder()
        .status("400")
        .code("BAD_REQUEST")
        .title("Bad Request")
        .detail("The request was malformed")
        .build();

    let response = JsonApiErrorResponse::new(http::StatusCode::BAD_REQUEST, vec![error]);
    let document = response.to_document::<()>();

    // Verify the error response has the correct structure
    assert!(document.jsonapi.is_none()); // We don't add jsonapi version to error responses now
    assert!(document.data.is_none());
    assert_eq!(document.errors.as_ref().unwrap().len(), 1);
    assert_eq!(
        document.errors.as_ref().unwrap()[0].title.as_ref().unwrap(),
        "Bad Request"
    );
    assert_eq!(
        document.errors.as_ref().unwrap()[0]
            .detail
            .as_ref()
            .unwrap(),
        "The request was malformed"
    );
}