use rjapi::{JsonApiError, JsonApiErrorResponse, JsonApiResponse, Resource};
use serde_json::json;
#[test]
fn test_success_response() {
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,
};
let response = JsonApiResponse::new(resource).build();
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::<()>();
assert!(document.jsonapi.is_none()); 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"
);
}