forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rs
75 lines (65 loc) · 1.89 KB
/
common.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright Redis Ltd. 2016 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
use std::io::Read;
use serde_json::Value;
use rejson::jsonpath::{compile, create};
#[allow(dead_code)]
pub fn setup() {
let _ = env_logger::try_init();
}
#[allow(dead_code)]
pub fn read_json(path: &str) -> Value {
let mut f = std::fs::File::open(path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap()
}
#[allow(dead_code)]
pub fn read_contents(path: &str) -> String {
let mut f = std::fs::File::open(path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();
contents
}
#[allow(dead_code)]
pub fn select_and_then_compare(path: &str, json: Value, target: Value) {
let json_path = compile(path).unwrap();
let calculator = create(&json_path);
let result = calculator.calc(&json);
assert_eq!(
result
.iter()
.map(|v| v.clone().clone())
.collect::<Vec<Value>>(),
match target {
Value::Array(vec) => vec,
_ => panic!("Give me the Array!"),
},
"{}",
path
);
// let mut selector = Selector::default();
// let result = selector
// .str_path(path)
// .unwrap()
// .value(&json)
// .select()
// .unwrap();
// assert_eq!(
// result.iter().map(|v| v.clone().clone()).collect::<Vec<Value>>(),
// match target {
// Value::Array(vec) => vec,
// _ => panic!("Give me the Array!"),
// },
// "{}",
// path
// );
}
#[allow(dead_code)]
pub fn compare_result(result: Vec<&Value>, target: Value) {
let result = serde_json::to_value(result).unwrap();
assert_eq!(result, target);
}