hurl/tests/jsonpath.rs

91 lines
2.6 KiB
Rust
Raw Normal View History

2020-08-27 10:07:46 +03:00
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
extern crate hurl;
2020-09-20 09:40:02 +03:00
use std::fs::read_to_string;
2020-08-27 10:07:46 +03:00
use serde_json;
use serde_json::json;
use hurl::jsonpath;
fn test_ok(s: &str, value: serde_json::Value) -> Vec<serde_json::Value> {
return match jsonpath::parse(s) {
2020-09-20 09:40:02 +03:00
Ok(expr) => expr.eval(value),
2020-08-27 10:07:46 +03:00
Err(e) => panic!("{:?}", e),
};
}
#[test]
fn test_bookstore_path() {
2020-09-20 09:40:02 +03:00
let no_result: Vec<serde_json::Value> = vec![];
2020-08-27 10:07:46 +03:00
let s = read_to_string("tests/bookstore.json").expect("could not read string from file");
2020-09-20 09:40:02 +03:00
let value: serde_json::Value =
serde_json::from_str(s.as_str()).expect("could not parse json file");
2020-08-27 10:07:46 +03:00
2020-09-20 09:40:02 +03:00
assert_eq!(
test_ok("$.store.book[0].title", value.clone()),
vec![json!("Sayings of the Century")]
);
assert_eq!(
test_ok("$.store.book[0].title", value.clone()),
vec![json!("Sayings of the Century")]
);
assert_eq!(
test_ok("$.store.book[?(@.price<10)].title", value.clone()),
vec![json!("Sayings of the Century"), json!("Moby Dick")]
);
assert_eq!(
test_ok("$.store.book[?(@.price < 10)].title", value.clone()),
vec![json!("Sayings of the Century"), json!("Moby Dick")]
);
2020-08-27 10:07:46 +03:00
2020-09-20 09:40:02 +03:00
assert_eq!(
test_ok("$..book[2]", value.clone()),
vec![json!({
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
})]
);
assert_eq!(
test_ok("$..author", value.clone()),
vec![
json!("Nigel Rees"),
json!("Evelyn Waugh"),
json!("Herman Melville"),
json!("J. R. R. Tolkien")
]
);
assert_eq!(
test_ok("$.store.book[?(@.price>100)]", value.clone()),
no_result.clone()
);
2020-08-27 10:07:46 +03:00
}
#[test]
fn test_array() {
2020-09-20 09:40:02 +03:00
let array = json!([0, 1, 2, 3]);
assert_eq!(test_ok("$[2]", array.clone()), vec![json!(2)]);
2020-08-27 10:07:46 +03:00
let array = json!([{"name": "Bob"},{"name": "Bill"}]);
assert_eq!(test_ok("$[0].name", array.clone()), vec![json!("Bob")]);
}