Skip to content

Commit 8742ace

Browse files
committed
builders/krate: Add category option
1 parent 58e67c2 commit 8742ace

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/tests/builders/krate.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo_registry::{
2-
models::{Crate, Keyword, NewCrate},
2+
models::{Category, Crate, Keyword, NewCrate},
33
schema::{crates, version_downloads},
44
util::errors::AppResult,
55
};
@@ -13,6 +13,7 @@ use super::VersionBuilder;
1313
/// If you want to test logic that happens as part of a publish request, use `PublishBuilder`
1414
/// instead.
1515
pub struct CrateBuilder<'a> {
16+
categories: Vec<&'a str>,
1617
downloads: Option<i32>,
1718
keywords: Vec<&'a str>,
1819
krate: NewCrate<'a>,
@@ -27,6 +28,7 @@ impl<'a> CrateBuilder<'a> {
2728
/// doesn't exist in the database, `expect_build` will fail.
2829
pub fn new(name: &str, owner_id: i32) -> CrateBuilder<'_> {
2930
CrateBuilder {
31+
categories: Vec::new(),
3032
downloads: None,
3133
keywords: Vec::new(),
3234
krate: NewCrate {
@@ -91,6 +93,12 @@ impl<'a> CrateBuilder<'a> {
9193
self
9294
}
9395

96+
/// Adds a category to the crate.
97+
pub fn category(mut self, category: &'a str) -> Self {
98+
self.categories.push(category);
99+
self
100+
}
101+
94102
/// Adds a keyword to the crate.
95103
pub fn keyword(mut self, keyword: &'a str) -> Self {
96104
self.keywords.push(keyword);
@@ -143,6 +151,10 @@ impl<'a> CrateBuilder<'a> {
143151
select(refresh_recent_crate_downloads).execute(connection)?;
144152
}
145153

154+
if !self.categories.is_empty() {
155+
Category::update_crate(connection, &krate, &self.categories)?;
156+
}
157+
146158
if !self.keywords.is_empty() {
147159
Keyword::update_crate(connection, &krate, &self.keywords)?;
148160
}

src/tests/krate.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,17 +1341,23 @@ fn summary_new_crates() {
13411341
let now_ = Utc::now().naive_utc();
13421342
let now_plus_two = now_ + chrono::Duration::seconds(2);
13431343

1344-
let krate = CrateBuilder::new("some_downloads", user.id)
1344+
new_category("Category 1", "cat1", "Category 1 crates")
1345+
.create_or_update(conn)
1346+
.unwrap();
1347+
1348+
CrateBuilder::new("some_downloads", user.id)
13451349
.version(VersionBuilder::new("0.1.0"))
13461350
.description("description")
13471351
.keyword("popular")
1352+
.category("cat1")
13481353
.downloads(20)
13491354
.recent_downloads(10)
13501355
.expect_build(conn);
13511356

1352-
let krate2 = CrateBuilder::new("most_recent_downloads", user.id)
1357+
CrateBuilder::new("most_recent_downloads", user.id)
13531358
.version(VersionBuilder::new("0.2.0"))
13541359
.keyword("popular")
1360+
.category("cat1")
13551361
.downloads(5000)
13561362
.recent_downloads(50)
13571363
.expect_build(conn);
@@ -1377,12 +1383,6 @@ fn summary_new_crates() {
13771383
.downloads(1000)
13781384
.expect_build(conn);
13791385

1380-
new_category("Category 1", "cat1", "Category 1 crates")
1381-
.create_or_update(conn)
1382-
.unwrap();
1383-
Category::update_crate(conn, &krate, &["cat1"]).unwrap();
1384-
Category::update_crate(conn, &krate2, &["cat1"]).unwrap();
1385-
13861386
// set total_downloads global value for `num_downloads` prop
13871387
update(metadata::table)
13881388
.set(metadata::total_downloads.eq(6000))

0 commit comments

Comments
 (0)