44//! Tests EQL comparison operators with ORE (Order-Revealing Encryption)
55
66use anyhow:: { Context , Result } ;
7- use eql_tests:: { get_ore_encrypted, QueryAssertion } ;
7+ use eql_tests:: { get_ore_encrypted, get_ore_encrypted_as_jsonb , QueryAssertion } ;
88use sqlx:: { PgPool , Row } ;
99
10-
11- /// Helper to fetch ORE encrypted value as JSONB for comparison
12- ///
13- /// This creates a JSONB value from the ore table that can be used with JSONB comparison
14- /// operators. The ore table values only contain {"ob": [...]}, so we merge in the required
15- /// "i" (index metadata) and "v" (version) fields to create a valid eql_v2_encrypted structure.
16- async fn get_ore_encrypted_as_jsonb ( pool : & PgPool , id : i32 ) -> Result < String > {
17- let sql = format ! (
18- "SELECT (e::jsonb || jsonb_build_object('i', jsonb_build_object('t', 'ore'), 'v', 2))::text FROM ore WHERE id = {}" ,
19- id
20- ) ;
21-
22- let row = sqlx:: query ( & sql)
23- . fetch_one ( pool)
24- . await
25- . with_context ( || format ! ( "fetching ore encrypted as jsonb for id={}" , id) ) ?;
26-
27- let result: Option < String > = row
28- . try_get ( 0 )
29- . with_context ( || format ! ( "extracting jsonb text for id={}" , id) ) ?;
30-
31- result. with_context ( || format ! ( "ore table returned NULL for id={}" , id) )
32- }
33-
34-
3510/// Helper to execute create_encrypted_json SQL function
3611#[ allow( dead_code) ]
3712async fn create_encrypted_json_with_index (
@@ -114,10 +89,7 @@ async fn less_than_operator_encrypted_less_than_jsonb(pool: PgPool) -> Result<()
11489
11590 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
11691
117- let sql = format ! (
118- "SELECT id FROM ore WHERE e < '{}'::jsonb" ,
119- json_value
120- ) ;
92+ let sql = format ! ( "SELECT id FROM ore WHERE e < '{}'::jsonb" , json_value) ;
12193
12294 // Records with id < 42 should match (ids 1-41)
12395 QueryAssertion :: new ( & pool, & sql) . count ( 41 ) . await ;
@@ -132,10 +104,7 @@ async fn less_than_operator_jsonb_less_than_encrypted(pool: PgPool) -> Result<()
132104
133105 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
134106
135- let sql = format ! (
136- "SELECT id FROM ore WHERE '{}'::jsonb < e" ,
137- json_value
138- ) ;
107+ let sql = format ! ( "SELECT id FROM ore WHERE '{}'::jsonb < e" , json_value) ;
139108
140109 // jsonb(42) < e means e > 42, so 57 records (43-99)
141110 QueryAssertion :: new ( & pool, & sql) . count ( 57 ) . await ;
@@ -190,10 +159,7 @@ async fn greater_than_operator_encrypted_greater_than_jsonb(pool: PgPool) -> Res
190159
191160 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
192161
193- let sql = format ! (
194- "SELECT id FROM ore WHERE e > '{}'::jsonb" ,
195- json_value
196- ) ;
162+ let sql = format ! ( "SELECT id FROM ore WHERE e > '{}'::jsonb" , json_value) ;
197163
198164 // Records with id > 42 should match (ids 43-99 = 57 records)
199165 QueryAssertion :: new ( & pool, & sql) . count ( 57 ) . await ;
@@ -208,10 +174,7 @@ async fn greater_than_operator_jsonb_greater_than_encrypted(pool: PgPool) -> Res
208174
209175 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
210176
211- let sql = format ! (
212- "SELECT id FROM ore WHERE '{}'::jsonb > e" ,
213- json_value
214- ) ;
177+ let sql = format ! ( "SELECT id FROM ore WHERE '{}'::jsonb > e" , json_value) ;
215178
216179 // jsonb(42) > e means e < 42, so 41 records (1-41)
217180 QueryAssertion :: new ( & pool, & sql) . count ( 41 ) . await ;
@@ -267,10 +230,7 @@ async fn less_than_or_equal_with_jsonb(pool: PgPool) -> Result<()> {
267230
268231 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
269232
270- let sql = format ! (
271- "SELECT id FROM ore WHERE e <= '{}'::jsonb" ,
272- json_value
273- ) ;
233+ let sql = format ! ( "SELECT id FROM ore WHERE e <= '{}'::jsonb" , json_value) ;
274234
275235 QueryAssertion :: new ( & pool, & sql) . count ( 42 ) . await ;
276236
@@ -284,10 +244,7 @@ async fn less_than_or_equal_jsonb_lte_encrypted(pool: PgPool) -> Result<()> {
284244
285245 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
286246
287- let sql = format ! (
288- "SELECT id FROM ore WHERE '{}'::jsonb <= e" ,
289- json_value
290- ) ;
247+ let sql = format ! ( "SELECT id FROM ore WHERE '{}'::jsonb <= e" , json_value) ;
291248
292249 // jsonb(42) <= e means e >= 42, so 58 records (42-99)
293250 QueryAssertion :: new ( & pool, & sql) . count ( 58 ) . await ;
@@ -342,10 +299,7 @@ async fn greater_than_or_equal_with_jsonb(pool: PgPool) -> Result<()> {
342299
343300 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
344301
345- let sql = format ! (
346- "SELECT id FROM ore WHERE e >= '{}'::jsonb" ,
347- json_value
348- ) ;
302+ let sql = format ! ( "SELECT id FROM ore WHERE e >= '{}'::jsonb" , json_value) ;
349303
350304 QueryAssertion :: new ( & pool, & sql) . count ( 58 ) . await ;
351305
@@ -359,10 +313,7 @@ async fn greater_than_or_equal_jsonb_gte_encrypted(pool: PgPool) -> Result<()> {
359313
360314 let json_value = get_ore_encrypted_as_jsonb ( & pool, 42 ) . await ?;
361315
362- let sql = format ! (
363- "SELECT id FROM ore WHERE '{}'::jsonb >= e" ,
364- json_value
365- ) ;
316+ let sql = format ! ( "SELECT id FROM ore WHERE '{}'::jsonb >= e" , json_value) ;
366317
367318 // jsonb(42) >= e means e <= 42, so 42 records (1-42)
368319 QueryAssertion :: new ( & pool, & sql) . count ( 42 ) . await ;
0 commit comments