Skip to content

Commit 93740f7

Browse files
committed
test: insert with literal
1 parent a88c51e commit 93740f7

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

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_literal {
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, '{encrypted_val}')");
22+
insert(&sql, &[&id]).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_literal!(insert_with_literal_int2, i16, int2);
36+
test_insert_with_literal!(insert_with_literal_int4, i32, int4);
37+
test_insert_with_literal!(insert_with_literal_int8, i64, int8);
38+
test_insert_with_literal!(insert_with_literal_float8, f64, float8);
39+
test_insert_with_literal!(insert_with_literal_bool, bool, bool);
40+
test_insert_with_literal!(insert_with_literal_text, String, text);
41+
test_insert_with_literal!(insert_with_literal_date, NaiveDate, date);
42+
test_insert_with_literal!(insert_with_literal_jsonb, Value, jsonb);
43+
44+
// -----------------------------------------------------------------
45+
46+
/// Sanity check insert of unencrypted literal value
47+
#[tokio::test]
48+
pub async fn insert_with_literal_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, '{encrypted_val}')");
58+
insert(&sql, &[&id]).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: 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_null_literal {
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, NULL)");
22+
insert(&sql, &[&id]).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::<Option<$type>>(&sql, &id).await;
29+
30+
assert_eq!(expected, actual);
31+
}
32+
};
33+
}
34+
35+
test_insert_with_null_literal!(insert_with_null_literal_int2, i16, int2);
36+
test_insert_with_null_literal!(insert_with_null_literal_int4, i32, int4);
37+
test_insert_with_null_literal!(insert_with_null_literal_int8, i64, int8);
38+
test_insert_with_null_literal!(insert_with_null_literal_float8, f64, float8);
39+
test_insert_with_null_literal!(insert_with_null_literal_bool, bool, bool);
40+
test_insert_with_null_literal!(insert_with_null_literal_text, String, text);
41+
test_insert_with_null_literal!(insert_with_null_literal_date, NaiveDate, date);
42+
test_insert_with_null_literal!(insert_with_null_literal_jsonb, Value, jsonb);
43+
44+
// -----------------------------------------------------------------
45+
46+
/// Sanity check insert of unencrypted literal value
47+
#[tokio::test]
48+
pub async fn insert_with_literal_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, NULL");
58+
insert(&sql, &[&id]).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
@@ -1,2 +1,4 @@
1+
mod insert_with_literal;
2+
mod insert_with_null_literal;
13
mod insert_with_null_param;
24
mod insert_with_param;

0 commit comments

Comments
 (0)