feat: jsonschema based test

This commit is contained in:
Aravinth Manivannan 2023-10-24 19:44:18 +05:30
parent 88b687639e
commit 1ce0744b18
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 1396 additions and 22 deletions

1315
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -14,3 +14,6 @@ authors = ["realaravinth <realaravinth@batsense.net>"]
[dependencies]
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
[dev-dependencies]
jsonschema = "0.17.1"

100
tests/basic.rs Normal file
View file

@ -0,0 +1,100 @@
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);
}