|
| 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 | +} |
0 commit comments