@@ -26,9 +26,10 @@ def test_list_agencies(self):
26
26
agency = factories .AgencyFactory ()
27
27
url = reverse ("nc:agency-api-list" )
28
28
response = self .client .get (url , format = "json" )
29
+ response_data = response .json ()
29
30
self .assertEqual (response .status_code , status .HTTP_200_OK )
30
31
# Other Agencies may have been left around from other tests
31
- self .assertIn ((agency .pk , agency .name ), [(a ["id" ], a ["name" ]) for a in response . data ])
32
+ self .assertIn ((agency .pk , agency .name ), [(a ["id" ], a ["name" ]) for a in response_data ])
32
33
33
34
def test_agency_census_data (self ):
34
35
"""
@@ -39,19 +40,21 @@ def test_agency_census_data(self):
39
40
agency = factories .AgencyFactory (census_profile_id = census_profile .id )
40
41
url = reverse ("nc:agency-api-detail" , args = [agency .pk ])
41
42
response = self .client .get (url , format = "json" )
43
+ response_data = response .json ()
42
44
self .assertEqual (response .status_code , status .HTTP_200_OK )
43
- self .assertIn ("census_profile" , response . data )
45
+ self .assertIn ("census_profile" , response_data )
44
46
# CensusProfile tests check census data in more detail
45
47
for attr in ("hispanic" , "non_hispanic" , "total" ):
46
- self .assertEqual (response . data ["census_profile" ][attr ], getattr (census_profile , attr ))
48
+ self .assertEqual (response_data ["census_profile" ][attr ], getattr (census_profile , attr ))
47
49
48
50
def test_stops_api (self ):
49
51
"""Test Agency stops API endpoint with no stops"""
50
52
agency = factories .AgencyFactory ()
51
53
url = reverse ("nc:agency-api-stops" , args = [agency .pk ])
52
54
response = self .client .get (url , format = "json" )
55
+ response_data = response .json ()
53
56
self .assertEqual (response .status_code , status .HTTP_200_OK )
54
- self .assertEqual (len (response . data ), 0 )
57
+ self .assertEqual (len (response_data ), 0 )
55
58
56
59
def test_stops_count (self ):
57
60
"""Test Agency stop counts"""
@@ -73,18 +76,19 @@ def test_stops_count(self):
73
76
factories .PersonFactory (race = "I" , stop__agency = agency , ethnicity = "H" , stop__year = 2012 )
74
77
75
78
url = reverse ("nc:agency-api-stops" , args = [agency .pk ])
76
- response = self .client .get (url , format = "json" )
77
- self .assertEqual (len (response .data ), 2 )
79
+ response = self .client .get (url , format = "application/json" )
80
+ response_data = response .json ()
81
+ self .assertEqual (len (response_data ), 2 )
78
82
self .assertEqual (response .status_code , status .HTTP_200_OK )
79
- self .assertEqual (response . data [0 ]["year" ], 2010 )
80
- self .assertEqual (response . data [0 ]["black" ], 2 )
81
- self .assertEqual (response . data [0 ]["white" ], 1 )
82
- self .assertEqual (response . data [0 ]["asian" ], 0 )
83
- self .assertEqual (response . data [0 ]["hispanic" ], 3 )
84
- self .assertEqual (response . data [1 ]["year" ], 2012 )
85
- self .assertEqual (response . data [1 ]["black" ], 0 )
86
- self .assertEqual (response . data [1 ]["white" ], 1 )
87
- self .assertEqual (response . data [1 ]["hispanic" ], 4 )
83
+ self .assertEqual (response_data [0 ]["year" ], 2010 )
84
+ self .assertEqual (response_data [0 ]["black" ], 2 )
85
+ self .assertEqual (response_data [0 ]["white" ], 1 )
86
+ self .assertEqual (response_data [0 ]["asian" ], 0 )
87
+ self .assertEqual (response_data [0 ]["hispanic" ], 3 )
88
+ self .assertEqual (response_data [1 ]["year" ], 2012 )
89
+ self .assertEqual (response_data [1 ]["black" ], 0 )
90
+ self .assertEqual (response_data [1 ]["white" ], 1 )
91
+ self .assertEqual (response_data [1 ]["hispanic" ], 4 )
88
92
89
93
def test_grouping_by_year (self ):
90
94
"""
@@ -111,12 +115,13 @@ def test_grouping_by_year(self):
111
115
)
112
116
url = reverse ("nc:agency-api-stops" , args = [agency .pk ])
113
117
response = self .client .get (url , format = "json" )
118
+ response_data = response .json ()
114
119
self .assertEqual (response .status_code , status .HTTP_200_OK )
115
- self .assertEqual (len (response . data ), 2 )
116
- self .assertEqual (response . data [0 ]["year" ], year )
117
- self .assertEqual (response . data [0 ][race_label ], 1 )
118
- self .assertEqual (response . data [1 ]["year" ], year + 1 )
119
- self .assertEqual (response . data [1 ]["hispanic" ], 1 )
120
+ self .assertEqual (len (response_data ), 2 )
121
+ self .assertEqual (response_data [0 ]["year" ], year )
122
+ self .assertEqual (response_data [0 ][race_label ], 1 )
123
+ self .assertEqual (response_data [1 ]["year" ], year + 1 )
124
+ self .assertEqual (response_data [1 ]["hispanic" ], 1 )
120
125
121
126
def test_officer_stops_count (self ):
122
127
"""Test officer (within an agency) stop counts"""
@@ -131,12 +136,12 @@ def test_officer_stops_count(self):
131
136
url = reverse ("nc:agency-api-stops" , args = [agency .pk ])
132
137
url = "{}?officer={}" .format (url , p1 .stop .officer_id )
133
138
response = self .client .get (url , format = "json" )
139
+ response_data = response .json ()
134
140
self .assertEqual (response .status_code , status .HTTP_200_OK )
135
- self .assertEqual (len (response .data ), 2 )
136
- self .assertEqual (response .data [0 ]["year" ], p1 .stop .date .year )
137
- self .assertEqual (response .data [0 ][GROUPS [p1 .race ]], 1 )
138
- self .assertEqual (response .data [1 ]["year" ], p2 .stop .date .year )
139
- self .assertEqual (response .data [1 ]["hispanic" ], 2 )
141
+ self .assertEqual (len (response_data ), 2 )
142
+ self .assertEqual (response_data [0 ]["year" ], p1 .stop .date .year )
143
+ self .assertEqual (response_data [1 ]["year" ], p2 .stop .date .year )
144
+ self .assertEqual (response_data [1 ]["hispanic" ], 2 )
140
145
141
146
def test_stops_by_reason (self ):
142
147
"""Test Agency stops_by_reason API endpoint"""
@@ -189,10 +194,11 @@ def test_stops_by_reason(self):
189
194
factories .SearchFactory (stop = p5 .stop )
190
195
191
196
response = self .client .get (url , format = "json" )
197
+ response_data = response .json ()
192
198
self .assertEqual (response .status_code , status .HTTP_200_OK )
193
- self .assertEqual (len (response . data .keys ()), 2 )
199
+ self .assertEqual (len (response_data .keys ()), 2 )
194
200
195
- searches = response . data ["searches" ]
201
+ searches = response_data ["searches" ]
196
202
self .assertEqual (searches [0 ]["year" ], 2010 )
197
203
self .assertEqual (searches [0 ]["black" ], 0 )
198
204
self .assertEqual (searches [0 ]["hispanic" ], 3 )
@@ -201,7 +207,7 @@ def test_stops_by_reason(self):
201
207
self .assertEqual (searches [1 ]["black" ], 1 )
202
208
self .assertEqual (searches [1 ]["purpose" ], purpose_label )
203
209
204
- stops = response . data ["stops" ]
210
+ stops = response_data ["stops" ]
205
211
self .assertEqual (stops [0 ]["year" ], 2010 )
206
212
self .assertEqual (stops [0 ]["black" ], 1 )
207
213
self .assertEqual (stops [0 ]["hispanic" ], 3 )
@@ -227,16 +233,17 @@ def test_searches(self):
227
233
factories .SearchFactory (person = p5 , stop = p5 .stop )
228
234
url = reverse ("nc:agency-api-searches" , args = [agency .pk ])
229
235
response = self .client .get (url , format = "json" )
236
+ response_data = response .json ()
230
237
self .assertEqual (response .status_code , status .HTTP_200_OK )
231
- self .assertEqual (len (response . data ), 2 )
238
+ self .assertEqual (len (response_data ), 2 )
232
239
# Everyone got searched, so the expected racial data for 2015 are: 1 black,
233
240
# and for 2016 are: 1 native american, 3 hispanic
234
- self .assertEqual (response . data [0 ]["year" ], s1 .stop .date .year )
235
- self .assertEqual (response . data [0 ]["black" ], 1 )
236
- self .assertEqual (response . data [1 ]["year" ], s2 .stop .date .year )
237
- self .assertEqual (response . data [1 ]["black" ], 0 )
238
- self .assertEqual (response . data [1 ]["native_american" ], 1 )
239
- self .assertEqual (response . data [1 ]["hispanic" ], 3 )
241
+ self .assertEqual (response_data [0 ]["year" ], s1 .stop .date .year )
242
+ self .assertEqual (response_data [0 ]["black" ], 1 )
243
+ self .assertEqual (response_data [1 ]["year" ], s2 .stop .date .year )
244
+ self .assertEqual (response_data [1 ]["black" ], 0 )
245
+ self .assertEqual (response_data [1 ]["native_american" ], 1 )
246
+ self .assertEqual (response_data [1 ]["hispanic" ], 3 )
240
247
241
248
def test_searches_by_reason (self ):
242
249
agency = factories .AgencyFactory ()
@@ -259,18 +266,18 @@ def test_searches_by_reason(self):
259
266
factories .SearchFactory (person = p5 , stop = p5 .stop , type = type_code )
260
267
261
268
response = self .client .get (url , format = "json" )
269
+ response_data = response .json ()
262
270
self .assertEqual (response .status_code , status .HTTP_200_OK )
263
271
# Two years = two items
264
- self .assertEqual (len (response . data ), 2 )
272
+ self .assertEqual (len (response_data ), 2 )
265
273
266
- searches = response .data
267
- self .assertEqual (searches [0 ]["year" ], 2015 )
268
- self .assertEqual (searches [0 ]["black" ], 1 )
269
- self .assertEqual (searches [0 ]["search_type" ], type_label )
270
- self .assertEqual (searches [1 ]["year" ], 2016 )
271
- self .assertEqual (searches [1 ]["hispanic" ], 3 )
272
- self .assertEqual (searches [1 ]["native_american" ], 1 )
273
- self .assertEqual (searches [1 ]["search_type" ], type_label )
274
+ self .assertEqual (response_data [0 ]["year" ], 2015 )
275
+ self .assertEqual (response_data [0 ]["black" ], 1 )
276
+ self .assertEqual (response_data [0 ]["search_type" ], type_label )
277
+ self .assertEqual (response_data [1 ]["year" ], 2016 )
278
+ self .assertEqual (response_data [1 ]["hispanic" ], 3 )
279
+ self .assertEqual (response_data [1 ]["native_american" ], 1 )
280
+ self .assertEqual (response_data [1 ]["search_type" ], type_label )
274
281
275
282
def test_use_of_force (self ):
276
283
pass
0 commit comments