Skip to content

Commit 8cff694

Browse files
committed
Inline NewCategory::create_or_update()
The function is only used in our test suite, the update behavior isn't actually needed there and the return value is also not used anywhere. This makes it possible to significantly simplify the function to the point where there is no point anymore to keep the function. Inlining it then also allows us to merge a couple of calls into single queries that insert multiple rows at once.
1 parent 5f392c3 commit 8cff694

File tree

7 files changed

+62
-43
lines changed

7 files changed

+62
-43
lines changed

src/models/category.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,6 @@ pub struct NewCategory<'a> {
131131
pub description: &'a str,
132132
}
133133

134-
impl<'a> NewCategory<'a> {
135-
/// Inserts the category into the database, or updates an existing one.
136-
pub fn create_or_update(&self, conn: &mut impl Conn) -> QueryResult<Category> {
137-
insert_into(categories::table)
138-
.values(self)
139-
.on_conflict(categories::slug)
140-
.do_update()
141-
.set(self)
142-
.get_result(conn)
143-
}
144-
}
145-
146134
#[cfg(test)]
147135
mod tests {
148136
use super::*;

src/tests/krate/publish/categories.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::tests::builders::PublishBuilder;
22
use crate::tests::new_category;
33
use crate::tests::util::{RequestHelper, TestApp};
4+
use crates_io_database::schema::categories;
5+
use diesel::{insert_into, RunQueryDsl};
46
use googletest::prelude::*;
57
use http::StatusCode;
68
use insta::{assert_json_snapshot, assert_snapshot};
@@ -10,8 +12,9 @@ async fn good_categories() {
1012
let (app, _, _, token) = TestApp::full().with_token();
1113

1214
app.db(|conn| {
13-
new_category("Category 1", "cat1", "Category 1 crates")
14-
.create_or_update(conn)
15+
insert_into(categories::table)
16+
.values(new_category("Category 1", "cat1", "Category 1 crates"))
17+
.execute(conn)
1518
.unwrap();
1619
});
1720

src/tests/routes/categories/get.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::models::Category;
22
use crate::tests::builders::CrateBuilder;
33
use crate::tests::new_category;
44
use crate::tests::util::{MockAnonymousUser, RequestHelper, TestApp};
5+
use crates_io_database::schema::categories;
6+
use diesel::{insert_into, RunQueryDsl};
57
use insta::assert_json_snapshot;
68
use serde_json::Value;
79

@@ -15,10 +17,12 @@ async fn show() {
1517

1618
// Create a category and a subcategory
1719
app.db(|conn| {
18-
assert_ok!(new_category("Foo Bar", "foo-bar", "Foo Bar crates").create_or_update(conn));
19-
assert_ok!(
20-
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates").create_or_update(conn)
21-
);
20+
let cats = vec![
21+
new_category("Foo Bar", "foo-bar", "Foo Bar crates"),
22+
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates"),
23+
];
24+
25+
assert_ok!(insert_into(categories::table).values(cats).execute(conn));
2226
});
2327

2428
// The category and its subcategories should be in the json
@@ -41,10 +45,12 @@ async fn update_crate() {
4145
let user = user.as_model();
4246

4347
let krate = app.db(|conn| {
44-
assert_ok!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(conn));
45-
assert_ok!(
46-
new_category("Category 2", "category-2", "Category 2 crates").create_or_update(conn)
47-
);
48+
let cats = vec![
49+
new_category("cat1", "cat1", "Category 1 crates"),
50+
new_category("Category 2", "category-2", "Category 2 crates"),
51+
];
52+
53+
assert_ok!(insert_into(categories::table).values(cats).execute(conn));
4854

4955
CrateBuilder::new("foo_crate", user.id).expect_build(conn)
5056
});
@@ -101,7 +107,10 @@ async fn update_crate() {
101107

102108
// Add a category and its subcategory
103109
app.db(|conn| {
104-
assert_ok!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(conn));
110+
assert_ok!(insert_into(categories::table)
111+
.values(new_category("cat1::bar", "cat1::bar", "bar crates"))
112+
.execute(conn));
113+
105114
Category::update_crate(conn, &krate, &["cat1", "cat1::bar"]).unwrap();
106115
});
107116

src/tests/routes/categories/list.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::tests::new_category;
22
use crate::tests::util::{RequestHelper, TestApp};
3+
use crates_io_database::schema::categories;
4+
use diesel::{insert_into, RunQueryDsl};
35
use insta::assert_json_snapshot;
46
use serde_json::Value;
57

@@ -13,11 +15,14 @@ async fn index() {
1315

1416
// Create a category and a subcategory
1517
app.db(|conn| {
16-
new_category("foo", "foo", "Foo crates")
17-
.create_or_update(conn)
18-
.unwrap();
19-
new_category("foo::bar", "foo::bar", "Bar crates")
20-
.create_or_update(conn)
18+
let cats = vec![
19+
new_category("foo", "foo", "Foo crates"),
20+
new_category("foo::bar", "foo::bar", "Bar crates"),
21+
];
22+
23+
insert_into(categories::table)
24+
.values(cats)
25+
.execute(conn)
2126
.unwrap();
2227
});
2328

src/tests/routes/category_slugs/list.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
use crate::tests::new_category;
22
use crate::tests::util::{RequestHelper, TestApp};
3+
use crates_io_database::schema::categories;
4+
use diesel::{insert_into, RunQueryDsl};
35
use insta::assert_json_snapshot;
46
use serde_json::Value;
57

68
#[tokio::test(flavor = "multi_thread")]
79
async fn category_slugs_returns_all_slugs_in_alphabetical_order() {
810
let (app, anon) = TestApp::init().empty();
911
app.db(|conn| {
10-
new_category("Foo", "foo", "For crates that foo")
11-
.create_or_update(conn)
12-
.unwrap();
13-
new_category("Bar", "bar", "For crates that bar")
14-
.create_or_update(conn)
12+
let cats = vec![
13+
new_category("Foo", "foo", "For crates that foo"),
14+
new_category("Bar", "bar", "For crates that bar"),
15+
];
16+
17+
insert_into(categories::table)
18+
.values(cats)
19+
.execute(conn)
1520
.unwrap();
1621
});
1722

src/tests/routes/crates/list.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::schema::crates;
33
use crate::tests::builders::{CrateBuilder, VersionBuilder};
44
use crate::tests::util::{RequestHelper, TestApp};
55
use crate::tests::{new_category, new_user};
6+
use crates_io_database::schema::categories;
67
use diesel::{dsl::*, prelude::*, update};
78
use googletest::prelude::*;
89
use http::StatusCode;
@@ -154,12 +155,16 @@ async fn index_queries() {
154155
}
155156

156157
app.db(|conn| {
157-
new_category("Category 1", "cat1", "Category 1 crates")
158-
.create_or_update(conn)
159-
.unwrap();
160-
new_category("Category 1::Ba'r", "cat1::bar", "Ba'r crates")
161-
.create_or_update(conn)
158+
let cats = vec![
159+
new_category("Category 1", "cat1", "Category 1 crates"),
160+
new_category("Category 1::Ba'r", "cat1::bar", "Ba'r crates"),
161+
];
162+
163+
insert_into(categories::table)
164+
.values(cats)
165+
.execute(conn)
162166
.unwrap();
167+
163168
Category::update_crate(conn, &krate, &["cat1"]).unwrap();
164169
Category::update_crate(conn, &krate2, &["cat1::bar"]).unwrap();
165170
});
@@ -873,9 +878,11 @@ async fn test_default_sort_recent() {
873878
}
874879

875880
app.db(|conn| {
876-
new_category("Animal", "animal", "animal crates")
877-
.create_or_update(conn)
881+
insert_into(categories::table)
882+
.values(new_category("Animal", "animal", "animal crates"))
883+
.execute(conn)
878884
.unwrap();
885+
879886
Category::update_crate(conn, &green_crate, &["animal"]).unwrap();
880887
Category::update_crate(conn, &potato_crate, &["animal"]).unwrap();
881888
});

src/tests/routes/summary.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::tests::new_category;
44
use crate::tests::util::{RequestHelper, TestApp};
55
use crate::views::{EncodableCategory, EncodableCrate, EncodableKeyword};
66
use chrono::Utc;
7-
use diesel::{update, Connection, ExpressionMethods, RunQueryDsl};
7+
use crates_io_database::schema::categories;
8+
use diesel::{insert_into, update, Connection, ExpressionMethods, RunQueryDsl};
89

910
#[derive(Deserialize)]
1011
struct SummaryResponse {
@@ -33,8 +34,9 @@ async fn summary_new_crates() {
3334
let now_ = Utc::now().naive_utc();
3435
let now_plus_two = now_ + chrono::Duration::seconds(2);
3536

36-
new_category("Category 1", "cat1", "Category 1 crates")
37-
.create_or_update(conn)
37+
insert_into(categories::table)
38+
.values(new_category("Category 1", "cat1", "Category 1 crates"))
39+
.execute(conn)
3840
.unwrap();
3941

4042
CrateBuilder::new("some_downloads", user.id)

0 commit comments

Comments
 (0)