Skip to content

Commit bcd628e

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 d90ce45 commit bcd628e

File tree

7 files changed

+64
-43
lines changed

7 files changed

+64
-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
let mut conn = app.db_conn();
1214

13-
new_category("Category 1", "cat1", "Category 1 crates")
14-
.create_or_update(&mut conn)
15+
insert_into(categories::table)
16+
.values(new_category("Category 1", "cat1", "Category 1 crates"))
17+
.execute(&mut conn)
1518
.unwrap();
1619

1720
let crate_to_publish = PublishBuilder::new("foo_good_cat", "1.0.0").category("cat1");

src/tests/routes/categories/get.rs

Lines changed: 22 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

@@ -16,10 +18,14 @@ async fn show() {
1618
anon.get(url).await.assert_not_found();
1719

1820
// Create a category and a subcategory
19-
assert_ok!(new_category("Foo Bar", "foo-bar", "Foo Bar crates").create_or_update(&mut conn));
20-
assert_ok!(
21-
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates").create_or_update(&mut conn)
22-
);
21+
let cats = vec![
22+
new_category("Foo Bar", "foo-bar", "Foo Bar crates"),
23+
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates"),
24+
];
25+
26+
assert_ok!(insert_into(categories::table)
27+
.values(cats)
28+
.execute(&mut conn));
2329

2430
// The category and its subcategories should be in the json
2531
let json: Value = anon.get(url).await.good();
@@ -41,10 +47,14 @@ async fn update_crate() {
4147
let mut conn = app.db_conn();
4248
let user = user.as_model();
4349

44-
assert_ok!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(&mut conn));
45-
assert_ok!(
46-
new_category("Category 2", "category-2", "Category 2 crates").create_or_update(&mut conn)
47-
);
50+
let cats = vec![
51+
new_category("cat1", "cat1", "Category 1 crates"),
52+
new_category("Category 2", "category-2", "Category 2 crates"),
53+
];
54+
55+
assert_ok!(insert_into(categories::table)
56+
.values(cats)
57+
.execute(&mut conn));
4858

4959
let krate = CrateBuilder::new("foo_crate", user.id).expect_build(&mut conn);
5060

@@ -97,7 +107,10 @@ async fn update_crate() {
97107
assert_eq!(count(&anon, "category-2").await, 0);
98108

99109
// Add a category and its subcategory
100-
assert_ok!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(&mut conn));
110+
assert_ok!(insert_into(categories::table)
111+
.values(new_category("cat1::bar", "cat1::bar", "bar crates"))
112+
.execute(&mut conn));
113+
101114
Category::update_crate(&mut conn, &krate, &["cat1", "cat1::bar"]).unwrap();
102115

103116
assert_eq!(count(&anon, "cat1").await, 1);

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
assert_json_snapshot!(json);
1416

1517
// Create a category and a subcategory
16-
new_category("foo", "foo", "Foo crates")
17-
.create_or_update(&mut conn)
18-
.unwrap();
19-
new_category("foo::bar", "foo::bar", "Bar crates")
20-
.create_or_update(&mut 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(&mut conn)
2126
.unwrap();
2227

2328
// Only the top-level categories should be on the page

src/tests/routes/category_slugs/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

@@ -8,11 +10,14 @@ async fn category_slugs_returns_all_slugs_in_alphabetical_order() {
810
let (app, anon) = TestApp::init().empty();
911
let mut conn = app.db_conn();
1012

11-
new_category("Foo", "foo", "For crates that foo")
12-
.create_or_update(&mut conn)
13-
.unwrap();
14-
new_category("Bar", "bar", "For crates that bar")
15-
.create_or_update(&mut conn)
13+
let cats = vec![
14+
new_category("Foo", "foo", "For crates that foo"),
15+
new_category("Bar", "bar", "For crates that bar"),
16+
];
17+
18+
insert_into(categories::table)
19+
.values(cats)
20+
.execute(&mut conn)
1621
.unwrap();
1722

1823
let response: Value = anon.get("/api/v1/category_slugs").await.good();

src/tests/routes/crates/list.rs

Lines changed: 12 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;
@@ -152,12 +153,14 @@ async fn index_queries() {
152153
assert_eq!(json.meta.total, 0);
153154
}
154155

155-
new_category("Category 1", "cat1", "Category 1 crates")
156-
.create_or_update(&mut conn)
157-
.unwrap();
156+
let cats = vec![
157+
new_category("Category 1", "cat1", "Category 1 crates"),
158+
new_category("Category 1::Ba'r", "cat1::bar", "Ba'r crates"),
159+
];
158160

159-
new_category("Category 1::Ba'r", "cat1::bar", "Ba'r crates")
160-
.create_or_update(&mut conn)
161+
insert_into(categories::table)
162+
.values(cats)
163+
.execute(&mut conn)
161164
.unwrap();
162165

163166
Category::update_crate(&mut conn, &krate, &["cat1"]).unwrap();
@@ -862,9 +865,11 @@ async fn test_default_sort_recent() {
862865
assert_eq!(json.crates[1].downloads, 20);
863866
}
864867

865-
new_category("Animal", "animal", "animal crates")
866-
.create_or_update(&mut conn)
868+
insert_into(categories::table)
869+
.values(new_category("Animal", "animal", "animal crates"))
870+
.execute(&mut conn)
867871
.unwrap();
872+
868873
Category::update_crate(&mut conn, &green_crate, &["animal"]).unwrap();
869874
Category::update_crate(&mut conn, &potato_crate, &["animal"]).unwrap();
870875

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 {
@@ -34,8 +35,9 @@ async fn summary_new_crates() {
3435
let now_ = Utc::now().naive_utc();
3536
let now_plus_two = now_ + chrono::Duration::seconds(2);
3637

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

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

0 commit comments

Comments
 (0)