jsonpath/tests/lib.rs

141 lines
3.6 KiB
Rust
Raw Normal View History

2019-03-14 22:30:42 +09:00
extern crate jsonpath_lib as jsonpath;
2019-03-24 21:18:58 +09:00
extern crate serde;
2019-03-14 22:30:42 +09:00
#[macro_use]
extern crate serde_json;
2019-06-02 22:03:35 +09:00
use serde::Deserialize;
2019-03-14 22:30:42 +09:00
use serde_json::Value;
2019-06-02 22:03:35 +09:00
use common::{compare_result, read_contents, read_json, setup};
2019-03-14 22:30:42 +09:00
2019-06-02 22:03:35 +09:00
mod common;
2019-03-14 22:30:42 +09:00
#[test]
fn compile() {
2019-06-02 22:03:35 +09:00
setup();
2019-03-14 22:30:42 +09:00
let mut template = jsonpath::compile("$..friends[2]");
let json_obj = read_json("./benches/data_obj.json");
let json = template(&json_obj).unwrap();
let ret = json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Gray Berry"}
]);
2019-06-02 22:03:35 +09:00
compare_result(json, ret);
2019-03-14 22:30:42 +09:00
let json_obj = read_json("./benches/data_array.json");
let json = template(&json_obj).unwrap();
let ret = json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Rosetta Erickson"}
]);
2019-06-02 22:03:35 +09:00
compare_result(json, ret);
2019-03-14 22:30:42 +09:00
}
#[test]
fn selector() {
2019-06-02 22:03:35 +09:00
setup();
2019-03-14 22:30:42 +09:00
let json_obj = read_json("./benches/data_obj.json");
let mut reader = jsonpath::selector(&json_obj);
let json = reader("$..friends[2]").unwrap();
let ret = json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Gray Berry"}
]);
2019-06-02 22:03:35 +09:00
compare_result(json, ret);
2019-03-14 22:30:42 +09:00
let json = reader("$..friends[0]").unwrap();
let ret = json!([
{"id": 0},
{"id": 0,"name": "Millicent Norman"}
]);
2019-06-02 22:03:35 +09:00
compare_result(json, ret);
2019-03-14 22:30:42 +09:00
}
2019-04-04 09:37:44 +09:00
#[test]
fn selector_as() {
2019-06-02 22:03:35 +09:00
#[derive(Deserialize, PartialEq, Debug)]
2019-04-04 09:37:44 +09:00
struct Friend {
id: u8,
name: Option<String>,
}
2019-06-02 22:03:35 +09:00
let json_obj = read_json("./benches/data_obj.json");
let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
2019-04-04 09:37:44 +09:00
let json = selector("$..friends[2]").unwrap();
let ret = vec!(
Friend { id: 2, name: Some("Gray Berry".to_string()) },
Friend { id: 2, name: Some("Gray Berry".to_string()) },
);
assert_eq!(json, ret);
let json = selector("$..friends[0]").unwrap();
let ret = vec!(
Friend { id: 0, name: None },
Friend { id: 0, name: Some("Millicent Norman".to_string()) },
);
assert_eq!(json, ret);
}
2019-03-14 22:30:42 +09:00
#[test]
fn select() {
let json_obj = read_json("./benches/example.json");
let json = jsonpath::select(&json_obj, "$..book[2]").unwrap();
let ret = json!([{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"price" : 8.99
}]);
2019-06-02 22:03:35 +09:00
compare_result(json, ret);
}
#[test]
fn select_str() {
let json_str = read_contents("./benches/example.json");
2019-04-01 14:32:20 +09:00
let result_str = jsonpath::select_as_str(&json_str, "$..book[2]").unwrap();
let ret = json!([{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"price" : 8.99
}]);
let json: Value = serde_json::from_str(&result_str).unwrap();
assert_eq!(json, ret);
2019-03-24 21:18:58 +09:00
}
#[test]
fn test_to_struct() {
2019-06-02 22:03:35 +09:00
#[derive(Deserialize, PartialEq, Debug)]
2019-03-24 21:18:58 +09:00
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
2019-06-02 22:03:35 +09:00
let ret: Vec<Person> = jsonpath::select_as(r#"
2019-03-24 21:18:58 +09:00
{
"person":
{
"name": "Doe John",
"age": 44,
"phones": [
"+44 1234567",
"+44 2345678"
]
}
}
"#, "$.person").unwrap();
let person = Person {
name: "Doe John".to_string(),
age: 44,
phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
};
2019-06-02 22:03:35 +09:00
assert_eq!(vec![person], ret);
2019-03-14 22:30:42 +09:00
}