f3-rs/tests/basic.rs

101 lines
2.6 KiB
Rust
Raw Normal View History

2023-10-24 19:44:18 +05:30
use std::fs;
use std::path::Path;
use jsonschema::JSONSchema;
use serde::Serialize;
use serde_json::json;
fn util<T: Serialize>(instance: &T, schema_path: &Path) {
let instance = json!(instance);
let schema_str = fs::read_to_string(schema_path).unwrap();
let schema = serde_json::from_str(&schema_str).unwrap();
let compiled = JSONSchema::compile(&schema).expect("A valid schema");
let result = compiled.validate(&instance);
if let Err(errors) = result {
for error in errors {
println!("Validation error: {}", error);
println!("Instance path: {}", error.instance_path);
}
}
assert!(compiled.is_valid(&instance));
}
#[test]
fn test_comment() {
let schema_path = Path::new("./f3-schemas/comment.json");
let instance = f3_rs::Comment {
created: "1963-06-19T08:30:06.283185Z".into(),
updated: "1963-06-19T08:30:06.283185Z".into(),
..Default::default()
};
util(&instance, schema_path);
}
#[test]
fn test_issue() {
let schema_path = Path::new("./f3-schemas/issue.json");
let instance = f3_rs::Issue {
created: "1963-06-19T08:30:06.283185Z".into(),
updated: "1963-06-19T08:30:06.283185Z".into(),
..Default::default()
};
util(&instance, schema_path);
}
#[test]
fn test_user() {
let schema_path = Path::new("./f3-schemas/user.json");
let instance = f3_rs::User {
..Default::default()
};
util(&instance, schema_path);
}
#[test]
fn test_pr() {
let schema_path = Path::new("./f3-schemas/pullrequest.json");
let instance = f3_rs::PullRequest {
created: "1963-06-19T08:30:06.283185Z".into(),
updated: "1963-06-19T08:30:06.283185Z".into(),
..Default::default()
};
util(&instance, schema_path);
}
#[test]
fn test_identities() {
let schema_path = Path::new("./f3-schemas/identities.json");
let instance = f3_rs::Identities::default();
util(&instance, schema_path);
}
#[test]
fn test_label() {
let schema_path = Path::new("./f3-schemas/label.json");
let instance = f3_rs::Label::default();
util(&instance, schema_path);
}
#[test]
fn test_object() {
let schema_path = Path::new("./f3-schemas/object.json");
let instance = f3_rs::Object::default();
util(&instance, schema_path);
}
#[test]
fn test_organization() {
let schema_path = Path::new("./f3-schemas/organization.json");
let instance = f3_rs::Organization::default();
util(&instance, schema_path);
}
#[test]
fn test_project() {
let schema_path = Path::new("./f3-schemas/project.json");
let instance = f3_rs::Project {
..Default::default()
};
util(&instance, schema_path);
}