Skip to content

Commit 07e44b1

Browse files
committed
test(sqlx): add selector-based ORE comparison tests
Add 13 Rust/SQLx test cases for selector-based ORE comparisons with index fallback: - Tests for <, >, <=, >= operators with e->'selector' syntax - Coverage for ore_cllw_u64_8 index type ($.n numeric selector) - Coverage for ore_cllw_var_8 index type ($.hello string selector) - Fallback behavior when requested index type missing Includes new helper function get_ste_vec_selector_term() to extract selector terms using SQL helper functions, matching the pattern from legacy SQL tests. These tests verify that ORE comparisons work correctly with selector extraction and gracefully fall back to JSONB literal comparison when the expected index type is not present.
1 parent fa9265b commit 07e44b1

File tree

3 files changed

+495
-2
lines changed

3 files changed

+495
-2
lines changed

tests/sqlx/src/helpers.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,88 @@ pub async fn get_ore_encrypted_as_jsonb(pool: &PgPool, id: i32) -> Result<String
8282

8383
result.with_context(|| format!("ore table returned NULL for id={}", id))
8484
}
85+
86+
/// Fetch STE vec encrypted value from ste_vec table
87+
///
88+
/// The ste_vec table is created by migration `003_install_ste_vec_data.sql`
89+
/// and contains 10 pre-seeded records (ids 1-10) with ore_cllw_u64_8 and ore_cllw_var_8 indexes.
90+
///
91+
/// Test data structure:
92+
/// - Records have selectors for $.hello (a7cea93975ed8c01f861ccb6bd082784) with ore_cllw_var_8
93+
/// - Records have selectors for $.n (2517068c0d1f9d4d41d2c666211f785e) with ore_cllw_u64_8
94+
pub async fn get_ste_vec_encrypted(pool: &PgPool, id: i32) -> Result<String> {
95+
let sql = format!("SELECT e::text FROM ste_vec WHERE id = {}", id);
96+
let row = sqlx::query(&sql)
97+
.fetch_one(pool)
98+
.await
99+
.with_context(|| format!("fetching ste_vec encrypted value for id={}", id))?;
100+
101+
let result: Option<String> = row
102+
.try_get(0)
103+
.with_context(|| format!("extracting text column for id={}", id))?;
104+
105+
result.with_context(|| format!("ste_vec table returned NULL for id={}", id))
106+
}
107+
108+
/// Extract selector term using SQL helper functions
109+
///
110+
/// Uses the get_numeric_ste_vec_*() helper functions to extract a selector term.
111+
/// This matches the SQL test pattern exactly.
112+
///
113+
/// # Arguments
114+
/// * `pool` - Database connection pool
115+
/// * `value` - Which STE vec value to use (10, 20, 30, or 42)
116+
/// * `selector` - Selector hash to extract (e.g., Selectors::N, Selectors::HELLO)
117+
///
118+
/// # Example
119+
/// ```ignore
120+
/// // Extract $.n selector from n=30 test data
121+
/// let term = get_ste_vec_selector_term(&pool, 30, Selectors::N).await?;
122+
/// ```
123+
pub async fn get_ste_vec_selector_term(
124+
pool: &PgPool,
125+
value: i32,
126+
selector: &str,
127+
) -> Result<String> {
128+
// Call the appropriate get_numeric_ste_vec_*() function
129+
let func_name = match value {
130+
10 => "get_numeric_ste_vec_10",
131+
20 => "get_numeric_ste_vec_20",
132+
30 => "get_numeric_ste_vec_30",
133+
42 => "get_numeric_ste_vec_42",
134+
_ => {
135+
return Err(anyhow::anyhow!(
136+
"Invalid value: {}. Must be 10, 20, 30, or 42",
137+
value
138+
))
139+
}
140+
};
141+
142+
// SQL equivalent: sv := get_numeric_ste_vec_30()::eql_v2_encrypted;
143+
// term := sv->'2517068c0d1f9d4d41d2c666211f785e'::text;
144+
let sql = format!(
145+
"SELECT ({}()::eql_v2_encrypted -> '{}'::text)::text",
146+
func_name, selector
147+
);
148+
149+
let row = sqlx::query(&sql).fetch_one(pool).await.with_context(|| {
150+
format!(
151+
"extracting selector '{}' from ste_vec value={}",
152+
selector, value
153+
)
154+
})?;
155+
156+
let result: Option<String> = row.try_get(0).with_context(|| {
157+
format!(
158+
"getting text column for selector '{}' from value={}",
159+
selector, value
160+
)
161+
})?;
162+
163+
result.with_context(|| {
164+
format!(
165+
"selector extraction returned NULL for selector='{}', value={}",
166+
selector, value
167+
)
168+
})
169+
}

tests/sqlx/src/lib.rs

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

1212
pub use assertions::QueryAssertion;
13-
pub use helpers::{get_encrypted_term, get_ore_encrypted, get_ore_encrypted_as_jsonb};
13+
pub use helpers::{
14+
get_encrypted_term, get_ore_encrypted, get_ore_encrypted_as_jsonb, get_ste_vec_encrypted,
15+
get_ste_vec_selector_term,
16+
};
1417
pub use index_types as IndexTypes;
1518
pub use selectors::Selectors;
1619

0 commit comments

Comments
 (0)