<commit_before>extern crate jsonapi;
extern crate serde_json;
extern crate env_logger;

use jsonapi::api::*;

mod helper;
use helper::read_json_file;

#[test]
fn it_works() {
    let _ = env_logger::try_init();
    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    assert_eq!(resource.id, "123");

    let serialized = serde_json::to_string(&resource).unwrap();
    let deserialized: Resource = serde_json::from_str(&serialized).unwrap();

    assert_eq!(deserialized.id, resource.id);

    let jsonapidocument = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    assert_eq!(jsonapidocument.is_valid(), true);

}

#[test]
fn the_members_data_and_errors_must_not_coexist() {
    use serde_json::Value;

    let jsonapidocument = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    let string = serde_json::to_string(&jsonapidocument).unwrap();
    let json: Value = serde_json::from_str(&string).unwrap();
    assert_eq!(json.get("data").unwrap().is_null(), true);
    assert_eq!(json.get("errors"), None);

    let errors = JsonApiErrors::new();
    let jsonapi_document_with_errors = JsonApiDocument {
        errors: Some(errors),
        ..Default::default()
    };

    let string = serde_json::to_string(&jsonapi_document_with_errors).unwrap();
    let json: Value = serde_json::from_str(&string).unwrap();
    assert_eq!(json.get("data"), None);
    assert_eq!(json.get("errors").unwrap().is_array(), true);
}

#[test]
fn jsonapi_document_can_be_valid() {
    let _ = env_logger::try_init();
    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let errors = JsonApiErrors::new();

    let jsonapi_document_with_data = JsonApiDocument {
        data: Some(PrimaryData::Single(Box::new(resource))),
        ..Default::default()
    };

    assert_eq!(jsonapi_document_with_data.is_valid(), true);

    let jsonapi_document_with_errors = JsonApiDocument {
        data: Some(PrimaryData::None),
        errors: Some(errors),
        ..Default::default()
    };

    assert_eq!(jsonapi_document_with_errors.is_valid(), false);
}

#[test]
fn jsonapi_document_invalid_errors() {
    let _ = env_logger::try_init();

    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let included_resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let errors = JsonApiErrors::new();

    let no_content_document: JsonApiDocument = Default::default();

    match no_content_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(&DocumentValidationError::MissingContent));
        }
    }

    let null_data_content_document = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    match null_data_content_document.validate() {
        None => assert!(true),
        Some(_) => assert!(false),
    }

    let mixed_errors_and_data_document = JsonApiDocument {
        data: Some(PrimaryData::Single(Box::new(resource))),
        errors: Some(errors),
        ..Default::default()
    };

    match mixed_errors_and_data_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(&DocumentValidationError::DataWithErrors));
        }
    }

    let included_without_data_document = JsonApiDocument {
        included: Some(vec![included_resource]),
        ..Default::default()
    };

    match included_without_data_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(
                &DocumentValidationError::IncludedWithoutData,
            ));
        }
    }
}

#[test]
fn error_from_json_string() {
    let _ = env_logger::try_init();

    let serialized = r#"
        {"id":"1", "links" : {}, "status" : "unknown", "code" : "code1", "title" : "error-title", "detail": "error-detail"}
        "#;
    let error: Result<JsonApiError, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(error.is_ok(), true);
    match error {
        Ok(jsonapierror) => {
            match jsonapierror.id {
                Some(id) => assert_eq!(id, "1"),
                None => assert!(false),
            }
        }
        Err(_) => assert!(false),
    }
}

