@@ -9,6 +9,10 @@ mod tests {
9
9
10
10
macro_rules! test_insert_with_literal {
11
11
( $name: ident, $type: ident, $pg_type: ident) => {
12
+ test_insert_with_literal!( $name, $type, $pg_type, false ) ;
13
+ } ;
14
+
15
+ ( $name: ident, $type: ident, $pg_type: ident, $cast: expr) => {
12
16
#[ tokio:: test]
13
17
pub async fn $name( ) {
14
18
trace( ) ;
@@ -22,8 +26,14 @@ mod tests {
22
26
23
27
let expected = vec![ encrypted_val. clone( ) ] ;
24
28
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" ) ;
29
+ let cast_to_type: & str = if $cast {
30
+ & format!( "::{}" , stringify!( $pg_type) )
31
+ } else {
32
+ ""
33
+ } ;
34
+
35
+ let insert_sql = format!( "INSERT INTO encrypted (id, {encrypted_col}) VALUES ($1, '{encrypted_val}'{cast_to_type})" ) ;
36
+ let select_sql = format!( "SELECT {encrypted_col}{cast_to_type} FROM encrypted WHERE id = $1" ) ;
27
37
28
38
execute_query( & insert_sql, & [ & id] ) . await ;
29
39
let actual = query_by:: <$type>( & select_sql, & id) . await ;
@@ -36,6 +46,10 @@ mod tests {
36
46
37
47
macro_rules! test_insert_simple_query_with_literal {
38
48
( $name: ident, $type: ident, $pg_type: ident) => {
49
+ test_insert_simple_query_with_literal!( $name, $type, $pg_type, false ) ;
50
+ } ;
51
+
52
+ ( $name: ident, $type: ident, $pg_type: ident, $cast: expr) => {
39
53
#[ tokio:: test]
40
54
pub async fn $name( ) {
41
55
trace( ) ;
@@ -48,8 +62,14 @@ mod tests {
48
62
let encrypted_col = format!( "encrypted_{}" , stringify!( $pg_type) ) ;
49
63
let encrypted_val = crate :: value_for_type!( $type, random_limited( ) ) ;
50
64
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}" ) ;
65
+ let cast_to_type: & str = if $cast {
66
+ & format!( "::{}" , stringify!( $pg_type) )
67
+ } else {
68
+ ""
69
+ } ;
70
+
71
+ let insert_sql = format!( "INSERT INTO encrypted (id, {encrypted_col}) VALUES ({id}, '{encrypted_val}'{cast_to_type})" ) ;
72
+ let select_sql = format!( "SELECT {encrypted_col}{cast_to_type} FROM encrypted WHERE id = {id}" ) ;
53
73
54
74
55
75
let expected = vec![ encrypted_val] ;
@@ -69,7 +89,7 @@ mod tests {
69
89
test_insert_with_literal ! ( insert_with_literal_bool, bool , bool ) ;
70
90
test_insert_with_literal ! ( insert_with_literal_text, String , text) ;
71
91
test_insert_with_literal ! ( insert_with_literal_date, NaiveDate , date) ;
72
- test_insert_with_literal ! ( insert_with_literal_jsonb, Value , jsonb) ;
92
+ test_insert_with_literal ! ( insert_with_literal_jsonb, Value , jsonb, true ) ;
73
93
74
94
test_insert_simple_query_with_literal ! ( insert_simple_query_with_literal_int2, i16 , int2) ;
75
95
test_insert_simple_query_with_literal ! ( insert_simple_query_with_literal_int4, i32 , int4) ;
@@ -78,7 +98,12 @@ mod tests {
78
98
test_insert_simple_query_with_literal ! ( insert_simple_query_with_literal_bool, bool , bool ) ;
79
99
test_insert_simple_query_with_literal ! ( insert_simple_query_with_literal_text, String , text) ;
80
100
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) ;
101
+ test_insert_simple_query_with_literal ! (
102
+ insert_simple_query_with_literal_jsonb,
103
+ Value ,
104
+ jsonb,
105
+ true
106
+ ) ;
82
107
83
108
// -----------------------------------------------------------------
84
109
0 commit comments