Debian Codegraph @

tests/postgres/test-attr.rs

raw symbols SCIP
1 // The no-arg variant is covered by other tests already.
2
3 use sqlx::PgPool;
4
5 const MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("tests/postgres/migrations");
6
7 #[sqlx::test]
8 async fn it_gets_a_pool(pool: PgPool) -> sqlx::Result<()> {
9 let mut conn = pool.acquire().await?;
10
11 let db_name: String = sqlx::query_scalar("SELECT current_database()")
12 .fetch_one(&mut *conn)
13 .await?;
14
15 assert!(db_name.starts_with("_sqlx_test"), "dbname: {db_name:?}");
16
17 Ok(())
18 }
19
20 // This should apply migrations and then `fixtures/users.sql`
21 #[sqlx::test(migrations = "tests/postgres/migrations", fixtures("users"))]
22 async fn it_gets_users(pool: PgPool) -> sqlx::Result<()> {
23 let usernames: Vec<String> =
24 sqlx::query_scalar(r#"SELECT username FROM "user" ORDER BY username"#)
25 .fetch_all(&pool)
26 .await?;
27
28 assert_eq!(usernames, ["alice", "bob"]);
29
30 let post_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM post)")
31 .fetch_one(&pool)
32 .await?;
33
34 assert!(!post_exists);
35
36 let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
37 .fetch_one(&pool)
38 .await?;
39
40 assert!(!comment_exists);
41
42 Ok(())
43 }
44
45 // This should apply migrations and then fixtures `fixtures/users.sql` and `fixtures/posts.sql`
46 #[sqlx::test(migrations = "tests/postgres/migrations", fixtures("users", "posts"))]
47 async fn it_gets_posts(pool: PgPool) -> sqlx::Result<()> {
48 let post_contents: Vec<String> =
49 sqlx::query_scalar("SELECT content FROM post ORDER BY created_at")
50 .fetch_all(&pool)
51 .await?;
52
53 assert_eq!(
54 post_contents,
55 [
56 "This new computer is lightning-fast!",
57 "@alice is a haxxor :("
58 ]
59 );
60
61 let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
62 .fetch_one(&pool)
63 .await?;
64
65 assert!(!comment_exists);
66
67 Ok(())
68 }
69
70 // This should apply migrations and then `../fixtures/postgres/users.sql` and `../fixtures/postgres/posts.sql`
71 #[sqlx::test(
72 migrations = "tests/postgres/migrations",
73 fixtures("../fixtures/postgres/users.sql", "../fixtures/postgres/posts.sql")
74 )]
75 async fn it_gets_posts_explicit_fixtures_path(pool: PgPool) -> sqlx::Result<()> {
76 let post_contents: Vec<String> =
77 sqlx::query_scalar("SELECT content FROM post ORDER BY created_at")
78 .fetch_all(&pool)
79 .await?;
80
81 assert_eq!(
82 post_contents,
83 [
84 "This new computer is lightning-fast!",
85 "@alice is a haxxor :("
86 ]
87 );
88
89 let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
90 .fetch_one(&pool)
91 .await?;
92
93 assert!(!comment_exists);
94
95 Ok(())
96 }
97
98 // This should apply migrations and then `../fixtures/postgres/users.sql` and `fixtures/posts.sql`
99 #[sqlx::test(
100 migrations = "tests/postgres/migrations",
101 fixtures("../fixtures/postgres/users.sql"),
102 fixtures("posts")
103 )]
104 async fn it_gets_posts_mixed_fixtures_path(pool: PgPool) -> sqlx::Result<()> {
105 let post_contents: Vec<String> =
106 sqlx::query_scalar("SELECT content FROM post ORDER BY created_at")
107 .fetch_all(&pool)
108 .await?;
109
110 assert_eq!(
111 post_contents,
112 [
113 "This new computer is lightning-fast!",
114 "@alice is a haxxor :("
115 ]
116 );
117
118 let comment_exists: bool = sqlx::query_scalar("SELECT exists(SELECT 1 FROM comment)")
119 .fetch_one(&pool)
120 .await?;
121
122 assert!(!comment_exists);
123
124 Ok(())
125 }
126
127 // This should apply migrations and then `../fixtures/postgres/users.sql` and `../fixtures/postgres/posts.sql`
128 #[sqlx::test(
129 migrations = "tests/postgres/migrations",
130 fixtures("../fixtures/postgres/users.sql", "../fixtures/postgres/posts.sql")
131 )]
132 async fn it_gets_posts_custom_relative_fixtures_path(pool: PgPool) -> 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 // Try `migrator`
156 #[sqlx::test(migrator = "MIGRATOR", fixtures("users", "posts", "comments"))]
157 async fn it_gets_comments(pool: PgPool) -> sqlx::Result<()> {
158 let post_1_comments: Vec<String> = sqlx::query_scalar(
159 "SELECT content FROM comment WHERE post_id = $1::uuid ORDER BY created_at",
160 )
161 .bind(&"252c1d98-a9b0-4f18-8298-e59058bdfe16")
162 .fetch_all(&pool)
163 .await?;
164
165 assert_eq!(
166 post_1_comments,
167 ["lol bet ur still bad, 1v1 me", "you're on!"]
168 );
169
170 let post_2_comments: Vec<String> = sqlx::query_scalar(
171 "SELECT content FROM comment WHERE post_id = $1::uuid ORDER BY created_at",
172 )
173 .bind(&"844265f7-2472-4689-9a2e-b21f40dbf401")
174 .fetch_all(&pool)
175 .await?;
176
177 assert_eq!(post_2_comments, ["lol you're just mad you lost :P"]);
178
179 Ok(())
180 }
181
182 #[sqlx::test(
183 migrations = "tests/postgres/migrations",
184 fixtures(path = "../fixtures/postgres", scripts("users", "posts"))
185 )]
186 async fn this_should_compile(_pool: PgPool) -> sqlx::Result<()> {
187 Ok(())
188 }
189
190 macro_rules! macro_using_test {
191 ($migrations: literal) => {
192 #[sqlx::test(
193 migrations = $migrations,
194 fixtures(path = "../fixtures/postgres", scripts("users", "posts"))
195 )]
196 async fn macro_using_macro(_pool: PgPool) -> sqlx::Result<()> {
197 Ok(())
198 }
199 };
200 }
201 macro_using_test!("tests/postgres/migrations");