| 1 |
|
| 2 |
|
| 3 |
use sqlx::{Row, SqlitePool}; |
| 4 |
|
| 5 |
const MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("tests/sqlite/migrations"); |
| 6 |
|
| 7 |
#[sqlx::test] |
| 8 |
async fn it_gets_a_pool(pool: SqlitePool) -> sqlx::Result<()> { |
| 9 |
let mut conn = pool.acquire().await?; |
| 10 |
|
| 11 |
|
| 12 |
let db = sqlx::query("PRAGMA database_list") |
| 13 |
.fetch_one(&mut *conn) |
| 14 |
.await?; |
| 15 |
|
| 16 |
let db_name = db.get::<String, _>(2); |
| 17 |
|
| 18 |
assert!( |
| 19 |
db_name.ends_with("target/sqlx/test-dbs/sqlite_test_attr/it_gets_a_pool.sqlite"), |
| 20 |
"db_name: {:?}", |
| 21 |
db_name |
| 22 |
); |
| 23 |
|
| 24 |
Ok(()) |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[sqlx::test(migrations = "tests/sqlite/migrations", fixtures("users"))] |
| 29 |
async fn it_gets_users(pool: SqlitePool) -> sqlx::Result<()> { |
| 30 |
let usernames: Vec<String> = |
| 31 |
sqlx::query_scalar(r#"SELECT username FROM "user" ORDER BY username"#) |
| 32 |
.fetch_all(&pool) |
| 33 |
.await?; |
| 34 |
|
| 35 |
assert_eq!(usernames, ["alice", "bob"]); |
| 36 |
|
| 37 |
let post_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM post)") |
| 38 |
.fetch_one(&pool) |
| 39 |
.await?; |
| 40 |
|
| 41 |
assert!(!post_exists); |
| 42 |
|
| 43 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 44 |
.fetch_one(&pool) |
| 45 |
.await?; |
| 46 |
|
| 47 |
assert!(!comment_exists); |
| 48 |
|
| 49 |
Ok(()) |
| 50 |
} |
| 51 |
|
| 52 |
#[sqlx::test(migrations = "tests/sqlite/migrations", fixtures("users", "posts"))] |
| 53 |
async fn it_gets_posts(pool: SqlitePool) -> sqlx::Result<()> { |
| 54 |
let post_contents: Vec<String> = |
| 55 |
sqlx::query_scalar("SELECT content FROM post ORDER BY created_at") |
| 56 |
.fetch_all(&pool) |
| 57 |
.await?; |
| 58 |
|
| 59 |
assert_eq!( |
| 60 |
post_contents, |
| 61 |
[ |
| 62 |
"This new computer is lightning-fast!", |
| 63 |
"@alice is a haxxor :(" |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 68 |
.fetch_one(&pool) |
| 69 |
.await?; |
| 70 |
|
| 71 |
assert!(!comment_exists); |
| 72 |
|
| 73 |
Ok(()) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#[sqlx::test(migrator = "MIGRATOR", fixtures("users", "posts", "comments"))] |
| 78 |
async fn it_gets_comments(pool: SqlitePool) -> sqlx::Result<()> { |
| 79 |
let post_1_comments: Vec<String> = |
| 80 |
sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at") |
| 81 |
.bind(&1) |
| 82 |
.fetch_all(&pool) |
| 83 |
.await?; |
| 84 |
|
| 85 |
assert_eq!( |
| 86 |
post_1_comments, |
| 87 |
["lol bet ur still bad, 1v1 me", "you're on!"] |
| 88 |
); |
| 89 |
|
| 90 |
let post_2_comments: Vec<String> = |
| 91 |
sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at") |
| 92 |
.bind(&2) |
| 93 |
.fetch_all(&pool) |
| 94 |
.await?; |
| 95 |
|
| 96 |
assert_eq!(post_2_comments, ["lol you're just mad you lost :P"]); |
| 97 |
|
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
|
| 101 |
#[sqlx::test( |
| 102 |
migrations = "tests/sqlite/migrations", |
| 103 |
fixtures(path = "./fixtures", scripts("users", "posts")) |
| 104 |
)] |
| 105 |
async fn this_should_compile(_pool: SqlitePool) -> sqlx::Result<()> { |
| 106 |
Ok(()) |
| 107 |
} |