#[test]
fn single_resource_from_json_string() {
    let _ = env_logger::try_init();
    let serialized =
        r#"{ "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }"#;
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn multiple_resource_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"[
            { "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
            { "id" :"2", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
            { "id" :"3", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }
        ]"#;
    let data: Result<Resources, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn no_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : null
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn single_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : {
                "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {}
            }
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn multiple_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : [
                { "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
                { "id" :"2", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
                { "id" :"3", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }
            ]
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn api_document_from_json_file() {
    let _ = env_logger::try_init();

    let s = ::read_json_file("data/results.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);

    match data {
        Ok(res) => {
            match res.data {
                Some(PrimaryData::Multiple(arr)) => {
                    assert_eq!(arr.len(), 1);
                }
                Some(PrimaryData::Single(_)) => {
                    println!(
                        "api_document_from_json_file : Expected one Resource in a vector, \
                              not a direct Resource"
                    );
                    assert!(false);
                }
                Some(PrimaryData::None) => {
                    println!("api_document_from_json_file : Expected one Resource in a vector");
                    assert!(false);
                }
                None => assert!(false),
            }
        }
        Err(err) => {
            println!("api_document_from_json_file : Error: {:?}", err);
            assert!(false);
        }
    }
}

#[test]
fn api_document_collection_from_json_file() {
    let _ = env_logger::try_init();

    let s = ::read_json_file("data/collection.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);

    match data {
        Ok(res) => {

            match res.data {
                Some(PrimaryData::Multiple(arr)) => {
                    assert_eq!(arr.len(), 1);
                }
                Some(PrimaryData::Single(_)) => {
                    println!(
                        "api_document_collection_from_json_file : Expected one Resource in \
                              a vector, not a direct Resource"
                    );
                    assert!(false);
                }
                Some(PrimaryData::None) => {
                    println!(
                        "api_document_collection_from_json_file : Expected one Resource in \
                              a vector"
                    );
                    assert!(false);
                }
                None => assert!(false),
            }

            match res.included {
                Some(arr) => {
                    assert_eq!(arr.len(), 3);
                    assert_eq!(arr[0].id, "9");
                    assert_eq!(arr[1].id, "5");
                    assert_eq!(arr[2].id, "12");
                }
                None => {
                    println!(
                        "api_document_collection_from_json_file : Expected three Resources \
                              in 'included' in a vector"
                    );
                    assert!(false);
                }
            }

            match res.links {
                Some(links) => {
                    assert_eq!(links.len(), 3);
                }
                None => {
                    println!("api_document_collection_from_json_file : expected links");
                    assert!(false);
                }
            }

        }
        Err(err) => {
            println!("api_document_collection_from_json_file : Error: {:?}", err);
            assert!(false);
        }
    }
}

#[test]
fn can_deserialize_jsonapi_example_resource_001() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_001.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_002() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_002.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_003() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_003.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_004() {
    let _ = env_logger::init();
    let s = ::read_json_file("data/resource_004.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_compound_document() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/compound_document.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}
<commit_msg>test: Fix typo with new test for optional Relationship data<commit_after>extern crate jsonapi;
extern crate serde_json;
extern crate env_logger;

use jsonapi::api::*;

mod helper;
use helper::read_json_file;

#[test]
fn it_works() {
    let _ = env_logger::try_init();
    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    assert_eq!(resource.id, "123");

    let serialized = serde_json::to_string(&resource).unwrap();
    let deserialized: Resource = serde_json::from_str(&serialized).unwrap();

    assert_eq!(deserialized.id, resource.id);

    let jsonapidocument = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    assert_eq!(jsonapidocument.is_valid(), true);

}

#[test]
fn the_members_data_and_errors_must_not_coexist() {
    use serde_json::Value;

    let jsonapidocument = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    let string = serde_json::to_string(&jsonapidocument).unwrap();
    let json: Value = serde_json::from_str(&string).unwrap();
    assert_eq!(json.get("data").unwrap().is_null(), true);
    assert_eq!(json.get("errors"), None);

    let errors = JsonApiErrors::new();
    let jsonapi_document_with_errors = JsonApiDocument {
        errors: Some(errors),
        ..Default::default()
    };

    let string = serde_json::to_string(&jsonapi_document_with_errors).unwrap();
    let json: Value = serde_json::from_str(&string).unwrap();
    assert_eq!(json.get("data"), None);
    assert_eq!(json.get("errors").unwrap().is_array(), true);
}

#[test]
fn jsonapi_document_can_be_valid() {
    let _ = env_logger::try_init();
    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let errors = JsonApiErrors::new();

    let jsonapi_document_with_data = JsonApiDocument {
        data: Some(PrimaryData::Single(Box::new(resource))),
        ..Default::default()
    };

    assert_eq!(jsonapi_document_with_data.is_valid(), true);

    let jsonapi_document_with_errors = JsonApiDocument {
        data: Some(PrimaryData::None),
        errors: Some(errors),
        ..Default::default()
    };

    assert_eq!(jsonapi_document_with_errors.is_valid(), false);
}

#[test]
fn jsonapi_document_invalid_errors() {
    let _ = env_logger::try_init();

    let resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let included_resource = Resource {
        _type: "test".into(),
        id: "123".into(),
        attributes: ResourceAttributes::new(),
        relationships: Some(Relationships::new()),
        links: None,
        meta: Some(Meta::new()),
    };

    let errors = JsonApiErrors::new();

    let no_content_document: JsonApiDocument = Default::default();

    match no_content_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(&DocumentValidationError::MissingContent));
        }
    }

    let null_data_content_document = JsonApiDocument {
        data: Some(PrimaryData::None),
        ..Default::default()
    };

    match null_data_content_document.validate() {
        None => assert!(true),
        Some(_) => assert!(false),
    }

    let mixed_errors_and_data_document = JsonApiDocument {
        data: Some(PrimaryData::Single(Box::new(resource))),
        errors: Some(errors),
        ..Default::default()
    };

    match mixed_errors_and_data_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(&DocumentValidationError::DataWithErrors));
        }
    }

    let included_without_data_document = JsonApiDocument {
        included: Some(vec![included_resource]),
        ..Default::default()
    };

    match included_without_data_document.validate() {
        None => assert!(false),
        Some(errors) => {
            assert!(errors.contains(
                &DocumentValidationError::IncludedWithoutData,
            ));
        }
    }
}

#[test]
fn error_from_json_string() {
    let _ = env_logger::try_init();

    let serialized = r#"
        {"id":"1", "links" : {}, "status" : "unknown", "code" : "code1", "title" : "error-title", "detail": "error-detail"}
        "#;
    let error: Result<JsonApiError, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(error.is_ok(), true);
    match error {
        Ok(jsonapierror) => {
            match jsonapierror.id {
                Some(id) => assert_eq!(id, "1"),
                None => assert!(false),
            }
        }
        Err(_) => assert!(false),
    }
}

#[test]
fn single_resource_from_json_string() {
    let _ = env_logger::try_init();
    let serialized =
        r#"{ "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }"#;
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn multiple_resource_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"[
            { "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
            { "id" :"2", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
            { "id" :"3", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }
        ]"#;
    let data: Result<Resources, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn no_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : null
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn single_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : {
                "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {}
            }
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn multiple_data_document_from_json_string() {
    let _ = env_logger::try_init();
    let serialized = r#"{
            "data" : [
                { "id" :"1", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
                { "id" :"2", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} },
                { "id" :"3", "type" : "post", "attributes" : {}, "relationships" : {}, "links" : {} }
            ]
        }"#;
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(serialized);
    assert_eq!(data.is_ok(), true);
}

#[test]
fn api_document_from_json_file() {
    let _ = env_logger::try_init();

    let s = ::read_json_file("data/results.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);

    match data {
        Ok(res) => {
            match res.data {
                Some(PrimaryData::Multiple(arr)) => {
                    assert_eq!(arr.len(), 1);
                }
                Some(PrimaryData::Single(_)) => {
                    println!(
                        "api_document_from_json_file : Expected one Resource in a vector, \
                              not a direct Resource"
                    );
                    assert!(false);
                }
                Some(PrimaryData::None) => {
                    println!("api_document_from_json_file : Expected one Resource in a vector");
                    assert!(false);
                }
                None => assert!(false),
            }
        }
        Err(err) => {
            println!("api_document_from_json_file : Error: {:?}", err);
            assert!(false);
        }
    }
}

#[test]
fn api_document_collection_from_json_file() {
    let _ = env_logger::try_init();

    let s = ::read_json_file("data/collection.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);

    match data {
        Ok(res) => {

            match res.data {
                Some(PrimaryData::Multiple(arr)) => {
                    assert_eq!(arr.len(), 1);
                }
                Some(PrimaryData::Single(_)) => {
                    println!(
                        "api_document_collection_from_json_file : Expected one Resource in \
                              a vector, not a direct Resource"
                    );
                    assert!(false);
                }
                Some(PrimaryData::None) => {
                    println!(
                        "api_document_collection_from_json_file : Expected one Resource in \
                              a vector"
                    );
                    assert!(false);
                }
                None => assert!(false),
            }

            match res.included {
                Some(arr) => {
                    assert_eq!(arr.len(), 3);
                    assert_eq!(arr[0].id, "9");
                    assert_eq!(arr[1].id, "5");
                    assert_eq!(arr[2].id, "12");
                }
                None => {
                    println!(
                        "api_document_collection_from_json_file : Expected three Resources \
                              in 'included' in a vector"
                    );
                    assert!(false);
                }
            }

            match res.links {
                Some(links) => {
                    assert_eq!(links.len(), 3);
                }
                None => {
                    println!("api_document_collection_from_json_file : expected links");
                    assert!(false);
                }
            }

        }
        Err(err) => {
            println!("api_document_collection_from_json_file : Error: {:?}", err);
            assert!(false);
        }
    }
}

#[test]
fn can_deserialize_jsonapi_example_resource_001() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_001.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_002() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_002.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_003() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_003.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_resource_004() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/resource_004.json");
    let data: Result<Resource, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

#[test]
fn can_deserialize_jsonapi_example_compound_document() {
    let _ = env_logger::try_init();
    let s = ::read_json_file("data/compound_document.json");
    let data: Result<JsonApiDocument, serde_json::Error> = serde_json::from_str(&s);
    assert!(data.is_ok());
}

{'fraction_non_alphanumeric': 0.15488715425080604, 'fraction_numerical': 0.005175632105888342, 'mean_word_length': 2.3201408450704224, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104417', 'n_tokens_mistral': 7321, 'n_tokens_neox': 7081, 'n_words': 1213}