Skip to content

Commit a68e255

Browse files
committed
test: insert with param
1 parent ff0533a commit a68e255

File tree

9 files changed

+161
-270
lines changed

9 files changed

+161
-270
lines changed

packages/cipherstash-proxy-integration/src/common.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ pub fn random_id() -> i64 {
2323
rng.random_range(1..=i64::MAX)
2424
}
2525

26+
// Limited by valid data range
27+
pub fn random() -> i32 {
28+
use rand::Rng;
29+
let mut rng = rand::rng();
30+
rng.random_range(1..=31)
31+
}
32+
2633
pub fn random_string() -> String {
2734
rand::rng()
2835
.sample_iter(&Alphanumeric)
@@ -126,6 +133,15 @@ pub async fn query<T: for<'a> tokio_postgres::types::FromSql<'a> + Send + Sync>(
126133
rows.iter().map(|row| row.get(0)).collect::<Vec<T>>()
127134
}
128135

136+
pub async fn query_by<T>(sql: &str, param: &(dyn ToSql + Sync)) -> Vec<T>
137+
where
138+
T: for<'a> tokio_postgres::types::FromSql<'a> + Send + Sync,
139+
{
140+
let client = connect_with_tls(PROXY).await;
141+
let rows = client.query(sql, &[param]).await.unwrap();
142+
rows.iter().map(|row| row.get(0)).collect::<Vec<T>>()
143+
}
144+
129145
pub async fn simple_query<T: std::str::FromStr>(sql: &str) -> Vec<T>
130146
where
131147
<T as std::str::FromStr>::Err: std::fmt::Debug,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::common::{clear, insert, query, query_by, random_id, trace};
4+
use chrono::NaiveDate;
5+
use serde_json::Value;
6+
use tracing::info;
7+
8+
macro_rules! test_insert_with_null_param {
9+
($name: ident, $type: ident, $pg_type: ident) => {
10+
#[tokio::test]
11+
pub async fn $name() {
12+
trace();
13+
14+
clear().await;
15+
16+
let id = random_id();
17+
18+
let encrypted_col = format!("encrypted_{}", stringify!($pg_type));
19+
let encrypted_val: Option<$type> = None;
20+
21+
let sql = format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ($1, $2)");
22+
insert(&sql, &[&id, &encrypted_val]).await;
23+
24+
let expected = vec![encrypted_val];
25+
26+
let sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = $1");
27+
let result = query_by::<Option<$type>>(&sql, &id).await;
28+
29+
assert_eq!(expected, result);
30+
}
31+
};
32+
}
33+
34+
test_insert_with_null_param!(insert_with_null_param_int2, i16, int2);
35+
test_insert_with_null_param!(insert_with_null_param_int4, i32, int4);
36+
test_insert_with_null_param!(insert_with_null_param_int8, i64, int8);
37+
test_insert_with_null_param!(insert_with_null_param_float8, f64, float8);
38+
test_insert_with_null_param!(insert_with_null_param_bool, bool, bool);
39+
test_insert_with_null_param!(insert_with_null_param_text_only, String, text);
40+
test_insert_with_null_param!(insert_with_null_param_date, NaiveDate, date);
41+
test_insert_with_null_param!(insert_with_null_param_jsonb, Value, jsonb);
42+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::common::{clear, insert, query, query_by, random, random_id, trace};
4+
use chrono::NaiveDate;
5+
use serde_json::Value;
6+
use tracing::info;
7+
8+
macro_rules! test_insert_with_param {
9+
($name: ident, $type: ident, $pg_type: ident) => {
10+
#[tokio::test]
11+
pub async fn $name() {
12+
trace();
13+
14+
clear().await;
15+
16+
let id = random_id();
17+
18+
let encrypted_col = format!("encrypted_{}", stringify!($pg_type));
19+
let encrypted_val = crate::value_for_type!($type, random());
20+
21+
let sql = format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ($1, $2)");
22+
insert(&sql, &[&id, &encrypted_val]).await;
23+
24+
let expected = vec![encrypted_val];
25+
26+
let sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = $1");
27+
28+
let actual = query_by::<$type>(&sql, &id).await;
29+
30+
assert_eq!(expected, actual);
31+
}
32+
};
33+
}
34+
35+
test_insert_with_param!(insert_with_param_int2, i16, int2);
36+
test_insert_with_param!(insert_with_param_int4, i32, int4);
37+
test_insert_with_param!(insert_with_param_int8, i64, int8);
38+
test_insert_with_param!(insert_with_param_float8, f64, float8);
39+
test_insert_with_param!(insert_with_param_bool, bool, bool);
40+
test_insert_with_param!(insert_with_param_text, String, text);
41+
test_insert_with_param!(insert_with_param_date, NaiveDate, date);
42+
test_insert_with_param!(insert_with_param_jsonb, Value, jsonb);
43+
44+
// -----------------------------------------------------------------
45+
46+
/// Sanity check insert of unencrypted plaintext value
47+
#[tokio::test]
48+
pub async fn insert_with_param_plaintext() {
49+
trace();
50+
51+
clear().await;
52+
53+
let id = random_id();
54+
55+
let encrypted_val = crate::value_for_type!(String, random());
56+
57+
let sql = format!("INSERT INTO encrypted (id, plaintext) VALUES ($1, $2)");
58+
insert(&sql, &[&id, &encrypted_val]).await;
59+
60+
let expected = vec![encrypted_val];
61+
62+
let sql = format!("SELECT plaintext FROM encrypted WHERE id = $1");
63+
64+
let actual = query_by::<String>(&sql, &id).await;
65+
66+
assert_eq!(expected, actual);
67+
}
68+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod insert_with_null_param;
2+
mod insert_with_param;

packages/cipherstash-proxy-integration/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod common;
22
mod decrypt;
33
mod empty_result;
44
mod extended_protocol_error_messages;
5+
mod insert;
56
mod map_concat;
67
mod map_literals;
78
mod map_match_index;
@@ -16,3 +17,26 @@ mod pipeline;
1617
mod schema_change;
1718
mod select;
1819
mod simple_protocol;
20+
21+
#[macro_export]
22+
macro_rules! value_for_type {
23+
(String, $i:expr) => {
24+
((b'A' + ($i - 1) as u8) as char).to_string()
25+
};
26+
27+
(NaiveDate, $i:expr) => {
28+
NaiveDate::parse_from_str(&format!("2023-01-{}", $i), "%Y-%m-%d").unwrap()
29+
};
30+
31+
(Value, $i:expr) => {
32+
serde_json::json!({"n": $i, "s": format!("{}", $i) })
33+
};
34+
35+
(bool, $i:expr) => {
36+
$i % 2 == 0
37+
};
38+
39+
($type:ident, $i:expr) => {
40+
$i as $type
41+
};
42+
}

0 commit comments

Comments
 (0)