|
1 |
| -use crate::{app, builders::CrateBuilder, new_category, new_user, req, RequestHelper, TestApp}; |
2 |
| -use cargo_registry::{ |
3 |
| - models::Category, |
4 |
| - views::{EncodableCategory, EncodableCategoryWithSubcategories}, |
| 1 | +use crate::{ |
| 2 | + builders::CrateBuilder, new_category, util::MockAnonymousUser, RequestHelper, TestApp, |
5 | 3 | };
|
| 4 | +use cargo_registry::{models::Category, views::EncodableCategoryWithSubcategories}; |
6 | 5 |
|
7 |
| -use conduit::{Handler, Method}; |
8 |
| - |
9 |
| -#[derive(Deserialize)] |
10 |
| -struct CategoryList { |
11 |
| - categories: Vec<EncodableCategory>, |
12 |
| - meta: CategoryMeta, |
13 |
| -} |
14 |
| -#[derive(Deserialize)] |
15 |
| -struct CategoryMeta { |
16 |
| - total: i32, |
17 |
| -} |
18 |
| -#[derive(Deserialize)] |
19 |
| -struct GoodCategory { |
20 |
| - category: EncodableCategory, |
21 |
| -} |
22 | 6 | #[derive(Deserialize)]
|
23 | 7 | struct CategoryWithSubcategories {
|
24 | 8 | category: EncodableCategoryWithSubcategories,
|
@@ -75,94 +59,76 @@ fn show() {
|
75 | 59 | #[test]
|
76 | 60 | #[allow(clippy::cognitive_complexity)]
|
77 | 61 | fn update_crate() {
|
78 |
| - let (app, middle) = app(); |
79 |
| - let mut req = req(Method::Get, "/api/v1/categories/foo"); |
80 |
| - macro_rules! cnt { |
81 |
| - ($req:expr, $cat:expr) => {{ |
82 |
| - $req.with_path(&format!("/api/v1/categories/{}", $cat)); |
83 |
| - let mut response = ok_resp!(middle.call($req)); |
84 |
| - crate::json::<GoodCategory>(&mut response) |
85 |
| - .category |
86 |
| - .crates_cnt as usize |
87 |
| - }}; |
| 62 | + // Convenience function to get the number of crates in a category |
| 63 | + fn count(anon: &MockAnonymousUser, category: &str) -> usize { |
| 64 | + let json = anon.show_category(category); |
| 65 | + json.category.crates_cnt as usize |
88 | 66 | }
|
89 | 67 |
|
90 |
| - let krate = { |
91 |
| - let conn = t!(app.diesel_database.get()); |
92 |
| - let user = t!(new_user("foo").create_or_update(&conn)); |
93 |
| - t!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(&conn)); |
94 |
| - t!(new_category("Category 2", "category-2", "Category 2 crates").create_or_update(&conn)); |
95 |
| - CrateBuilder::new("foo_crate", user.id).expect_build(&conn) |
96 |
| - }; |
| 68 | + let (app, anon, user) = TestApp::init().with_user(); |
| 69 | + let user = user.as_model(); |
97 | 70 |
|
98 |
| - // Updating with no categories has no effect |
99 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &[]).unwrap(); |
100 |
| - assert_eq!(cnt!(&mut req, "cat1"), 0); |
101 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
102 |
| - |
103 |
| - // Happy path adding one category |
104 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &["cat1"]).unwrap(); |
105 |
| - assert_eq!(cnt!(&mut req, "cat1"), 1); |
106 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
107 |
| - |
108 |
| - // Replacing one category with another |
109 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &["category-2"]).unwrap(); |
110 |
| - assert_eq!(cnt!(&mut req, "cat1"), 0); |
111 |
| - assert_eq!(cnt!(&mut req, "category-2"), 1); |
112 |
| - |
113 |
| - // Removing one category |
114 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &[]).unwrap(); |
115 |
| - assert_eq!(cnt!(&mut req, "cat1"), 0); |
116 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
117 |
| - |
118 |
| - // Adding 2 categories |
119 |
| - Category::update_crate( |
120 |
| - &app.diesel_database.get().unwrap(), |
121 |
| - &krate, |
122 |
| - &["cat1", "category-2"], |
123 |
| - ) |
124 |
| - .unwrap(); |
125 |
| - assert_eq!(cnt!(&mut req, "cat1"), 1); |
126 |
| - assert_eq!(cnt!(&mut req, "category-2"), 1); |
127 |
| - |
128 |
| - // Removing all categories |
129 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &[]).unwrap(); |
130 |
| - assert_eq!(cnt!(&mut req, "cat1"), 0); |
131 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
132 |
| - |
133 |
| - // Attempting to add one valid category and one invalid category |
134 |
| - let invalid_categories = Category::update_crate( |
135 |
| - &app.diesel_database.get().unwrap(), |
136 |
| - &krate, |
137 |
| - &["cat1", "catnope"], |
138 |
| - ) |
139 |
| - .unwrap(); |
140 |
| - assert_eq!(invalid_categories, vec!["catnope"]); |
141 |
| - assert_eq!(cnt!(&mut req, "cat1"), 1); |
142 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
143 |
| - |
144 |
| - // Does not add the invalid category to the category list |
145 |
| - // (unlike the behavior of keywords) |
146 |
| - req.with_path("/api/v1/categories"); |
147 |
| - let mut response = ok_resp!(middle.call(&mut req)); |
148 |
| - let json: CategoryList = crate::json(&mut response); |
149 |
| - assert_eq!(json.categories.len(), 2); |
150 |
| - assert_eq!(json.meta.total, 2); |
151 |
| - |
152 |
| - // Attempting to add a category by display text; must use slug |
153 |
| - Category::update_crate(&app.diesel_database.get().unwrap(), &krate, &["Category 2"]).unwrap(); |
154 |
| - assert_eq!(cnt!(&mut req, "cat1"), 0); |
155 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
156 |
| - |
157 |
| - // Add a category and its subcategory |
158 |
| - { |
159 |
| - let conn = t!(app.diesel_database.get()); |
160 |
| - t!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(&conn,)); |
| 71 | + app.db(|conn| { |
| 72 | + t!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(conn)); |
| 73 | + t!(new_category("Category 2", "category-2", "Category 2 crates").create_or_update(conn)); |
| 74 | + let krate = CrateBuilder::new("foo_crate", user.id).expect_build(&conn); |
| 75 | + |
| 76 | + // Updating with no categories has no effect |
| 77 | + Category::update_crate(conn, &krate, &[]).unwrap(); |
| 78 | + assert_eq!(count(&anon, "cat1"), 0); |
| 79 | + assert_eq!(count(&anon, "category-2"), 0); |
| 80 | + |
| 81 | + // Happy path adding one category |
| 82 | + Category::update_crate(conn, &krate, &["cat1"]).unwrap(); |
| 83 | + assert_eq!(count(&anon, "cat1"), 1); |
| 84 | + assert_eq!(count(&anon, "category-2"), 0); |
| 85 | + |
| 86 | + // Replacing one category with another |
| 87 | + Category::update_crate(conn, &krate, &["category-2"]).unwrap(); |
| 88 | + assert_eq!(count(&anon, "cat1"), 0); |
| 89 | + assert_eq!(count(&anon, "category-2"), 1); |
| 90 | + |
| 91 | + // Removing one category |
| 92 | + Category::update_crate(conn, &krate, &[]).unwrap(); |
| 93 | + assert_eq!(count(&anon, "cat1"), 0); |
| 94 | + assert_eq!(count(&anon, "category-2"), 0); |
| 95 | + |
| 96 | + // Adding 2 categories |
| 97 | + Category::update_crate(conn, &krate, &["cat1", "category-2"]).unwrap(); |
| 98 | + assert_eq!(count(&anon, "cat1"), 1); |
| 99 | + assert_eq!(count(&anon, "category-2"), 1); |
| 100 | + |
| 101 | + // Removing all categories |
| 102 | + Category::update_crate(conn, &krate, &[]).unwrap(); |
| 103 | + assert_eq!(count(&anon, "cat1"), 0); |
| 104 | + assert_eq!(count(&anon, "category-2"), 0); |
| 105 | + |
| 106 | + // Attempting to add one valid category and one invalid category |
| 107 | + let invalid_categories = |
| 108 | + Category::update_crate(conn, &krate, &["cat1", "catnope"]).unwrap(); |
| 109 | + assert_eq!(invalid_categories, vec!["catnope"]); |
| 110 | + assert_eq!(count(&anon, "cat1"), 1); |
| 111 | + assert_eq!(count(&anon, "category-2"), 0); |
| 112 | + |
| 113 | + // Does not add the invalid category to the category list |
| 114 | + // (unlike the behavior of keywords) |
| 115 | + let json = anon.show_category_list(); |
| 116 | + assert_eq!(json.categories.len(), 2); |
| 117 | + assert_eq!(json.meta.total, 2); |
| 118 | + |
| 119 | + // Attempting to add a category by display text; must use slug |
| 120 | + Category::update_crate(conn, &krate, &["Category 2"]).unwrap(); |
| 121 | + assert_eq!(count(&anon, "cat1"), 0); |
| 122 | + assert_eq!(count(&anon, "category-2"), 0); |
| 123 | + |
| 124 | + // Add a category and its subcategory |
| 125 | + t!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(conn)); |
161 | 126 | Category::update_crate(&conn, &krate, &["cat1", "cat1::bar"]).unwrap();
|
162 |
| - } |
163 |
| - assert_eq!(cnt!(&mut req, "cat1"), 1); |
164 |
| - assert_eq!(cnt!(&mut req, "cat1::bar"), 1); |
165 |
| - assert_eq!(cnt!(&mut req, "category-2"), 0); |
| 127 | + |
| 128 | + assert_eq!(count(&anon, "cat1"), 1); |
| 129 | + assert_eq!(count(&anon, "cat1::bar"), 1); |
| 130 | + assert_eq!(count(&anon, "category-2"), 0); |
| 131 | + }); |
166 | 132 | }
|
167 | 133 |
|
168 | 134 | #[test]
|
|
0 commit comments