Skip to content

Commit bc6c8f0

Browse files
authored
Merge pull request #10057 from Turbo87/result-tests
Simplify tests by returning Result from test fns
2 parents b810634 + 459f273 commit bc6c8f0

File tree

15 files changed

+337
-348
lines changed

15 files changed

+337
-348
lines changed

src/tests/dump_db.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ static PATH_DATE_RE: LazyLock<Regex> =
1414
LazyLock::new(|| Regex::new(r"^\d{4}-\d{2}-\d{2}-\d{6}").unwrap());
1515

1616
#[tokio::test(flavor = "multi_thread")]
17-
async fn test_dump_db_job() {
17+
async fn test_dump_db_job() -> anyhow::Result<()> {
1818
let (app, _, _, token) = TestApp::full().with_token().await;
1919
let mut conn = app.db_conn().await;
2020

2121
CrateBuilder::new("test-crate", token.as_model().user_id)
2222
.expect_build(&mut conn)
2323
.await;
2424

25-
DumpDb.enqueue(&mut conn).await.unwrap();
25+
DumpDb.enqueue(&mut conn).await?;
2626

2727
app.run_pending_background_jobs().await;
2828

@@ -31,9 +31,9 @@ async fn test_dump_db_job() {
3131
db-dump.zip
3232
");
3333

34-
let path = object_store::path::Path::parse("db-dump.tar.gz").unwrap();
35-
let result = app.as_inner().storage.as_inner().get(&path).await.unwrap();
36-
let bytes = result.bytes().await.unwrap();
34+
let path = object_store::path::Path::parse("db-dump.tar.gz")?;
35+
let result = app.as_inner().storage.as_inner().get(&path).await?;
36+
let bytes = result.bytes().await?;
3737

3838
let gz = GzDecoder::new(bytes.reader());
3939
let mut tar = Archive::new(gz);
@@ -66,11 +66,11 @@ async fn test_dump_db_job() {
6666
]
6767
"#);
6868

69-
let path = object_store::path::Path::parse("db-dump.zip").unwrap();
70-
let result = app.as_inner().storage.as_inner().get(&path).await.unwrap();
71-
let bytes = result.bytes().await.unwrap();
69+
let path = object_store::path::Path::parse("db-dump.zip")?;
70+
let result = app.as_inner().storage.as_inner().get(&path).await?;
71+
let bytes = result.bytes().await?;
7272

73-
let archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
73+
let archive = zip::ZipArchive::new(Cursor::new(bytes))?;
7474
let zip_paths = archive.file_names().collect::<Vec<_>>();
7575
assert_debug_snapshot!(zip_paths, @r#"
7676
[
@@ -97,6 +97,8 @@ async fn test_dump_db_job() {
9797
"data/version_downloads.csv",
9898
]
9999
"#);
100+
101+
Ok(())
100102
}
101103

102104
fn tar_paths<R: Read>(archive: &mut Archive<R>) -> Vec<String> {

src/tests/owners.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async fn owners_can_remove_self() {
216216

217217
/// Verify consistency when adidng or removing multiple owners in a single request.
218218
#[tokio::test(flavor = "multi_thread")]
219-
async fn modify_multiple_owners() {
219+
async fn modify_multiple_owners() -> anyhow::Result<()> {
220220
let (app, _, user, token) = TestApp::init().with_token().await;
221221
let mut conn = app.db_conn().await;
222222
let username = &user.as_model().gh_login;
@@ -236,23 +236,23 @@ async fn modify_multiple_owners() {
236236
.await;
237237
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
238238
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"cannot remove all individual owners of a crate. Team member don't have permission to modify owners, so at least one individual owner is required."}]}"#);
239-
assert_eq!(krate.owners(&mut conn).await.unwrap().len(), 3);
239+
assert_eq!(krate.owners(&mut conn).await?.len(), 3);
240240

241241
// Deleting two owners at once is allowed.
242242
let response = token
243243
.remove_named_owners("owners_multiple", &["user2", "user3"])
244244
.await;
245245
assert_eq!(response.status(), StatusCode::OK);
246246
assert_snapshot!(response.text(), @r#"{"msg":"owners successfully removed","ok":true}"#);
247-
assert_eq!(krate.owners(&mut conn).await.unwrap().len(), 1);
247+
assert_eq!(krate.owners(&mut conn).await?.len(), 1);
248248

249249
// Adding multiple users fails if one of them already is an owner.
250250
let response = token
251251
.add_named_owners("owners_multiple", &["user2", username])
252252
.await;
253253
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
254254
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"`foo` is already an owner"}]}"#);
255-
assert_eq!(krate.owners(&mut conn).await.unwrap().len(), 1);
255+
assert_eq!(krate.owners(&mut conn).await?.len(), 1);
256256

257257
// Adding multiple users at once succeeds.
258258
let response = token
@@ -270,7 +270,9 @@ async fn modify_multiple_owners() {
270270
.accept_ownership_invitation(&krate.name, krate.id)
271271
.await;
272272

273-
assert_eq!(krate.owners(&mut conn).await.unwrap().len(), 3);
273+
assert_eq!(krate.owners(&mut conn).await?.len(), 3);
274+
275+
Ok(())
274276
}
275277

276278
/// Testing the crate ownership between two crates and one team.
@@ -280,21 +282,16 @@ async fn modify_multiple_owners() {
280282
/// and that the CrateList returned for the team_id contains
281283
/// only crates owned by that team.
282284
#[tokio::test(flavor = "multi_thread")]
283-
async fn check_ownership_two_crates() {
285+
async fn check_ownership_two_crates() -> anyhow::Result<()> {
284286
let (app, anon, user) = TestApp::init().with_user().await;
285287
let mut conn = app.db_conn().await;
286288
let user = user.as_model();
287289

288-
let team = new_team("team_foo")
289-
.create_or_update(&mut conn)
290-
.await
291-
.unwrap();
290+
let team = new_team("team_foo").create_or_update(&mut conn).await?;
292291
let krate_owned_by_team = CrateBuilder::new("foo", user.id)
293292
.expect_build(&mut conn)
294293
.await;
295-
add_team_to_crate(&team, &krate_owned_by_team, user, &mut conn)
296-
.await
297-
.unwrap();
294+
add_team_to_crate(&team, &krate_owned_by_team, user, &mut conn).await?;
298295

299296
let user2 = app.db_new_user("user_bar").await;
300297
let user2 = user2.as_model();
@@ -310,6 +307,8 @@ async fn check_ownership_two_crates() {
310307
let json = anon.search(&query).await;
311308
assert_eq!(json.crates.len(), 1);
312309
assert_eq!(json.crates[0].name, krate_owned_by_team.name);
310+
311+
Ok(())
313312
}
314313

315314
/// Given a crate owned by both a team and a user, check that the
@@ -320,21 +319,18 @@ async fn check_ownership_two_crates() {
320319
/// of form github:org_name:team_name as that is the format
321320
/// EncodableOwner::encodable is expecting
322321
#[tokio::test(flavor = "multi_thread")]
323-
async fn check_ownership_one_crate() {
322+
async fn check_ownership_one_crate() -> anyhow::Result<()> {
324323
let (app, anon, user) = TestApp::init().with_user().await;
325324
let mut conn = app.db_conn().await;
326325
let user = user.as_model();
327326

328327
let team = new_team("github:test_org:team_sloth")
329328
.create_or_update(&mut conn)
330-
.await
331-
.unwrap();
329+
.await?;
332330
let krate = CrateBuilder::new("best_crate", user.id)
333331
.expect_build(&mut conn)
334332
.await;
335-
add_team_to_crate(&team, &krate, user, &mut conn)
336-
.await
337-
.unwrap();
333+
add_team_to_crate(&team, &krate, user, &mut conn).await?;
338334

339335
let json: TeamResponse = anon
340336
.get("/api/v1/crates/best_crate/owner_team")
@@ -349,6 +345,8 @@ async fn check_ownership_one_crate() {
349345
.good();
350346
assert_eq!(json.users[0].kind, "user");
351347
assert_eq!(json.users[0].name, user.name);
348+
349+
Ok(())
352350
}
353351

354352
/// Assert the error response when attempting to add a team as a crate owner

src/tests/routes/categories/get.rs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use insta::assert_json_snapshot;
99
use serde_json::Value;
1010

1111
#[tokio::test(flavor = "multi_thread")]
12-
async fn show() {
12+
async fn show() -> anyhow::Result<()> {
1313
let (app, anon) = TestApp::init().empty().await;
1414
let mut conn = app.db_conn().await;
1515

@@ -24,23 +24,23 @@ async fn show() {
2424
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates"),
2525
];
2626

27-
assert_ok!(
28-
insert_into(categories::table)
29-
.values(cats)
30-
.execute(&mut conn)
31-
.await
32-
);
27+
insert_into(categories::table)
28+
.values(cats)
29+
.execute(&mut conn)
30+
.await?;
3331

3432
// The category and its subcategories should be in the json
3533
let json: Value = anon.get(url).await.good();
3634
assert_json_snapshot!(json, {
3735
".**.created_at" => "[datetime]",
3836
});
37+
38+
Ok(())
3939
}
4040

4141
#[tokio::test(flavor = "multi_thread")]
4242
#[allow(clippy::cognitive_complexity)]
43-
async fn update_crate() {
43+
async fn update_crate() -> anyhow::Result<()> {
4444
// Convenience function to get the number of crates in a category
4545
async fn count(anon: &MockAnonymousUser, category: &str) -> usize {
4646
let json = anon.show_category(category).await;
@@ -68,51 +68,38 @@ async fn update_crate() {
6868
.await;
6969

7070
// Updating with no categories has no effect
71-
Category::update_crate(&mut conn, krate.id, &[])
72-
.await
73-
.unwrap();
71+
Category::update_crate(&mut conn, krate.id, &[]).await?;
7472
assert_eq!(count(&anon, "cat1").await, 0);
7573
assert_eq!(count(&anon, "category-2").await, 0);
7674

7775
// Happy path adding one category
78-
Category::update_crate(&mut conn, krate.id, &["cat1"])
79-
.await
80-
.unwrap();
76+
Category::update_crate(&mut conn, krate.id, &["cat1"]).await?;
8177
assert_eq!(count(&anon, "cat1").await, 1);
8278
assert_eq!(count(&anon, "category-2").await, 0);
8379

8480
// Replacing one category with another
85-
Category::update_crate(&mut conn, krate.id, &["category-2"])
86-
.await
87-
.unwrap();
81+
Category::update_crate(&mut conn, krate.id, &["category-2"]).await?;
8882
assert_eq!(count(&anon, "cat1").await, 0);
8983
assert_eq!(count(&anon, "category-2").await, 1);
9084

9185
// Removing one category
92-
Category::update_crate(&mut conn, krate.id, &[])
93-
.await
94-
.unwrap();
86+
Category::update_crate(&mut conn, krate.id, &[]).await?;
9587
assert_eq!(count(&anon, "cat1").await, 0);
9688
assert_eq!(count(&anon, "category-2").await, 0);
9789

9890
// Adding 2 categories
99-
Category::update_crate(&mut conn, krate.id, &["cat1", "category-2"])
100-
.await
101-
.unwrap();
91+
Category::update_crate(&mut conn, krate.id, &["cat1", "category-2"]).await?;
10292
assert_eq!(count(&anon, "cat1").await, 1);
10393
assert_eq!(count(&anon, "category-2").await, 1);
10494

10595
// Removing all categories
106-
Category::update_crate(&mut conn, krate.id, &[])
107-
.await
108-
.unwrap();
96+
Category::update_crate(&mut conn, krate.id, &[]).await?;
10997
assert_eq!(count(&anon, "cat1").await, 0);
11098
assert_eq!(count(&anon, "category-2").await, 0);
11199

112100
// Attempting to add one valid category and one invalid category
113-
let invalid_categories = Category::update_crate(&mut conn, krate.id, &["cat1", "catnope"])
114-
.await
115-
.unwrap();
101+
let invalid_categories =
102+
Category::update_crate(&mut conn, krate.id, &["cat1", "catnope"]).await?;
116103
assert_eq!(invalid_categories, vec!["catnope"]);
117104
assert_eq!(count(&anon, "cat1").await, 1);
118105
assert_eq!(count(&anon, "category-2").await, 0);
@@ -124,9 +111,7 @@ async fn update_crate() {
124111
assert_eq!(json.meta.total, 2);
125112

126113
// Attempting to add a category by display text; must use slug
127-
Category::update_crate(&mut conn, krate.id, &["Category 2"])
128-
.await
129-
.unwrap();
114+
Category::update_crate(&mut conn, krate.id, &["Category 2"]).await?;
130115
assert_eq!(count(&anon, "cat1").await, 0);
131116
assert_eq!(count(&anon, "category-2").await, 0);
132117

@@ -138,11 +123,11 @@ async fn update_crate() {
138123
.await
139124
);
140125

141-
Category::update_crate(&mut conn, krate.id, &["cat1", "cat1::bar"])
142-
.await
143-
.unwrap();
126+
Category::update_crate(&mut conn, krate.id, &["cat1", "cat1::bar"]).await?;
144127

145128
assert_eq!(count(&anon, "cat1").await, 1);
146129
assert_eq!(count(&anon, "cat1::bar").await, 1);
147130
assert_eq!(count(&anon, "category-2").await, 0);
131+
132+
Ok(())
148133
}

0 commit comments

Comments
 (0)