Skip to content

Commit b12ad8f

Browse files
committed
Add test to verify logged SQL matches executed statement
1 parent 39e36b9 commit b12ad8f

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl Client {
332332
///
333333
/// - **Input**: `&str` - the raw SQL query to be executed
334334
/// - **Output**: [`Query`] - the query builder that executes the query
335-
///
335+
///
336336
pub fn query_raw(&self, query: &str) -> query::Query {
337337
query::Query::raw(self, query)
338338
}

tests/it/query_raw.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn fetch_with_single_field_struct() {
6161
.await
6262
.unwrap();
6363

64-
// Test raw query with struct fetching
64+
// Test raw query with struct fetching
6565
let sql = "SELECT name FROM test_users ORDER BY name";
6666

6767
let mut cursor = client.query_raw(sql).fetch::<PersonName<'_>>().unwrap();
@@ -92,7 +92,7 @@ async fn fetch_with_multi_field_struct() {
9292
.await
9393
.unwrap();
9494

95-
// Test raw query with multi-field struct
95+
// Test raw query with multi-field struct
9696
let sql = "SELECT name, age FROM test_persons ORDER BY age";
9797

9898
let mut cursor = client.query_raw(sql).fetch::<PersonInfo>().unwrap();
@@ -279,21 +279,42 @@ async fn complex_sql_with_question_marks() {
279279
}
280280

281281
#[tokio::test]
282-
async fn query_raw_preserves_exact_sql() {
282+
async fn query_matches_log() {
283+
use uuid::Uuid;
284+
285+
// setup
283286
let client = prepare_database!();
284-
//check client
287+
let query_id = Uuid::new_v4().to_string(); // unique per run
288+
let sql = "SELECT 1 WHERE 'x?' = 'x?'"; // raw statement to verify
285289

286-
// Test that raw query preserves the exact SQL including whitespace and formatting
287-
let sql = "SELECT 1 WHERE 'test?' = 'test?' ";
290+
// execute with explicit query_id
291+
client
292+
.query_raw(sql)
293+
.with_option("query_id", &query_id)
294+
.execute()
295+
.await
296+
.expect("executing raw SQL failed");
288297

289-
let result = client.query_raw(sql).fetch_bytes("TSV").unwrap();
298+
crate::flush_query_log(&client).await;
290299

291-
let mut data = Vec::new();
292-
let mut cursor = result;
293-
while let Some(chunk) = cursor.next().await.unwrap() {
294-
data.extend_from_slice(&chunk);
295-
}
296-
let response = String::from_utf8(data).unwrap();
300+
// read log row *inline*
301+
let log_sql = format!(
302+
"SELECT query \
303+
FROM system.query_log \
304+
WHERE query_id = '{}' LIMIT 1",
305+
query_id
306+
);
297307

298-
assert_eq!(response.trim(), "1");
308+
let logged_sql: String = client
309+
.query_raw(&log_sql)
310+
.fetch_one()
311+
.await
312+
.expect("log entry not found");
313+
314+
// assertion
315+
assert_eq!(
316+
logged_sql.trim(),
317+
sql.trim(),
318+
"Logged SQL differs from the statement sent"
319+
);
299320
}

0 commit comments

Comments
 (0)