Skip to content

Commit ac823dc

Browse files
committed
test(sqlx): improve equality test error reporting
1 parent 902387d commit ac823dc

File tree

1 file changed

+85
-55
lines changed

1 file changed

+85
-55
lines changed

tests/sqlx/tests/equality_tests.rs

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,291 +3,321 @@
33
//! Converted from src/operators/=_test.sql
44
//! Tests EQL equality operators with encrypted data (HMAC and Blake3 indexes)
55
6+
use anyhow::{Context, Result};
67
use eql_tests::QueryAssertion;
78
use sqlx::{PgPool, Row};
89

910
/// Helper to execute create_encrypted_json SQL function with specific indexes
1011
/// Uses variadic form: create_encrypted_json(id, index1, index2, ...)
11-
async fn create_encrypted_json_with_index(pool: &PgPool, id: i32, index_type: &str) -> String {
12+
async fn create_encrypted_json_with_index(
13+
pool: &PgPool,
14+
id: i32,
15+
index_type: &str,
16+
) -> Result<String> {
1217
let sql = format!(
1318
"SELECT create_encrypted_json({}, '{}')::text",
1419
id, index_type
1520
);
1621

17-
let row = sqlx::query(&sql).fetch_one(pool).await.unwrap_or_else(|e| {
18-
panic!(
19-
"Failed to create encrypted JSON with id={}, index_type='{}': {}",
20-
id, index_type, e
21-
)
22-
});
22+
let row = sqlx::query(&sql)
23+
.fetch_one(pool)
24+
.await
25+
.with_context(|| format!("fetching create_encrypted_json({}, '{}')", id, index_type))?;
2326

24-
let result: Option<String> = row.try_get(0).unwrap_or_else(|e| {
25-
panic!(
26-
"Failed to get result from create_encrypted_json(id={}, index_type='{}'): {}",
27-
id, index_type, e
27+
let result: Option<String> = row.try_get(0).with_context(|| {
28+
format!(
29+
"extracting text column for id={}, index_type='{}'",
30+
id, index_type
2831
)
29-
});
32+
})?;
3033

31-
result.unwrap_or_else(|| {
32-
panic!(
34+
result.with_context(|| {
35+
format!(
3336
"create_encrypted_json returned NULL for id={}, index_type='{}'",
3437
id, index_type
3538
)
3639
})
3740
}
3841

42+
async fn fetch_text_column(pool: &PgPool, sql: &str) -> Result<String> {
43+
let row = sqlx::query(sql).fetch_one(pool).await.with_context(|| {
44+
format!("executing query for text result: {}", sql)
45+
})?;
46+
47+
row.try_get(0).with_context(|| format!("extracting text column for query: {}", sql))
48+
}
49+
3950
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
40-
async fn equality_operator_finds_matching_record_hmac(pool: PgPool) {
51+
async fn equality_operator_finds_matching_record_hmac(pool: PgPool) -> Result<()> {
4152
// Test: eql_v2_encrypted = eql_v2_encrypted with HMAC index
4253
// Original SQL line 10-32 in src/operators/=_test.sql
4354

44-
let encrypted = create_encrypted_json_with_index(&pool, 1, "hm").await;
55+
let encrypted = create_encrypted_json_with_index(&pool, 1, "hm").await?;
4556

4657
let sql = format!(
4758
"SELECT e FROM encrypted WHERE e = '{}'::eql_v2_encrypted",
4859
encrypted
4960
);
5061

5162
QueryAssertion::new(&pool, &sql).returns_rows().await;
63+
64+
Ok(())
5265
}
5366

5467
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
55-
async fn equality_operator_returns_empty_for_no_match_hmac(pool: PgPool) {
68+
async fn equality_operator_returns_empty_for_no_match_hmac(pool: PgPool) -> Result<()> {
5669
// Test: equality returns no results for non-existent record
5770
// Original SQL line 25-29 in src/operators/=_test.sql
5871
// Note: Using id=4 instead of 91347 to ensure ore data exists (start=40 is within ore range 1-99)
5972
// The important part is that id=4 doesn't exist in the fixture data (only 1, 2, 3)
6073

61-
let encrypted = create_encrypted_json_with_index(&pool, 4, "hm").await;
74+
let encrypted = create_encrypted_json_with_index(&pool, 4, "hm").await?;
6275

6376
let sql = format!(
6477
"SELECT e FROM encrypted WHERE e = '{}'::eql_v2_encrypted",
6578
encrypted
6679
);
6780

6881
QueryAssertion::new(&pool, &sql).count(0).await;
82+
83+
Ok(())
6984
}
7085

7186
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
72-
async fn equality_operator_finds_matching_record_blake3(pool: PgPool) {
87+
async fn equality_operator_finds_matching_record_blake3(pool: PgPool) -> Result<()> {
7388
// Test: eql_v2_encrypted = eql_v2_encrypted with Blake3 index
7489
// Original SQL line 105-127 in src/operators/=_test.sql
7590

76-
let encrypted = create_encrypted_json_with_index(&pool, 1, "b3").await;
91+
let encrypted = create_encrypted_json_with_index(&pool, 1, "b3").await?;
7792

7893
let sql = format!(
7994
"SELECT e FROM encrypted WHERE e = '{}'::eql_v2_encrypted",
8095
encrypted
8196
);
8297

8398
QueryAssertion::new(&pool, &sql).returns_rows().await;
99+
100+
Ok(())
84101
}
85102

86103
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
87-
async fn equality_operator_returns_empty_for_no_match_blake3(pool: PgPool) {
104+
async fn equality_operator_returns_empty_for_no_match_blake3(pool: PgPool) -> Result<()> {
88105
// Test: equality returns no results for non-existent record with Blake3
89106
// Original SQL line 120-124 in src/operators/=_test.sql
90107
// Note: Using id=4 instead of 91347 to ensure ore data exists
91108
// The important part is that id=4 doesn't exist in the fixture data (only 1, 2, 3)
92109

93-
let encrypted = create_encrypted_json_with_index(&pool, 4, "b3").await;
110+
let encrypted = create_encrypted_json_with_index(&pool, 4, "b3").await?;
94111

95112
let sql = format!(
96113
"SELECT e FROM encrypted WHERE e = '{}'::eql_v2_encrypted",
97114
encrypted
98115
);
99116

100117
QueryAssertion::new(&pool, &sql).count(0).await;
118+
119+
Ok(())
101120
}
102121

103122
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
104-
async fn eq_function_finds_matching_record_hmac(pool: PgPool) {
123+
async fn eq_function_finds_matching_record_hmac(pool: PgPool) -> Result<()> {
105124
// Test: eql_v2.eq() function with HMAC index
106125
// Original SQL line 38-59 in src/operators/=_test.sql
107126
// Uses create_encrypted_json(id)::jsonb-'ob' to get encrypted data without ORE field
108127

109128
// Call SQL function to create encrypted JSON and remove 'ob' field
110129
// Cast to eql_v2_encrypted first, then to text to get tuple format
111130
let sql_create = "SELECT ((create_encrypted_json(1)::jsonb - 'ob')::eql_v2_encrypted)::text";
112-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
113-
let encrypted: String = row.try_get(0).unwrap();
131+
let encrypted = fetch_text_column(&pool, sql_create).await?;
114132

115133
let sql = format!(
116134
"SELECT e FROM encrypted WHERE eql_v2.eq(e, '{}'::eql_v2_encrypted)",
117135
encrypted
118136
);
119137

120138
QueryAssertion::new(&pool, &sql).returns_rows().await;
139+
140+
Ok(())
121141
}
122142

123143
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
124-
async fn eq_function_finds_matching_record_blake3(pool: PgPool) {
144+
async fn eq_function_finds_matching_record_blake3(pool: PgPool) -> Result<()> {
125145
// Test: eql_v2.eq() function with Blake3 index
126146
// Original SQL line 135-156 in src/operators/=_test.sql
127147

128148
// Call SQL function to create encrypted JSON with Blake3 and remove 'ob' field
129149
let sql_create = "SELECT ((create_encrypted_json(1, 'b3')::jsonb - 'ob')::eql_v2_encrypted)::text";
130-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
131-
let encrypted: String = row.try_get(0).unwrap();
150+
let encrypted = fetch_text_column(&pool, sql_create).await?;
132151

133152
let sql = format!(
134153
"SELECT e FROM encrypted WHERE eql_v2.eq(e, '{}'::eql_v2_encrypted)",
135154
encrypted
136155
);
137156

138157
QueryAssertion::new(&pool, &sql).returns_rows().await;
158+
159+
Ok(())
139160
}
140161

141162
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
142-
async fn eq_function_returns_empty_for_no_match_blake3(pool: PgPool) {
163+
async fn eq_function_returns_empty_for_no_match_blake3(pool: PgPool) -> Result<()> {
143164
// Test: eql_v2.eq() returns no results for non-existent record with Blake3
144165
// Original SQL line 148-153 in src/operators/=_test.sql
145166

146167
let sql_create = "SELECT ((create_encrypted_json(4, 'b3')::jsonb - 'ob')::eql_v2_encrypted)::text";
147-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
148-
let encrypted: String = row.try_get(0).unwrap();
168+
let encrypted = fetch_text_column(&pool, sql_create).await?;
149169

150170
let sql = format!(
151171
"SELECT e FROM encrypted WHERE eql_v2.eq(e, '{}'::eql_v2_encrypted)",
152172
encrypted
153173
);
154174

155175
QueryAssertion::new(&pool, &sql).count(0).await;
176+
177+
Ok(())
156178
}
157179

158180
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
159-
async fn equality_operator_encrypted_equals_jsonb_hmac(pool: PgPool) {
181+
async fn equality_operator_encrypted_equals_jsonb_hmac(pool: PgPool) -> Result<()> {
160182
// Test: eql_v2_encrypted = jsonb with HMAC index
161183
// Original SQL line 65-94 in src/operators/=_test.sql
162184

163185
// Create encrypted JSON with HMAC, remove 'ob' field for comparison
164186
let sql_create = "SELECT (create_encrypted_json(1)::jsonb - 'ob')::text";
165-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
166-
let json_value: String = row.try_get(0).unwrap();
187+
let json_value = fetch_text_column(&pool, sql_create).await?;
167188

168189
let sql = format!(
169190
"SELECT e FROM encrypted WHERE e = '{}'::jsonb",
170191
json_value
171192
);
172193

173194
QueryAssertion::new(&pool, &sql).returns_rows().await;
195+
196+
Ok(())
174197
}
175198

176199
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
177-
async fn equality_operator_jsonb_equals_encrypted_hmac(pool: PgPool) {
200+
async fn equality_operator_jsonb_equals_encrypted_hmac(pool: PgPool) -> Result<()> {
178201
// Test: jsonb = eql_v2_encrypted with HMAC index (reverse direction)
179202
// Original SQL line 78-81 in src/operators/=_test.sql
180203

181204
let sql_create = "SELECT (create_encrypted_json(1)::jsonb - 'ob')::text";
182-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
183-
let json_value: String = row.try_get(0).unwrap();
205+
let json_value = fetch_text_column(&pool, sql_create).await?;
184206

185207
let sql = format!(
186208
"SELECT e FROM encrypted WHERE '{}'::jsonb = e",
187209
json_value
188210
);
189211

190212
QueryAssertion::new(&pool, &sql).returns_rows().await;
213+
214+
Ok(())
191215
}
192216

193217
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
194-
async fn equality_operator_encrypted_equals_jsonb_no_match_hmac(pool: PgPool) {
218+
async fn equality_operator_encrypted_equals_jsonb_no_match_hmac(pool: PgPool) -> Result<()> {
195219
// Test: eql_v2_encrypted = jsonb with no matching record
196220
// Original SQL line 83-87 in src/operators/=_test.sql
197221

198222
let sql_create = "SELECT (create_encrypted_json(4)::jsonb - 'ob')::text";
199-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
200-
let json_value: String = row.try_get(0).unwrap();
223+
let json_value = fetch_text_column(&pool, sql_create).await?;
201224

202225
let sql = format!(
203226
"SELECT e FROM encrypted WHERE e = '{}'::jsonb",
204227
json_value
205228
);
206229

207230
QueryAssertion::new(&pool, &sql).count(0).await;
231+
232+
Ok(())
208233
}
209234

210235
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
211-
async fn equality_operator_jsonb_equals_encrypted_no_match_hmac(pool: PgPool) {
236+
async fn equality_operator_jsonb_equals_encrypted_no_match_hmac(pool: PgPool) -> Result<()> {
212237
// Test: jsonb = eql_v2_encrypted with no matching record
213238
// Original SQL line 89-91 in src/operators/=_test.sql
214239

215240
let sql_create = "SELECT (create_encrypted_json(4)::jsonb - 'ob')::text";
216-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
217-
let json_value: String = row.try_get(0).unwrap();
241+
let json_value = fetch_text_column(&pool, sql_create).await?;
218242

219243
let sql = format!(
220244
"SELECT e FROM encrypted WHERE '{}'::jsonb = e",
221245
json_value
222246
);
223247

224248
QueryAssertion::new(&pool, &sql).count(0).await;
249+
250+
Ok(())
225251
}
226252

227253
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
228-
async fn equality_operator_encrypted_equals_jsonb_blake3(pool: PgPool) {
254+
async fn equality_operator_encrypted_equals_jsonb_blake3(pool: PgPool) -> Result<()> {
229255
// Test: eql_v2_encrypted = jsonb with Blake3 index
230256
// Original SQL line 164-193 in src/operators/=_test.sql
231257

232258
let sql_create = "SELECT create_encrypted_json(1, 'b3')::jsonb::text";
233-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
234-
let json_value: String = row.try_get(0).unwrap();
259+
let json_value = fetch_text_column(&pool, sql_create).await?;
235260

236261
let sql = format!(
237262
"SELECT e FROM encrypted WHERE e = '{}'::jsonb",
238263
json_value
239264
);
240265

241266
QueryAssertion::new(&pool, &sql).returns_rows().await;
267+
268+
Ok(())
242269
}
243270

244271
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
245-
async fn equality_operator_jsonb_equals_encrypted_blake3(pool: PgPool) {
272+
async fn equality_operator_jsonb_equals_encrypted_blake3(pool: PgPool) -> Result<()> {
246273
// Test: jsonb = eql_v2_encrypted with Blake3 index (reverse direction)
247274
// Original SQL line 177-180 in src/operators/=_test.sql
248275

249276
let sql_create = "SELECT create_encrypted_json(1, 'b3')::jsonb::text";
250-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
251-
let json_value: String = row.try_get(0).unwrap();
277+
let json_value = fetch_text_column(&pool, sql_create).await?;
252278

253279
let sql = format!(
254280
"SELECT e FROM encrypted WHERE '{}'::jsonb = e",
255281
json_value
256282
);
257283

258284
QueryAssertion::new(&pool, &sql).returns_rows().await;
285+
286+
Ok(())
259287
}
260288

261289
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
262-
async fn equality_operator_encrypted_equals_jsonb_no_match_blake3(pool: PgPool) {
290+
async fn equality_operator_encrypted_equals_jsonb_no_match_blake3(pool: PgPool) -> Result<()> {
263291
// Test: eql_v2_encrypted = jsonb with no matching record (Blake3)
264292
// Original SQL line 184-187 in src/operators/=_test.sql
265293

266294
let sql_create = "SELECT create_encrypted_json(4, 'b3')::jsonb::text";
267-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
268-
let json_value: String = row.try_get(0).unwrap();
295+
let json_value = fetch_text_column(&pool, sql_create).await?;
269296

270297
let sql = format!(
271298
"SELECT e FROM encrypted WHERE e = '{}'::jsonb",
272299
json_value
273300
);
274301

275302
QueryAssertion::new(&pool, &sql).count(0).await;
303+
304+
Ok(())
276305
}
277306

278307
#[sqlx::test(fixtures(path = "../fixtures", scripts("encrypted_json")))]
279-
async fn equality_operator_jsonb_equals_encrypted_no_match_blake3(pool: PgPool) {
308+
async fn equality_operator_jsonb_equals_encrypted_no_match_blake3(pool: PgPool) -> Result<()> {
280309
// Test: jsonb = eql_v2_encrypted with no matching record (Blake3)
281310
// Original SQL line 188-191 in src/operators/=_test.sql
282311

283312
let sql_create = "SELECT create_encrypted_json(4, 'b3')::jsonb::text";
284-
let row = sqlx::query(sql_create).fetch_one(&pool).await.unwrap();
285-
let json_value: String = row.try_get(0).unwrap();
313+
let json_value = fetch_text_column(&pool, sql_create).await?;
286314

287315
let sql = format!(
288316
"SELECT e FROM encrypted WHERE '{}'::jsonb = e",
289317
json_value
290318
);
291319

292320
QueryAssertion::new(&pool, &sql).count(0).await;
321+
322+
Ok(())
293323
}

0 commit comments

Comments
 (0)