| 1 |
|
| 2 |
|
| 3 |
use sqlx::MySqlPool; |
| 4 |
|
| 5 |
const MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("tests/mysql/migrations"); |
| 6 |
|
| 7 |
#[sqlx::test] |
| 8 |
async fn it_gets_a_pool(pool: MySqlPool) -> sqlx::Result<()> { |
| 9 |
let mut conn = pool.acquire().await?; |
| 10 |
|
| 11 |
let db_name: String = sqlx::query_scalar("select database()") |
| 12 |
.fetch_one(&mut *conn) |
| 13 |
.await?; |
| 14 |
|
| 15 |
assert!( |
| 16 |
db_name.starts_with("_sqlx_test_database_"), |
| 17 |
"db_name: {:?}", |
| 18 |
db_name |
| 19 |
); |
| 20 |
|
| 21 |
Ok(()) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
#[sqlx::test(migrations = "tests/mysql/migrations", fixtures("users"))] |
| 26 |
async fn it_gets_users(pool: MySqlPool) -> sqlx::Result<()> { |
| 27 |
let usernames: Vec<String> = |
| 28 |
sqlx::query_scalar(r#"SELECT username FROM user ORDER BY username"#) |
| 29 |
.fetch_all(&pool) |
| 30 |
.await?; |
| 31 |
|
| 32 |
assert_eq!(usernames, ["alice", "bob"]); |
| 33 |
|
| 34 |
let post_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM post)") |
| 35 |
.fetch_one(&pool) |
| 36 |
.await?; |
| 37 |
|
| 38 |
assert!(!post_exists); |
| 39 |
|
| 40 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 41 |
.fetch_one(&pool) |
| 42 |
.await?; |
| 43 |
|
| 44 |
assert!(!comment_exists); |
| 45 |
|
| 46 |
Ok(()) |
| 47 |
} |
| 48 |
|
| 49 |
#[sqlx::test(migrations = "tests/mysql/migrations", fixtures("users", "posts"))] |
| 50 |
async fn it_gets_posts(pool: MySqlPool) -> sqlx::Result<()> { |
| 51 |
let post_contents: Vec<String> = |
| 52 |
sqlx::query_scalar("SELECT content FROM post ORDER BY created_at") |
| 53 |
.fetch_all(&pool) |
| 54 |
.await?; |
| 55 |
|
| 56 |
assert_eq!( |
| 57 |
post_contents, |
| 58 |
[ |
| 59 |
"This new computer is lightning-fast!", |
| 60 |
"@alice is a haxxor :(" |
| 61 |
] |
| 62 |
); |
| 63 |
|
| 64 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 65 |
.fetch_one(&pool) |
| 66 |
.await?; |
| 67 |
|
| 68 |
assert!(!comment_exists); |
| 69 |
|
| 70 |
Ok(()) |
| 71 |
} |
| 72 |
|
| 73 |
#[sqlx::test( |
| 74 |
migrations = "tests/mysql/migrations", |
| 75 |
fixtures("../fixtures/mysql/users.sql", "../fixtures/mysql/posts.sql") |
| 76 |
)] |
| 77 |
async fn it_gets_posts_explicit_fixtures_path(pool: MySqlPool) -> sqlx::Result<()> { |
| 78 |
let post_contents: Vec<String> = |
| 79 |
sqlx::query_scalar("SELECT content FROM post ORDER BY created_at") |
| 80 |
.fetch_all(&pool) |
| 81 |
.await?; |
| 82 |
|
| 83 |
assert_eq!( |
| 84 |
post_contents, |
| 85 |
[ |
| 86 |
"This new computer is lightning-fast!", |
| 87 |
"@alice is a haxxor :(" |
| 88 |
] |
| 89 |
); |
| 90 |
|
| 91 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 92 |
.fetch_one(&pool) |
| 93 |
.await?; |
| 94 |
|
| 95 |
assert!(!comment_exists); |
| 96 |
|
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
#[sqlx::test( |
| 101 |
migrations = "tests/mysql/migrations", |
| 102 |
fixtures("../fixtures/mysql/users.sql"), |
| 103 |
fixtures("posts") |
| 104 |
)] |
| 105 |
async fn it_gets_posts_mixed_fixtures_path(pool: MySqlPool) -> sqlx::Result<()> { |
| 106 |
let post_contents: Vec<String> = |
| 107 |
sqlx::query_scalar("SELECT content FROM post ORDER BY created_at") |
| 108 |
.fetch_all(&pool) |
| 109 |
.await?; |
| 110 |
|
| 111 |
assert_eq!( |
| 112 |
post_contents, |
| 113 |
[ |
| 114 |
"This new computer is lightning-fast!", |
| 115 |
"@alice is a haxxor :(" |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 120 |
.fetch_one(&pool) |
| 121 |
.await?; |
| 122 |
|
| 123 |
assert!(!comment_exists); |
| 124 |
|
| 125 |
Ok(()) |
| 126 |
} |
| 127 |
|
| 128 |
#[sqlx::test( |
| 129 |
migrations = "tests/mysql/migrations", |
| 130 |
fixtures(path = "../fixtures/mysql", scripts("users", "posts")) |
| 131 |
)] |
| 132 |
async fn it_gets_posts_custom_relative_fixtures_path(pool: MySqlPool) -> sqlx::Result<()> { |
| 133 |
let post_contents: Vec<String> = |
| 134 |
sqlx::query_scalar("SELECT content FROM post ORDER BY created_at") |
| 135 |
.fetch_all(&pool) |
| 136 |
.await?; |
| 137 |
|
| 138 |
assert_eq!( |
| 139 |
post_contents, |
| 140 |
[ |
| 141 |
"This new computer is lightning-fast!", |
| 142 |
"@alice is a haxxor :(" |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)") |
| 147 |
.fetch_one(&pool) |
| 148 |
.await?; |
| 149 |
|
| 150 |
assert!(!comment_exists); |
| 151 |
|
| 152 |
Ok(()) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
#[sqlx::test(migrator = "MIGRATOR", fixtures("users", "posts", "comments"))] |
| 157 |
async fn it_gets_comments(pool: MySqlPool) -> sqlx::Result<()> { |
| 158 |
let post_1_comments: Vec<String> = |
| 159 |
sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at") |
| 160 |
.bind(&1) |
| 161 |
.fetch_all(&pool) |
| 162 |
.await?; |
| 163 |
|
| 164 |
assert_eq!( |
| 165 |
post_1_comments, |
| 166 |
["lol bet ur still bad, 1v1 me", "you're on!"] |
| 167 |
); |
| 168 |
|
| 169 |
let post_2_comments: Vec<String> = |
| 170 |
sqlx::query_scalar("SELECT content FROM comment WHERE post_id = ? ORDER BY created_at") |
| 171 |
.bind(&2) |
| 172 |
.fetch_all(&pool) |
| 173 |
.await?; |
| 174 |
|
| 175 |
assert_eq!(post_2_comments, ["lol you're just mad you lost :P"]); |
| 176 |
|
| 177 |
Ok(()) |
| 178 |
} |