Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] patch examples and add a no fuss quickstart snippet #3554

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions src/macros/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sqlite>) -> 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::<String, _>("first_name") == "hello");

Ok(())
}

```

By default, this behaves identically to `#[tokio::test]`<sup>1</sup> or `#[async_std::test]`:

```rust
Expand Down Expand Up @@ -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::<String, _>("bar"), "foobar!");
Expand Down Expand Up @@ -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::<String, _>("bar"), "foobar!");
Expand Down Expand Up @@ -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::<String, _>("bar"), "foobar!");
Expand All @@ -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::<String, _>("bar"), "foobar!");
Expand Down