Skip to content

Commit dfc30d0

Browse files
authored
typosquat/test_util: Migrate user() fn to diesel-async (#10055)
1 parent bd29578 commit dfc30d0

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/typosquat/database.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,24 @@ mod tests {
182182
#[tokio::test]
183183
async fn top_crates() -> Result<(), Error> {
184184
let test_db = TestDatabase::new();
185-
let mut conn = test_db.connect();
186-
let mut async_conn = test_db.async_connect().await;
185+
let mut conn = test_db.async_connect().await;
187186

188187
// Set up two users.
189-
let user_a = faker::user(&mut conn, "a")?;
190-
let user_b = faker::user(&mut conn, "b")?;
188+
let user_a = faker::user(&mut conn, "a").await?;
189+
let user_b = faker::user(&mut conn, "b").await?;
191190

192191
// Set up three crates with various ownership schemes.
193-
let _top_a = faker::crate_and_version(&mut async_conn, "a", "Hello", &user_a, 2).await?;
192+
let _top_a = faker::crate_and_version(&mut conn, "a", "Hello", &user_a, 2).await?;
194193
let top_b =
195-
faker::crate_and_version(&mut async_conn, "b", "Yes, this is dog", &user_b, 1).await?;
196-
let not_top_c =
197-
faker::crate_and_version(&mut async_conn, "c", "Unpopular", &user_a, 0).await?;
194+
faker::crate_and_version(&mut conn, "b", "Yes, this is dog", &user_b, 1).await?;
195+
let not_top_c = faker::crate_and_version(&mut conn, "c", "Unpopular", &user_a, 0).await?;
198196

199197
// Let's set up a team that owns both b and c, but not a.
200-
let not_the_a_team = faker::team(&mut async_conn, "org", "team").await?;
201-
add_team_to_crate(&not_the_a_team, &top_b, &user_b, &mut async_conn).await?;
202-
add_team_to_crate(&not_the_a_team, &not_top_c, &user_b, &mut async_conn).await?;
198+
let not_the_a_team = faker::team(&mut conn, "org", "team").await?;
199+
add_team_to_crate(&not_the_a_team, &top_b, &user_b, &mut conn).await?;
200+
add_team_to_crate(&not_the_a_team, &not_top_c, &user_b, &mut conn).await?;
203201

204-
let top_crates = TopCrates::new(&mut async_conn, 2).await?;
202+
let top_crates = TopCrates::new(&mut conn, 2).await?;
205203

206204
// Let's ensure the top crates include what we expect (which is a and b, since we asked for
207205
// 2 crates and they're the most downloaded).
@@ -215,7 +213,7 @@ mod tests {
215213
assert!(!pkg_a.shared_authors(pkg_b.authors()));
216214

217215
// Now let's go get package c and pretend it's a new package.
218-
let pkg_c = Crate::from_name(&mut async_conn, "c").await?;
216+
let pkg_c = Crate::from_name(&mut conn, "c").await?;
219217

220218
// c _does_ have an author in common with a.
221219
assert!(pkg_a.shared_authors(pkg_c.authors()));

src/typosquat/test_util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use diesel::{prelude::*, PgConnection};
1+
use diesel::prelude::*;
2+
use diesel_async::RunQueryDsl;
23

34
use crate::tests::util::github::next_gh_id;
45
use crate::{
@@ -40,11 +41,12 @@ pub mod faker {
4041
Ok(team.create_or_update(conn).await?)
4142
}
4243

43-
pub fn user(conn: &mut PgConnection, login: &str) -> QueryResult<User> {
44+
pub async fn user(conn: &mut AsyncPgConnection, login: &str) -> QueryResult<User> {
4445
let user = NewUser::new(next_gh_id(), login, None, None, "token");
4546

4647
diesel::insert_into(users::table)
4748
.values(user)
4849
.get_result(conn)
50+
.await
4951
}
5052
}

src/worker/jobs/typosquat.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,19 @@ mod tests {
130130
async fn integration() -> anyhow::Result<()> {
131131
let emails = Emails::new_in_memory();
132132
let test_db = TestDatabase::new();
133-
let mut conn = test_db.connect();
134-
let mut async_conn = test_db.async_connect().await;
133+
let mut conn = test_db.async_connect().await;
135134

136135
// Set up a user and a popular crate to match against.
137-
let user = faker::user(&mut conn, "a")?;
138-
faker::crate_and_version(&mut async_conn, "my-crate", "It's awesome", &user, 100).await?;
136+
let user = faker::user(&mut conn, "a").await?;
137+
faker::crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100).await?;
139138

140139
// Prime the cache so it only includes the crate we just created.
141140
let mut async_conn = test_db.async_connect().await;
142141
let cache = Cache::new(vec!["[email protected]".to_string()], &mut async_conn).await?;
143142
let cache = Arc::new(cache);
144143

145144
// Now we'll create new crates: one problematic, one not so.
146-
let other_user = faker::user(&mut conn, "b")?;
145+
let other_user = faker::user(&mut async_conn, "b").await?;
147146
let angel = faker::crate_and_version(
148147
&mut async_conn,
149148
"innocent-crate",

0 commit comments

Comments
 (0)