Skip to content

Commit 5ae0f67

Browse files
authored
Migrate add_team_to_crate() to async queries (#10017)
1 parent 92b8fac commit 5ae0f67

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/tests/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010

1111
use crate::tests::util::github::next_gh_id;
1212
use diesel::prelude::*;
13+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
1314

1415
mod account_lock;
1516
mod authentication;
@@ -111,11 +112,11 @@ fn new_team(login: &str) -> NewTeam<'_> {
111112
.build()
112113
}
113114

114-
pub fn add_team_to_crate(
115+
pub async fn add_team_to_crate(
115116
t: &Team,
116117
krate: &Crate,
117118
u: &User,
118-
conn: &mut PgConnection,
119+
conn: &mut AsyncPgConnection,
119120
) -> QueryResult<()> {
120121
let crate_owner = CrateOwner {
121122
crate_id: krate.id,
@@ -130,7 +131,8 @@ pub fn add_team_to_crate(
130131
.on_conflict(crate_owners::table.primary_key())
131132
.do_update()
132133
.set(crate_owners::deleted.eq(false))
133-
.execute(conn)?;
134+
.execute(conn)
135+
.await?;
134136

135137
Ok(())
136138
}

src/tests/owners.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,14 @@ async fn modify_multiple_owners() {
279279
async fn check_ownership_two_crates() {
280280
let (app, anon, user) = TestApp::init().with_user().await;
281281
let mut conn = app.db_conn();
282+
let mut async_conn = app.async_db_conn().await;
282283
let user = user.as_model();
283284

284285
let team = new_team("team_foo").create_or_update(&mut conn).unwrap();
285286
let krate_owned_by_team = CrateBuilder::new("foo", user.id).expect_build(&mut conn);
286-
add_team_to_crate(&team, &krate_owned_by_team, user, &mut conn).unwrap();
287+
add_team_to_crate(&team, &krate_owned_by_team, user, &mut async_conn)
288+
.await
289+
.unwrap();
287290

288291
let user2 = app.db_new_user("user_bar").await;
289292
let user2 = user2.as_model();
@@ -310,13 +313,16 @@ async fn check_ownership_two_crates() {
310313
async fn check_ownership_one_crate() {
311314
let (app, anon, user) = TestApp::init().with_user().await;
312315
let mut conn = app.db_conn();
316+
let mut async_conn = app.async_db_conn().await;
313317
let user = user.as_model();
314318

315319
let team = new_team("github:test_org:team_sloth")
316320
.create_or_update(&mut conn)
317321
.unwrap();
318322
let krate = CrateBuilder::new("best_crate", user.id).expect_build(&mut conn);
319-
add_team_to_crate(&team, &krate, user, &mut conn).unwrap();
323+
add_team_to_crate(&team, &krate, user, &mut async_conn)
324+
.await
325+
.unwrap();
320326

321327
let json: TeamResponse = anon
322328
.get("/api/v1/crates/best_crate/owner_team")
@@ -339,13 +345,16 @@ async fn check_ownership_one_crate() {
339345
async fn add_existing_team() {
340346
let (app, _, user, token) = TestApp::init().with_token().await;
341347
let mut conn = app.db_conn();
348+
let mut async_conn = app.async_db_conn().await;
342349
let user = user.as_model();
343350

344351
let t = new_team("github:test_org:bananas")
345352
.create_or_update(&mut conn)
346353
.unwrap();
347354
let krate = CrateBuilder::new("best_crate", user.id).expect_build(&mut conn);
348-
add_team_to_crate(&t, &krate, user, &mut conn).unwrap();
355+
add_team_to_crate(&t, &krate, user, &mut async_conn)
356+
.await
357+
.unwrap();
349358

350359
let ret = token
351360
.add_named_owner("best_crate", "github:test_org:bananas")

src/tests/team.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,16 @@ async fn add_owners_as_team_owner() {
419419
async fn crates_by_team_id() {
420420
let (app, anon, user) = TestApp::init().with_user().await;
421421
let mut conn = app.db_conn();
422+
let mut async_conn = app.async_db_conn().await;
422423
let user = user.as_model();
423424

424425
let t = new_team("github:test-org:team")
425426
.create_or_update(&mut conn)
426427
.unwrap();
427428
let krate = CrateBuilder::new("foo", user.id).expect_build(&mut conn);
428-
add_team_to_crate(&t, &krate, user, &mut conn).unwrap();
429+
add_team_to_crate(&t, &krate, user, &mut async_conn)
430+
.await
431+
.unwrap();
429432

430433
let json = anon.search(&format!("team_id={}", t.id)).await;
431434
assert_eq!(json.crates.len(), 1);
@@ -435,6 +438,7 @@ async fn crates_by_team_id() {
435438
async fn crates_by_team_id_not_including_deleted_owners() {
436439
let (app, anon) = TestApp::init().empty().await;
437440
let mut conn = app.db_conn();
441+
let mut async_conn = app.async_db_conn().await;
438442
let user = app.db_new_user("user-all-teams").await;
439443
let user = user.as_model();
440444

@@ -447,7 +451,9 @@ async fn crates_by_team_id_not_including_deleted_owners() {
447451
let t = new_team.create_or_update(&mut conn).unwrap();
448452

449453
let krate = CrateBuilder::new("foo", user.id).expect_build(&mut conn);
450-
add_team_to_crate(&t, &krate, user, &mut conn).unwrap();
454+
add_team_to_crate(&t, &krate, user, &mut async_conn)
455+
.await
456+
.unwrap();
451457
krate.owner_remove(&mut conn, &t.login).unwrap();
452458

453459
let json = anon.search(&format!("team_id={}", t.id)).await;

src/typosquat/database.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ mod tests {
183183
async fn top_crates() -> Result<(), Error> {
184184
let test_db = TestDatabase::new();
185185
let mut conn = test_db.connect();
186+
let mut async_conn = test_db.async_connect().await;
186187

187188
// Set up two users.
188189
let user_a = faker::user(&mut conn, "a")?;
@@ -195,10 +196,9 @@ mod tests {
195196

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

201-
let mut async_conn = test_db.async_connect().await;
202202
let top_crates = TopCrates::new(&mut async_conn, 2).await?;
203203

204204
// Let's ensure the top crates include what we expect (which is a and b, since we asked for

0 commit comments

Comments
 (0)