-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonb_path_query.rs
More file actions
126 lines (93 loc) · 3.74 KB
/
jsonb_path_query.rs
File metadata and controls
126 lines (93 loc) · 3.74 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#[cfg(test)]
mod tests {
use crate::common::{
clear_with_client, connect_with_tls, insert_jsonb_with_client, query_by_with_client,
simple_query_with_client, trace, PROXY,
};
use crate::support::assert::assert_expected;
use crate::support::json_path::JsonPath;
use serde::de::DeserializeOwned;
use serde_json::Value;
use tokio_postgres::Client;
async fn select_jsonb<T>(selector: &str, value: T, client: &Client)
where
T: DeserializeOwned,
serde_json::Value: From<T>,
{
let selector = JsonPath::new(selector);
let value = Value::from(value);
let expected = vec![value];
let sql = "SELECT jsonb_path_query(encrypted_jsonb, $1) FROM encrypted";
let actual = query_by_with_client::<Value>(sql, &selector, client).await;
assert_expected(&expected, &actual);
let sql = format!("SELECT jsonb_path_query(encrypted_jsonb, '{selector}') FROM encrypted");
let actual = simple_query_with_client::<Value>(&sql, client).await;
assert_expected(&expected, &actual);
}
#[tokio::test]
async fn select_jsonb_path_query_number() {
trace();
let client = connect_with_tls(PROXY).await;
clear_with_client(&client).await;
insert_jsonb_with_client(&client).await;
select_jsonb("$.number", 42, &client).await;
}
#[tokio::test]
async fn select_jsonb_path_query_string() {
trace();
let client = connect_with_tls(PROXY).await;
clear_with_client(&client).await;
insert_jsonb_with_client(&client).await;
select_jsonb("$.nested.string", "world".to_string(), &client).await;
}
#[tokio::test]
async fn select_jsonb_path_query_value() {
trace();
let client = connect_with_tls(PROXY).await;
clear_with_client(&client).await;
insert_jsonb_with_client(&client).await;
let v = serde_json::json!({
"number": 1815,
"string": "world",
});
select_jsonb("$.nested", v, &client).await;
}
#[tokio::test]
async fn select_jsonb_path_query_with_unknown() {
trace();
let client = connect_with_tls(PROXY).await;
clear_with_client(&client).await;
insert_jsonb_with_client(&client).await;
let selector = JsonPath::new("$.vtha");
let expected = vec![];
let sql = "SELECT jsonb_path_query(encrypted_jsonb, $1) as selected FROM encrypted";
let actual = query_by_with_client::<Value>(sql, &selector, &client).await;
assert_expected(&expected, &actual);
let sql = format!(
"SELECT jsonb_path_query(encrypted_jsonb, '{selector}') as selected FROM encrypted"
);
let actual = simple_query_with_client::<Value>(&sql, &client).await;
assert_expected(&expected, &actual);
}
#[tokio::test]
async fn select_jsonb_path_query_with_alias() {
trace();
let client = connect_with_tls(PROXY).await;
clear_with_client(&client).await;
insert_jsonb_with_client(&client).await;
let value = serde_json::json!({
"number": 1815,
"string": "world",
});
let selector = JsonPath::new("$.nested");
let expected = vec![value];
let sql = "SELECT jsonb_path_query(encrypted_jsonb, $1) as selected FROM encrypted";
let actual = query_by_with_client::<Value>(sql, &selector, &client).await;
assert_expected(&expected, &actual);
let sql = format!(
"SELECT jsonb_path_query(encrypted_jsonb, '{selector}') as selected FROM encrypted"
);
let actual = simple_query_with_client::<Value>(&sql, &client).await;
assert_expected(&expected, &actual);
}
}