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 ,
@@ -27,10 +11,9 @@ struct CategoryWithSubcategories {
27
11
#[ test]
28
12
fn index ( ) {
29
13
let ( app, anon) = TestApp :: init ( ) . empty ( ) ;
30
- let url = "/api/v1/categories" ;
31
14
32
15
// List 0 categories if none exist
33
- let json: CategoryList = anon. get ( url ) . good ( ) ;
16
+ let json = anon. show_category_list ( ) ;
34
17
assert_eq ! ( json. categories. len( ) , 0 ) ;
35
18
assert_eq ! ( json. meta. total, 0 ) ;
36
19
@@ -45,7 +28,7 @@ fn index() {
45
28
} ) ;
46
29
47
30
// Only the top-level categories should be on the page
48
- let json: CategoryList = anon. get ( url ) . good ( ) ;
31
+ let json = anon. show_category_list ( ) ;
49
32
assert_eq ! ( json. categories. len( ) , 1 ) ;
50
33
assert_eq ! ( json. meta. total, 1 ) ;
51
34
assert_eq ! ( json. categories[ 0 ] . category, "foo" ) ;
@@ -76,94 +59,76 @@ fn show() {
76
59
#[ test]
77
60
#[ allow( clippy:: cognitive_complexity) ]
78
61
fn update_crate ( ) {
79
- let ( app, middle) = app ( ) ;
80
- let mut req = req ( Method :: Get , "/api/v1/categories/foo" ) ;
81
- macro_rules! cnt {
82
- ( $req: expr, $cat: expr) => { {
83
- $req. with_path( & format!( "/api/v1/categories/{}" , $cat) ) ;
84
- let mut response = ok_resp!( middle. call( $req) ) ;
85
- crate :: json:: <GoodCategory >( & mut response)
86
- . category
87
- . crates_cnt as usize
88
- } } ;
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
89
66
}
90
67
91
- let krate = {
92
- let conn = t ! ( app. diesel_database. get( ) ) ;
93
- let user = t ! ( new_user( "foo" ) . create_or_update( & conn) ) ;
94
- t ! ( new_category( "cat1" , "cat1" , "Category 1 crates" ) . create_or_update( & conn) ) ;
95
- t ! ( new_category( "Category 2" , "category-2" , "Category 2 crates" ) . create_or_update( & conn) ) ;
96
- CrateBuilder :: new ( "foo_crate" , user. id ) . expect_build ( & conn)
97
- } ;
68
+ let ( app, anon, user) = TestApp :: init ( ) . with_user ( ) ;
69
+ let user = user. as_model ( ) ;
98
70
99
- // Updating with no categories has no effect
100
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ ] ) . unwrap ( ) ;
101
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 0 ) ;
102
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
103
-
104
- // Happy path adding one category
105
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ "cat1" ] ) . unwrap ( ) ;
106
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 1 ) ;
107
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
108
-
109
- // Replacing one category with another
110
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ "category-2" ] ) . unwrap ( ) ;
111
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 0 ) ;
112
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 1 ) ;
113
-
114
- // Removing one category
115
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ ] ) . unwrap ( ) ;
116
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 0 ) ;
117
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
118
-
119
- // Adding 2 categories
120
- Category :: update_crate (
121
- & app. diesel_database . get ( ) . unwrap ( ) ,
122
- & krate,
123
- & [ "cat1" , "category-2" ] ,
124
- )
125
- . unwrap ( ) ;
126
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 1 ) ;
127
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 1 ) ;
128
-
129
- // Removing all categories
130
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ ] ) . unwrap ( ) ;
131
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 0 ) ;
132
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
133
-
134
- // Attempting to add one valid category and one invalid category
135
- let invalid_categories = Category :: update_crate (
136
- & app. diesel_database . get ( ) . unwrap ( ) ,
137
- & krate,
138
- & [ "cat1" , "catnope" ] ,
139
- )
140
- . unwrap ( ) ;
141
- assert_eq ! ( invalid_categories, vec![ "catnope" ] ) ;
142
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 1 ) ;
143
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
144
-
145
- // Does not add the invalid category to the category list
146
- // (unlike the behavior of keywords)
147
- req. with_path ( "/api/v1/categories" ) ;
148
- let mut response = ok_resp ! ( middle. call( & mut req) ) ;
149
- let json: CategoryList = crate :: json ( & mut response) ;
150
- assert_eq ! ( json. categories. len( ) , 2 ) ;
151
- assert_eq ! ( json. meta. total, 2 ) ;
152
-
153
- // Attempting to add a category by display text; must use slug
154
- Category :: update_crate ( & app. diesel_database . get ( ) . unwrap ( ) , & krate, & [ "Category 2" ] ) . unwrap ( ) ;
155
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 0 ) ;
156
- assert_eq ! ( cnt!( & mut req, "category-2" ) , 0 ) ;
157
-
158
- // Add a category and its subcategory
159
- {
160
- let conn = t ! ( app. diesel_database. get( ) ) ;
161
- 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) ) ;
162
126
Category :: update_crate ( & conn, & krate, & [ "cat1" , "cat1::bar" ] ) . unwrap ( ) ;
163
- }
164
- assert_eq ! ( cnt!( & mut req, "cat1" ) , 1 ) ;
165
- assert_eq ! ( cnt!( & mut req, "cat1::bar" ) , 1 ) ;
166
- 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
+ } ) ;
167
132
}
168
133
169
134
#[ test]
0 commit comments