Skip to content

Commit 083bf9a

Browse files
committed
refactor(tests): consolidate duplicate helper functions
Move get_ore_encrypted_as_jsonb helper from ore_comparison_tests.rs and comparison_tests.rs to src/helpers.rs to eliminate duplication and provide centralized maintenance. Addresses FINAL_CODE_REVIEW.md recommendation #1 (P3).
1 parent 296fd2d commit 083bf9a

File tree

4 files changed

+47
-90
lines changed

4 files changed

+47
-90
lines changed

tests/sqlx/src/helpers.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ pub async fn get_ore_encrypted(pool: &PgPool, id: i32) -> Result<String> {
3939
pub async fn get_encrypted_term(pool: &PgPool, selector: &str) -> Result<String> {
4040
// Note: Must cast selector to ::text to disambiguate operator overload
4141
// The -> operator has multiple signatures (text, eql_v2_encrypted, integer)
42-
let sql = format!("SELECT (e -> '{}'::text)::text FROM encrypted LIMIT 1", selector);
42+
let sql = format!(
43+
"SELECT (e -> '{}'::text)::text FROM encrypted LIMIT 1",
44+
selector
45+
);
4346
let row = sqlx::query(&sql)
4447
.fetch_one(pool)
4548
.await
@@ -49,5 +52,33 @@ pub async fn get_encrypted_term(pool: &PgPool, selector: &str) -> Result<String>
4952
.try_get(0)
5053
.with_context(|| format!("getting text column for selector={}", selector))?;
5154

52-
result.with_context(|| format!("encrypted term extraction returned NULL for selector={}", selector))
55+
result.with_context(|| {
56+
format!(
57+
"encrypted term extraction returned NULL for selector={}",
58+
selector
59+
)
60+
})
61+
}
62+
63+
/// Fetch ORE encrypted value as JSONB for comparison
64+
///
65+
/// This creates a JSONB value from the ore table that can be used with JSONB comparison
66+
/// operators. The ore table values only contain {"ob": [...]}, so we merge in the required
67+
/// "i" (index metadata) and "v" (version) fields to create a valid eql_v2_encrypted structure.
68+
pub async fn get_ore_encrypted_as_jsonb(pool: &PgPool, id: i32) -> Result<String> {
69+
let sql = format!(
70+
"SELECT (e::jsonb || jsonb_build_object('i', jsonb_build_object('t', 'ore'), 'v', 2))::text FROM ore WHERE id = {}",
71+
id
72+
);
73+
74+
let row = sqlx::query(&sql)
75+
.fetch_one(pool)
76+
.await
77+
.with_context(|| format!("fetching ore encrypted as jsonb for id={}", id))?;
78+
79+
let result: Option<String> = row
80+
.try_get(0)
81+
.with_context(|| format!("extracting jsonb text for id={}", id))?;
82+
83+
result.with_context(|| format!("ore table returned NULL for id={}", id))
5384
}

tests/sqlx/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ pub mod index_types;
1010
pub mod selectors;
1111

1212
pub use assertions::QueryAssertion;
13-
pub use helpers::{get_encrypted_term, get_ore_encrypted};
13+
pub use helpers::{get_encrypted_term, get_ore_encrypted, get_ore_encrypted_as_jsonb};
1414
pub use index_types as IndexTypes;
1515
pub use selectors::Selectors;
1616

1717
/// Reset pg_stat_user_functions tracking before tests
1818
pub async fn reset_function_stats(pool: &PgPool) -> anyhow::Result<()> {
19-
sqlx::query("SELECT pg_stat_reset()")
20-
.execute(pool)
21-
.await?;
19+
sqlx::query("SELECT pg_stat_reset()").execute(pool).await?;
2220
Ok(())
2321
}

tests/sqlx/tests/comparison_tests.rs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,9 @@
44
//! Tests EQL comparison operators with ORE (Order-Revealing Encryption)
55
66
use anyhow::{Context, Result};
7-
use eql_tests::{get_ore_encrypted, QueryAssertion};
7+
use eql_tests::{get_ore_encrypted, get_ore_encrypted_as_jsonb, QueryAssertion};
88
use 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)]
3712
async 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;

tests/sqlx/tests/ore_comparison_tests.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,9 @@
44
//! and src/operators/<=_ore_cllw_var_8_test.sql
55
//! Tests ORE CLLW comparison operators
66
7-
use anyhow::{Context, Result};
8-
use eql_tests::{get_ore_encrypted, QueryAssertion};
9-
use sqlx::{PgPool, Row};
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-
}
7+
use anyhow::Result;
8+
use eql_tests::{get_ore_encrypted, get_ore_encrypted_as_jsonb, QueryAssertion};
9+
use sqlx::PgPool;
3310

3411
#[sqlx::test]
3512
async fn lte_operator_cllw_u64_8(pool: PgPool) -> Result<()> {

0 commit comments

Comments
 (0)