From 5ba20798809d616e3ce030f631681c95a5922a09 Mon Sep 17 00:00:00 2001 From: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:13:20 -0400 Subject: [PATCH] patch examples and add a no fuss quickstart snippet --- src/macros/test.md | 72 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/macros/test.md b/src/macros/test.md index 46e93f86ce..29b5bed33f 100644 --- a/src/macros/test.md +++ b/src/macros/test.md @@ -3,6 +3,70 @@ Mark an `async fn` as a test with SQLx support. The test will automatically be executed in the async runtime according to the chosen `runtime-{async-std, tokio}` feature. If more than one runtime feature is enabled, `runtime-tokio` is preferred. +TL;DR: +If you want the quickest introduction to sqlx (without setup or installs), you can use a built-in sqlite db, and the testing framework (which leverages `cargo test`): + +```rust +// in cargo.toml... +// sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-native-tls", "sqlite", "migrate"] } +// tokio = "1.40.0" + + +use sqlx::{Pool, Sqlite, Row}; + +#[sqlx::test] +async fn basic_test(pool: Pool) -> anyhow::Result<()> { + let mut conn = pool.acquire().await?; + + let a = sqlx::query("SELECT * FROM foo").execute(&mut *conn).await; + + // every time you run this there will be an empty db + assert!(a.is_err()); // should error with no table called foo + + let id = sqlx::query( + r#" + CREATE TABLE foo ( + contact_id INTEGER PRIMARY KEY, + first_name TEXT NOT NULL + ); + "#, + ) + .execute(&mut *conn) + .await? + .last_insert_rowid(); + + assert!(id == 0); + + let id = sqlx::query( + r#" + INSERT INTO foo (contact_id, first_name) + VALUES + (1, "hello"); + "#, + ) + .execute(&mut *conn) + .await? + .last_insert_rowid(); + + assert!(id == 1); + + let res = sqlx::query( + r#" + SELECT * FROM foo + WHERE contact_id=1 + "#, + ) + .fetch_one(&mut *conn) + .await?; + + // get() requires trait Row + assert!(&res.get::("first_name") == "hello"); + + Ok(()) +} + +``` + By default, this behaves identically to `#[tokio::test]`1 or `#[async_std::test]`: ```rust @@ -71,7 +135,7 @@ async fn basic_test(pool: PgPool) -> sqlx::Result<()> { let mut conn = pool.acquire().await?; let foo = sqlx::query("SELECT * FROM foo") - .fetch_one(&mut conn) + .fetch_one(&mut *conn) .await?; assert_eq!(foo.get::("bar"), "foobar!"); @@ -103,7 +167,7 @@ async fn basic_test(pool: PgPool) -> sqlx::Result<()> { let mut conn = pool.acquire().await?; let foo = sqlx::query("SELECT * FROM foo") - .fetch_one(&mut conn) + .fetch_one(&mut *conn) .await?; assert_eq!(foo.get::("bar"), "foobar!"); @@ -143,7 +207,7 @@ async fn basic_test(pool: PgPool) -> sqlx::Result<()> { let mut conn = pool.acquire().await?; let foo = sqlx::query("SELECT * FROM foo") - .fetch_one(&mut conn) + .fetch_one(&mut *conn) .await?; assert_eq!(foo.get::("bar"), "foobar!"); @@ -167,7 +231,7 @@ async fn basic_test(pool: PgPool) -> sqlx::Result<()> { conn.execute("CREATE TABLE foo(bar text)").await?; let foo = sqlx::query("SELECT * FROM foo") - .fetch_one(&mut conn) + .fetch_one(&mut *conn) .await?; assert_eq!(foo.get::("bar"), "foobar!");