Skip to content

Commit 38c7e17

Browse files
committed
refactor(tests): address code review feedback for containment tests
- Extract get_encrypted_term helper to reduce duplication (32 lines → 1 line per test) - Clarify comment about expected count in contains_operator_count_matches - Add missing negative assertion test for asymmetric containment (term @> e) - Total: 7 tests now (was 6), covering all original SQL assertions - All tests compile successfully
1 parent 561d0f7 commit 38c7e17

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

tests/sqlx/src/helpers.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@ pub async fn get_ore_encrypted(pool: &PgPool, id: i32) -> Result<String> {
2222

2323
result.with_context(|| format!("ore table returned NULL for id={}", id))
2424
}
25+
26+
/// Extract encrypted term from encrypted table by selector
27+
///
28+
/// Extracts a field from the first record in the encrypted table using
29+
/// the provided selector hash. Used for containment operator tests.
30+
///
31+
/// # Arguments
32+
/// * `pool` - Database connection pool
33+
/// * `selector` - Selector hash for the field to extract (e.g., from Selectors constants)
34+
///
35+
/// # Example
36+
/// ```
37+
/// let term = get_encrypted_term(&pool, Selectors::HELLO).await?;
38+
/// ```
39+
pub async fn get_encrypted_term(pool: &PgPool, selector: &str) -> Result<String> {
40+
let sql = format!("SELECT (e -> '{}')::text FROM encrypted LIMIT 1", selector);
41+
let row = sqlx::query(&sql)
42+
.fetch_one(pool)
43+
.await
44+
.with_context(|| format!("extracting encrypted term for selector={}", selector))?;
45+
46+
let result: Option<String> = row
47+
.try_get(0)
48+
.with_context(|| format!("getting text column for selector={}", selector))?;
49+
50+
result.with_context(|| format!("encrypted term extraction returned NULL for selector={}", selector))
51+
}

tests/sqlx/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod index_types;
1010
pub mod selectors;
1111

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

tests/sqlx/tests/containment_tests.rs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//! Tests encrypted JSONB containment operations
55
66
use anyhow::Result;
7-
use eql_tests::{QueryAssertion, Selectors};
8-
use sqlx::{PgPool, Row};
7+
use eql_tests::{get_encrypted_term, QueryAssertion, Selectors};
8+
use sqlx::PgPool;
99

1010
// ============================================================================
1111
// Task 10: Containment Operators (@> and <@)
@@ -40,20 +40,30 @@ async fn contains_operator_with_extracted_term(pool: PgPool) -> Result<()> {
4040
Ok(())
4141
}
4242

43+
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
44+
async fn contains_operator_term_does_not_contain_full_value(pool: PgPool) -> Result<()> {
45+
// Test: term does NOT contain full encrypted value (asymmetric containment)
46+
// Original SQL lines 48-49 in src/operators/@>_test.sql
47+
// Verifies that while e @> term is true, term @> e is false
48+
49+
let sql = format!(
50+
"SELECT e FROM encrypted WHERE (e -> '{}') @> e LIMIT 1",
51+
Selectors::N
52+
);
53+
54+
// Should return 0 records - extracted term cannot contain the full encrypted value
55+
QueryAssertion::new(&pool, &sql).count(0).await;
56+
57+
Ok(())
58+
}
59+
4360
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
4461
async fn contains_operator_with_encrypted_term(pool: PgPool) -> Result<()> {
4562
// Test: e @> encrypted_term with encrypted selector
4663
// Original SQL lines 68-90 in src/operators/@>_test.sql
4764
// Uses encrypted test data with $.hello selector
4865

49-
// Get encrypted term by extracting $.hello from first record
50-
let sql_create = format!(
51-
"SELECT (e -> '{}')::text FROM encrypted LIMIT 1",
52-
Selectors::HELLO
53-
);
54-
let row = sqlx::query(&sql_create).fetch_one(&pool).await?;
55-
let term: Option<String> = row.try_get(0)?;
56-
let term = term.expect("Should extract encrypted term");
66+
let term = get_encrypted_term(&pool, Selectors::HELLO).await?;
5767

5868
let sql = format!(
5969
"SELECT e FROM encrypted WHERE e @> '{}'::eql_v2_encrypted",
@@ -72,21 +82,15 @@ async fn contains_operator_count_matches(pool: PgPool) -> Result<()> {
7282
// Original SQL lines 84-87 in src/operators/@>_test.sql
7383
// Verifies count of records containing the term
7484

75-
// Get encrypted term for $.hello
76-
let sql_create = format!(
77-
"SELECT (e -> '{}')::text FROM encrypted LIMIT 1",
78-
Selectors::HELLO
79-
);
80-
let row = sqlx::query(&sql_create).fetch_one(&pool).await?;
81-
let term: Option<String> = row.try_get(0)?;
82-
let term = term.expect("Should extract encrypted term");
85+
let term = get_encrypted_term(&pool, Selectors::HELLO).await?;
8386

8487
let sql = format!(
8588
"SELECT e FROM encrypted WHERE e @> '{}'::eql_v2_encrypted",
8689
term
8790
);
8891

89-
// All 3 records in encrypted_json fixture have $.hello field
92+
// Expects 1 match: containment checks the specific encrypted term value,
93+
// not just the presence of the $.hello field
9094
QueryAssertion::new(&pool, &sql).count(1).await;
9195

9296
Ok(())
@@ -98,14 +102,7 @@ async fn contained_by_operator_with_encrypted_term(pool: PgPool) -> Result<()> {
98102
// Original SQL lines 19-41 in src/operators/<@_test.sql
99103
// Tests that extracted term is contained by the original encrypted value
100104

101-
// Get encrypted term for $.hello
102-
let sql_create = format!(
103-
"SELECT (e -> '{}')::text FROM encrypted LIMIT 1",
104-
Selectors::HELLO
105-
);
106-
let row = sqlx::query(&sql_create).fetch_one(&pool).await?;
107-
let term: Option<String> = row.try_get(0)?;
108-
let term = term.expect("Should extract encrypted term");
105+
let term = get_encrypted_term(&pool, Selectors::HELLO).await?;
109106

110107
let sql = format!(
111108
"SELECT e FROM encrypted WHERE '{}'::eql_v2_encrypted <@ e",
@@ -124,14 +121,7 @@ async fn contained_by_operator_count_matches(pool: PgPool) -> Result<()> {
124121
// Original SQL lines 35-38 in src/operators/<@_test.sql
125122
// Verifies count of records containing the term
126123

127-
// Get encrypted term for $.hello
128-
let sql_create = format!(
129-
"SELECT (e -> '{}')::text FROM encrypted LIMIT 1",
130-
Selectors::HELLO
131-
);
132-
let row = sqlx::query(&sql_create).fetch_one(&pool).await?;
133-
let term: Option<String> = row.try_get(0)?;
134-
let term = term.expect("Should extract encrypted term");
124+
let term = get_encrypted_term(&pool, Selectors::HELLO).await?;
135125

136126
let sql = format!(
137127
"SELECT e FROM encrypted WHERE '{}'::eql_v2_encrypted <@ e",

0 commit comments

Comments
 (0)