Skip to content

Commit 33c53ce

Browse files
committed
refactor(sqlx): replace literal selectors with constants
Replace magic selector literals with Selectors constants: - Use Selectors::ARRAY_ROOT for $.a path - Use Selectors::ARRAY_ELEMENTS for $.a[*] path - Use Selectors::as_encrypted() helper for encrypted selectors Affected tests: - jsonb_path_query_first_with_array_selector - jsonb_path_query_first_filters_non_null - jsonb_path_query_with_array_selector_returns_single_result - jsonb_path_exists_with_array_selector - jsonb_array_elements_with_encrypted_selector - jsonb_array_elements_with_encrypted_selector_throws_for_non_array Improves code maintainability and self-documentation.
1 parent 37ed012 commit 33c53ce

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

tests/sqlx/tests/jsonb_tests.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ async fn jsonb_path_query_first_with_array_selector(pool: PgPool) {
211211
// Test: jsonb_path_query_first returns first element from array path
212212
// Original SQL line 135-160 in src/jsonb/functions_test.sql
213213

214-
let sql = "SELECT eql_v2.jsonb_path_query_first(e, '33743aed3ae636f6bf05cff11ac4b519') as e FROM encrypted";
214+
let sql = format!(
215+
"SELECT eql_v2.jsonb_path_query_first(e, '{}') as e FROM encrypted",
216+
Selectors::ARRAY_ROOT
217+
);
215218

216219
// Should return 4 total rows (3 from encrypted_json + 1 from array_data)
217220
QueryAssertion::new(&pool, sql).count(4).await;
@@ -222,7 +225,11 @@ async fn jsonb_path_query_first_filters_non_null(pool: PgPool) {
222225
// Test: jsonb_path_query_first can filter by non-null values
223226
// Original SQL line 331-333 in src/jsonb/functions_test.sql
224227

225-
let sql = "SELECT eql_v2.jsonb_path_query_first(e, '33743aed3ae636f6bf05cff11ac4b519') as e FROM encrypted WHERE eql_v2.jsonb_path_query_first(e, '33743aed3ae636f6bf05cff11ac4b519') IS NOT NULL";
228+
let sql = format!(
229+
"SELECT eql_v2.jsonb_path_query_first(e, '{}') as e FROM encrypted WHERE eql_v2.jsonb_path_query_first(e, '{}') IS NOT NULL",
230+
Selectors::ARRAY_ROOT,
231+
Selectors::ARRAY_ROOT
232+
);
226233

227234
// Should return only 1 row (the one with array data)
228235
QueryAssertion::new(&pool, sql).count(1).await;
@@ -233,7 +240,10 @@ async fn jsonb_path_query_with_array_selector_returns_single_result(pool: PgPool
233240
// Test: jsonb_path_query wraps arrays as single result
234241
// Original SQL line 254-274 in src/jsonb/functions_test.sql
235242

236-
let sql = "SELECT eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5') FROM encrypted";
243+
let sql = format!(
244+
"SELECT eql_v2.jsonb_path_query(e, '{}') FROM encrypted",
245+
Selectors::ARRAY_ELEMENTS
246+
);
237247

238248
// Array should be wrapped and returned as single element
239249
QueryAssertion::new(&pool, sql).count(1).await;
@@ -244,7 +254,10 @@ async fn jsonb_path_exists_with_array_selector(pool: PgPool) {
244254
// Test: jsonb_path_exists works with array selectors
245255
// Original SQL line 282-303 in src/jsonb/functions_test.sql
246256

247-
let sql = "SELECT eql_v2.jsonb_path_exists(e, 'f510853730e1c3dbd31b86963f029dd5') FROM encrypted";
257+
let sql = format!(
258+
"SELECT eql_v2.jsonb_path_exists(e, '{}') FROM encrypted",
259+
Selectors::ARRAY_ELEMENTS
260+
);
248261

249262
// Should return 4 rows (3 encrypted_json + 1 array_data)
250263
QueryAssertion::new(&pool, sql).count(4).await;
@@ -257,8 +270,11 @@ async fn jsonb_array_elements_with_encrypted_selector(pool: PgPool) {
257270
// Tests alternative API pattern using encrypted selector
258271

259272
// Create encrypted selector for array elements path
260-
let selector_sql = "SELECT '{\"s\": \"f510853730e1c3dbd31b86963f029dd5\"}'::jsonb::eql_v2_encrypted::text";
261-
let row = sqlx::query(selector_sql).fetch_one(&pool).await.unwrap();
273+
let selector_sql = format!(
274+
"SELECT '{}'::jsonb::eql_v2_encrypted::text",
275+
Selectors::as_encrypted(Selectors::ARRAY_ELEMENTS)
276+
);
277+
let row = sqlx::query(&selector_sql).fetch_one(&pool).await.unwrap();
262278
let encrypted_selector: String = row.try_get(0).unwrap();
263279

264280
let sql = format!(
@@ -278,8 +294,11 @@ async fn jsonb_array_elements_with_encrypted_selector_throws_for_non_array(pool:
278294
// Test: encrypted selector also validates array type
279295
// Original SQL line 61-63 in src/jsonb/functions_test.sql
280296

281-
let selector_sql = "SELECT '{\"s\": \"33743aed3ae636f6bf05cff11ac4b519\"}'::jsonb::eql_v2_encrypted::text";
282-
let row = sqlx::query(selector_sql).fetch_one(&pool).await.unwrap();
297+
let selector_sql = format!(
298+
"SELECT '{}'::jsonb::eql_v2_encrypted::text",
299+
Selectors::as_encrypted(Selectors::ARRAY_ROOT)
300+
);
301+
let row = sqlx::query(&selector_sql).fetch_one(&pool).await.unwrap();
283302
let encrypted_selector: String = row.try_get(0).unwrap();
284303

285304
let sql = format!(

0 commit comments

Comments
 (0)