Skip to content

Commit 0882f65

Browse files
authored
Merge pull request #249 from cipherstash/add-simple-query-insert-literal-tests
test: add simple query to insert literal tests
2 parents 2fce71d + 1181026 commit 0882f65

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ pub async fn insert(sql: &str, params: &[&(dyn ToSql + Sync)]) {
125125
client.query(sql, params).await.unwrap();
126126
}
127127

128+
pub async fn insert_simple_query(sql: &str) {
129+
let client = connect_with_tls(PROXY).await;
130+
client.simple_query(sql).await.unwrap();
131+
}
132+
128133
pub async fn query<T: for<'a> tokio_postgres::types::FromSql<'a> + Send + Sync>(
129134
sql: &str,
130135
) -> Vec<T> {

packages/cipherstash-proxy-integration/src/insert/insert_with_literal.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#[cfg(test)]
22
mod tests {
3-
use crate::common::{clear, insert, query_by, random_id, random_limited, trace};
3+
use crate::common::{
4+
clear, insert, insert_simple_query, query_by, random_id, random_limited, simple_query,
5+
trace,
6+
};
47
use chrono::NaiveDate;
58
use serde_json::Value;
69

@@ -17,14 +20,42 @@ mod tests {
1720
let encrypted_col = format!("encrypted_{}", stringify!($pg_type));
1821
let encrypted_val = crate::value_for_type!($type, random_limited());
1922

20-
let sql = format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ($1, '{encrypted_val}')");
21-
insert(&sql, &[&id]).await;
23+
let expected = vec![encrypted_val.clone()];
2224

23-
let expected = vec![encrypted_val];
25+
let insert_sql = format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ($1, '{encrypted_val}')");
26+
let select_sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = $1");
27+
28+
insert(&insert_sql, &[&id]).await;
29+
let actual = query_by::<$type>(&select_sql, &id).await;
30+
31+
assert_eq!(expected, actual);
2432

25-
let sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = $1");
33+
}
34+
};
35+
}
2636

27-
let actual = query_by::<$type>(&sql, &id).await;
37+
macro_rules! test_insert_simple_query_with_literal {
38+
($name: ident, $type: ident, $pg_type: ident) => {
39+
#[tokio::test]
40+
pub async fn $name() {
41+
trace();
42+
43+
clear().await;
44+
45+
46+
let id = random_id();
47+
48+
let encrypted_col = format!("encrypted_{}", stringify!($pg_type));
49+
let encrypted_val = crate::value_for_type!($type, random_limited());
50+
51+
let insert_sql = format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ({id}, '{encrypted_val}')");
52+
let select_sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = {id}");
53+
54+
55+
let expected = vec![encrypted_val];
56+
57+
insert_simple_query(&insert_sql).await;
58+
let actual = simple_query::<$type>(&select_sql).await;
2859

2960
assert_eq!(expected, actual);
3061
}
@@ -40,6 +71,15 @@ mod tests {
4071
test_insert_with_literal!(insert_with_literal_date, NaiveDate, date);
4172
test_insert_with_literal!(insert_with_literal_jsonb, Value, jsonb);
4273

74+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_int2, i16, int2);
75+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_int4, i32, int4);
76+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_int8, i64, int8);
77+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_float8, f64, float8);
78+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_bool, bool, bool);
79+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_text, String, text);
80+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_date, NaiveDate, date);
81+
test_insert_simple_query_with_literal!(insert_simple_query_with_literal_jsonb, Value, jsonb);
82+
4383
// -----------------------------------------------------------------
4484

4585
/// Sanity check insert of unencrypted literal value

packages/cipherstash-proxy-integration/src/insert/insert_with_null_literal.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(test)]
22
mod tests {
3-
use crate::common::{clear, insert, query_by, random_id, trace};
3+
use crate::common::{
4+
clear, insert, insert_simple_query, query_by, random_id, simple_query_with_null, trace,
5+
};
46
use chrono::NaiveDate;
57
use serde_json::Value;
68

@@ -40,6 +42,45 @@ mod tests {
4042
test_insert_with_null_literal!(insert_with_null_literal_date, NaiveDate, date);
4143
test_insert_with_null_literal!(insert_with_null_literal_jsonb, Value, jsonb);
4244

45+
macro_rules! test_insert_simple_query_with_null_literal {
46+
($name: ident, $pg_type: ident) => {
47+
#[tokio::test]
48+
pub async fn $name() {
49+
trace();
50+
51+
clear().await;
52+
53+
let id = random_id();
54+
55+
let encrypted_col = format!("encrypted_{}", stringify!($pg_type));
56+
let encrypted_val: Option<String> = None;
57+
58+
let insert_sql =
59+
format!("INSERT INTO encrypted (id, {encrypted_col}) VALUES ({id}, NULL)");
60+
let select_sql = format!("SELECT {encrypted_col} FROM encrypted WHERE id = {id}");
61+
62+
let expected = vec![encrypted_val];
63+
64+
insert_simple_query(&insert_sql).await;
65+
let actual = simple_query_with_null(&select_sql).await;
66+
67+
assert_eq!(expected, actual);
68+
}
69+
};
70+
}
71+
72+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_int2, int2);
73+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_int4, int4);
74+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_int8, int8);
75+
test_insert_simple_query_with_null_literal!(
76+
insert_simple_query_with_null_literal_float8,
77+
float8
78+
);
79+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_bool, bool);
80+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_text, text);
81+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_date, date);
82+
test_insert_simple_query_with_null_literal!(insert_simple_query_with_null_literal_jsonb, jsonb);
83+
4384
// -----------------------------------------------------------------
4485

4586
/// Sanity check insert of unencrypted literal value

0 commit comments

Comments
 (0)