@@ -2,7 +2,7 @@ use conduit::{Handler, Method};
2
2
3
3
use models:: Category ;
4
4
use views:: { EncodableCategory , EncodableCategoryWithSubcategories } ;
5
- use { app, new_category, new_user, req, CrateBuilder } ;
5
+ use { app, new_category, new_user, req, CrateBuilder , RequestHelper , TestApp } ;
6
6
7
7
#[ derive( Deserialize ) ]
8
8
struct CategoryList {
@@ -24,55 +24,47 @@ struct CategoryWithSubcategories {
24
24
25
25
#[ test]
26
26
fn index ( ) {
27
- let ( _b , app, middle ) = app ( ) ;
28
- let mut req = req ( Method :: Get , "/api/v1/categories" ) ;
27
+ let ( app, anon ) = TestApp :: empty ( ) ;
28
+ let url = "/api/v1/categories" ;
29
29
30
30
// List 0 categories if none exist
31
- let mut response = ok_resp ! ( middle. call( & mut req) ) ;
32
- let json: CategoryList = :: json ( & mut response) ;
31
+ let json: CategoryList = anon. get ( url) . good ( ) ;
33
32
assert_eq ! ( json. categories. len( ) , 0 ) ;
34
33
assert_eq ! ( json. meta. total, 0 ) ;
35
34
36
35
// Create a category and a subcategory
37
- {
38
- let conn = t ! ( app. diesel_database. get( ) ) ;
36
+ app. db ( |conn| {
39
37
new_category ( "foo" , "foo" , "Foo crates" )
40
- . create_or_update ( & conn)
38
+ . create_or_update ( conn)
41
39
. unwrap ( ) ;
42
40
new_category ( "foo::bar" , "foo::bar" , "Bar crates" )
43
- . create_or_update ( & conn)
41
+ . create_or_update ( conn)
44
42
. unwrap ( ) ;
45
- }
46
-
47
- let mut response = ok_resp ! ( middle. call( & mut req) ) ;
48
- let json: CategoryList = :: json ( & mut response) ;
43
+ } ) ;
49
44
50
45
// Only the top-level categories should be on the page
46
+ let json: CategoryList = anon. get ( url) . good ( ) ;
51
47
assert_eq ! ( json. categories. len( ) , 1 ) ;
52
48
assert_eq ! ( json. meta. total, 1 ) ;
53
49
assert_eq ! ( json. categories[ 0 ] . category, "foo" ) ;
54
50
}
55
51
56
52
#[ test]
57
53
fn show ( ) {
58
- let ( _b, app, middle) = app ( ) ;
54
+ let ( app, anon) = TestApp :: empty ( ) ;
55
+ let url = "/api/v1/categories/foo-bar" ;
59
56
60
57
// Return not found if a category doesn't exist
61
- let mut req = req ( Method :: Get , "/api/v1/categories/foo-bar" ) ;
62
- let response = t ! ( middle. call( & mut req) ) ;
63
- assert_eq ! ( response. status. 0 , 404 ) ;
58
+ anon. get ( & url) . assert_not_found ( ) ;
64
59
65
60
// Create a category and a subcategory
66
- {
67
- let conn = t ! ( app. diesel_database. get( ) ) ;
68
-
69
- t ! ( new_category( "Foo Bar" , "foo-bar" , "Foo Bar crates" ) . create_or_update( & conn) ) ;
70
- t ! ( new_category( "Foo Bar::Baz" , "foo-bar::baz" , "Baz crates" ) . create_or_update( & conn) ) ;
71
- }
61
+ app. db ( |conn| {
62
+ t ! ( new_category( "Foo Bar" , "foo-bar" , "Foo Bar crates" ) . create_or_update( conn) ) ;
63
+ t ! ( new_category( "Foo Bar::Baz" , "foo-bar::baz" , "Baz crates" ) . create_or_update( conn) ) ;
64
+ } ) ;
72
65
73
66
// The category and its subcategories should be in the json
74
- let mut response = ok_resp ! ( middle. call( & mut req) ) ;
75
- let json: CategoryWithSubcategories = :: json ( & mut response) ;
67
+ let json: CategoryWithSubcategories = anon. get ( & url) . good ( ) ;
76
68
assert_eq ! ( json. category. category, "Foo Bar" ) ;
77
69
assert_eq ! ( json. category. slug, "foo-bar" ) ;
78
70
assert_eq ! ( json. category. subcategories. len( ) , 1 ) ;
@@ -169,18 +161,15 @@ fn update_crate() {
169
161
170
162
#[ test]
171
163
fn category_slugs_returns_all_slugs_in_alphabetical_order ( ) {
172
- let ( _b, app, middle) = app ( ) ;
173
- {
174
- let conn = app. diesel_database . get ( ) . unwrap ( ) ;
164
+ let ( app, anon) = TestApp :: empty ( ) ;
165
+ app. db ( |conn| {
175
166
new_category ( "Foo" , "foo" , "For crates that foo" )
176
167
. create_or_update ( & conn)
177
168
. unwrap ( ) ;
178
169
new_category ( "Bar" , "bar" , "For crates that bar" )
179
170
. create_or_update ( & conn)
180
171
. unwrap ( ) ;
181
- }
182
-
183
- let mut req = req ( Method :: Get , "/api/v1/category_slugs" ) ;
172
+ } ) ;
184
173
185
174
#[ derive( Deserialize , Debug , PartialEq ) ]
186
175
struct Slug {
@@ -194,7 +183,7 @@ fn category_slugs_returns_all_slugs_in_alphabetical_order() {
194
183
category_slugs : Vec < Slug > ,
195
184
}
196
185
197
- let response = :: json ( & mut ok_resp ! ( middle . call ( & mut req ) ) ) ;
186
+ let response = anon . get ( "/api/v1/category_slugs" ) . good ( ) ;
198
187
let expected_response = Slugs {
199
188
category_slugs : vec ! [
200
189
Slug {
0 commit comments