-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtest-attr.rs
More file actions
201 lines (161 loc) · 5.77 KB
/
test-attr.rs
File metadata and controls
201 lines (161 loc) · 5.77 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// The no-arg variant is covered by other tests already.
use sqlx::PgPool;
const MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("tests/postgres/migrations");
#[sqlx::test]
async fn it_gets_a_pool(pool: PgPool) -> sqlx::Result<()> {
let mut conn = pool.acquire().await?;
let db_name: String = sqlx::query_scalar("SELECT current_database()")
.fetch_one(&mut *conn)
.await?;
assert!(db_name.starts_with("_sqlx_test"), "dbname: {db_name:?}");
Ok(())
}
// This should apply migrations and then `fixtures/users.sql`
#[sqlx::test(migrations = "tests/postgres/migrations", fixtures("users"))]
async fn it_gets_users(pool: PgPool) -> 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(())
}
// This should apply migrations and then fixtures `fixtures/users.sql` and `fixtures/posts.sql`
#[sqlx::test(migrations = "tests/postgres/migrations", fixtures("users", "posts"))]
async fn it_gets_posts(pool: PgPool) -> 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(())
}
// This should apply migrations and then `../fixtures/postgres/users.sql` and `../fixtures/postgres/posts.sql`
#[sqlx::test(
migrations = "tests/postgres/migrations",
fixtures("../fixtures/postgres/users.sql", "../fixtures/postgres/posts.sql")
)]
async fn it_gets_posts_explicit_fixtures_path(pool: PgPool) -> 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(())
}
// This should apply migrations and then `../fixtures/postgres/users.sql` and `fixtures/posts.sql`
#[sqlx::test(
migrations = "tests/postgres/migrations",
fixtures("../fixtures/postgres/users.sql"),
fixtures("posts")
)]
async fn it_gets_posts_mixed_fixtures_path(pool: PgPool) -> 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(())
}
// This should apply migrations and then `../fixtures/postgres/users.sql` and `../fixtures/postgres/posts.sql`
#[sqlx::test(
migrations = "tests/postgres/migrations",
fixtures("../fixtures/postgres/users.sql", "../fixtures/postgres/posts.sql")
)]
async fn it_gets_posts_custom_relative_fixtures_path(pool: PgPool) -> 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: PgPool) -> sqlx::Result<()> {
let post_1_comments: Vec<String> = sqlx::query_scalar(
"SELECT content FROM comment WHERE post_id = $1::uuid ORDER BY created_at",
)
.bind("252c1d98-a9b0-4f18-8298-e59058bdfe16")
.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 = $1::uuid ORDER BY created_at",
)
.bind("844265f7-2472-4689-9a2e-b21f40dbf401")
.fetch_all(&pool)
.await?;
assert_eq!(post_2_comments, ["lol you're just mad you lost :P"]);
Ok(())
}
#[sqlx::test(
migrations = "tests/postgres/migrations",
fixtures(path = "../fixtures/postgres", scripts("users", "posts"))
)]
async fn this_should_compile(_pool: PgPool) -> sqlx::Result<()> {
Ok(())
}
macro_rules! macro_using_test {
($migrations: literal) => {
#[sqlx::test(
migrations = $migrations,
fixtures(path = "../fixtures/postgres", scripts("users", "posts"))
)]
async fn macro_using_macro(_pool: PgPool) -> sqlx::Result<()> {
Ok(())
}
};
}
macro_using_test!("tests/postgres/migrations");