File: test-attr.rs

package info (click to toggle)
rust-sqlx 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,744 kB
  • sloc: sql: 335; python: 268; sh: 71; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// The no-arg variant is covered by other tests already.

use sqlx::{Row, SqlitePool};

const MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("tests/sqlite/migrations");

#[sqlx::test]
async fn it_gets_a_pool(pool: SqlitePool) -> sqlx::Result<()> {
    let mut conn = pool.acquire().await?;

    // https://www.sqlite.org/pragma.html#pragma_database_list
    let db = sqlx::query("PRAGMA database_list")
        .fetch_one(&mut *conn)
        .await?;

    let db_name = db.get::<String, _>(2);

    assert!(
        db_name.ends_with("target/sqlx/test-dbs/sqlite_test_attr/it_gets_a_pool.sqlite"),
        "db_name: {:?}",
        db_name
    );

    Ok(())
}

// This should apply migrations and then `fixtures/users.sql`
#[sqlx::test(migrations = "tests/sqlite/migrations", fixtures("users"))]
async fn it_gets_users(pool: SqlitePool) -> sqlx::Result<()> {
    let usernames: Vec<String> =
        sqlx::query_scalar(r#"SELECT username FROM "user" ORDER BY username"#)
            .fetch_all(&pool)
            .await?;

    assert_eq!(usernames, ["alice", "bob"]);

    let post_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM post)")
        .fetch_one(&pool)
        .await?;

    assert!(!post_exists);

    let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
        .fetch_one(&pool)
        .await?;

    assert!(!comment_exists);

    Ok(())
}

#[sqlx::test(migrations = "tests/sqlite/migrations", fixtures("users", "posts"))]
async fn it_gets_posts(pool: SqlitePool) -> sqlx::Result<()> {
    let post_contents: Vec<String> =
        sqlx::query_scalar("SELECT content FROM post ORDER BY created_at")
            .fetch_all(&pool)
            .await?;

    assert_eq!(
        post_contents,
        [
            "This new computer is lightning-fast!",
            "@alice is a haxxor :("
        ]
    );

    let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
        .fetch_one(&pool)
        .await?;

    assert!(!comment_exists);

    Ok(())
}

// Try `migrator`
#[sqlx::test(migrator = "MIGRATOR", fixtures("users", "posts", "comments"))]
async fn it_gets_comments(pool: SqlitePool) -> sqlx::Result<()> {
    let post_1_comments: Vec<String> =
        sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at")
            .bind(&1)
            .fetch_all(&pool)
            .await?;

    assert_eq!(
        post_1_comments,
        ["lol bet ur still bad, 1v1 me", "you're on!"]
    );

    let post_2_comments: Vec<String> =
        sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at")
            .bind(&2)
            .fetch_all(&pool)
            .await?;

    assert_eq!(post_2_comments, ["lol you're just mad you lost :P"]);

    Ok(())
}

#[sqlx::test(
    migrations = "tests/sqlite/migrations",
    fixtures(path = "./fixtures", scripts("users", "posts"))
)]
async fn this_should_compile(_pool: SqlitePool) -> sqlx::Result<()> {
    Ok(())
}