Skip to content

Commit 6d3ed6b

Browse files
committed
feat(tests): add statement preparation tests for SQLx Any API
This commit introduces two new tests for the SQLx Any API: one for successfully preparing a valid SQL statement and another for handling errors when preparing an invalid statement. These tests enhance coverage and ensure the robustness of statement preparation functionality.
1 parent 8026918 commit 6d3ed6b

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

tests/any/any.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use sqlx_oldapi::any::AnyRow;
2-
use sqlx_oldapi::{Any, Connection, Decode, Executor, Row, Type};
2+
use sqlx_oldapi::{Any, Column, Connection, Decode, Executor, Row, Statement, Type};
33
use sqlx_test::new;
44

55
async fn get_val<T>(expr: &str) -> anyhow::Result<T>
@@ -307,3 +307,33 @@ async fn it_reports_rows_affected() -> anyhow::Result<()> {
307307

308308
Ok(())
309309
}
310+
311+
#[sqlx_macros::test]
312+
async fn it_prepares_statements() -> anyhow::Result<()> {
313+
let mut conn = new::<Any>().await?;
314+
315+
let stmt = conn.prepare("SELECT 42 AS answer").await?;
316+
317+
assert_eq!(stmt.columns().len(), 1);
318+
assert_eq!(stmt.columns()[0].name(), "answer");
319+
320+
let row = stmt.query().fetch_one(&mut conn).await?;
321+
let answer: i32 = row.try_get("answer")?;
322+
assert_eq!(answer, 42);
323+
324+
Ok(())
325+
}
326+
327+
#[sqlx_macros::test]
328+
async fn it_fails_to_prepare_invalid_statements() -> anyhow::Result<()> {
329+
let mut conn = new::<Any>().await?;
330+
331+
let result = conn.prepare("SELECT * FROM table_does_not_exist").await;
332+
333+
assert!(
334+
result.is_err(),
335+
"Expected error when preparing statement for non-existent table"
336+
);
337+
338+
Ok(())
339+
}

0 commit comments

Comments
 (0)