@@ -87,6 +87,70 @@ def read(self):
8787 # Response returned when no dcids are given.
8888 return MockResponse (json .dumps ({'payload' : res_json }))
8989
90+
91+ # Mock responses for urlopen requests to get_stats.
92+ if req .full_url == utils ._API_ROOT + utils ._API_ENDPOINTS ['get_stats' ]:
93+ if (data ['place' ] == ['geoId/05' , 'geoId/06' ] and
94+ data ['stats_var' ] == 'dc/0hyp6tkn18vcb' ):
95+ # Response returned when querying for multiple valid dcids.
96+ res_json = json .dumps ({
97+ 'geoId/05' : {
98+ 'data' : {
99+ '2011' : 18136 ,
100+ '2012' : 17279 ,
101+ '2013' : 17459 ,
102+ '2014' : 16966 ,
103+ '2015' : 17173 ,
104+ '2016' : 17041 ,
105+ '2017' : 17783 ,
106+ '2018' : 18003
107+ },
108+ 'place_name' : 'Arkansas'
109+ },
110+ 'geoId/06' : {
111+ 'data' : {
112+ '2011' : 316667 ,
113+ '2012' : 324116 ,
114+ '2013' : 331853 ,
115+ '2014' : 342818 ,
116+ '2015' : 348979 ,
117+ '2016' : 354806 ,
118+ '2017' : 360645 ,
119+ '2018' : 366331
120+ },
121+ 'place_name' : 'California'
122+ }
123+ })
124+ return MockResponse (json .dumps ({'payload' : res_json }))
125+ if (data ['place' ] == ['geoId/05' , 'dc/MadDcid' ] and
126+ data ['stats_var' ] == 'dc/0hyp6tkn18vcb' ):
127+ # Response returned when querying for a dcid that does not exist.
128+ res_json = json .dumps ({
129+ 'geoId/05' : {
130+ 'data' : {
131+ '2011' : 18136 ,
132+ '2012' : 17279 ,
133+ '2013' : 17459 ,
134+ '2014' : 16966 ,
135+ '2015' : 17173 ,
136+ '2016' : 17041 ,
137+ '2017' : 17783 ,
138+ '2018' : 18003
139+ },
140+ 'place_name' : 'Arkansas'
141+ }
142+ })
143+ return MockResponse (json .dumps ({'payload' : res_json }))
144+ if (data ['place' ] == ['dc/MadDcid' , 'dc/MadderDcid' ] and
145+ data ['stats_var' ] == 'dc/0hyp6tkn18vcb' ):
146+ # Response returned when both given dcids do not exist.
147+ res_json = json .dumps ([])
148+ return MockResponse (json .dumps ({'payload' : res_json }))
149+ if data ['place' ] == [] and data ['stats_var' ] == 'dc/0hyp6tkn18vcb' :
150+ res_json = json .dumps ([])
151+ # Response returned when no dcids are given.
152+ return MockResponse (json .dumps ({'payload' : res_json }))
153+
90154 # Otherwise, return an empty response and a 404.
91155 return urllib .error .HTTPError
92156
@@ -142,5 +206,89 @@ def test_no_dcids(self, urlopen):
142206 })
143207
144208
209+ class TestGetStats (unittest .TestCase ):
210+ """ Unit stests for get_stats. """
211+
212+ @mock .patch ('urllib.request.urlopen' , side_effect = request_mock )
213+ def test_multiple_dcids (self , urlopen ):
214+ """ Calling get_stats with proper dcids returns valid results. """
215+ # Set the API key
216+ dc .set_api_key ('TEST-API-KEY' )
217+
218+ # Call get_stats
219+ stats = dc .get_stats (['geoId/05' , 'geoId/06' ], 'dc/0hyp6tkn18vcb' )
220+ self .assertDictEqual (
221+ stats , {
222+ 'geoId/05' : {
223+ 'data' : {
224+ '2011' : 18136 ,
225+ '2012' : 17279 ,
226+ '2013' : 17459 ,
227+ '2014' : 16966 ,
228+ '2015' : 17173 ,
229+ '2016' : 17041 ,
230+ '2017' : 17783 ,
231+ '2018' : 18003
232+ },
233+ 'place_name' : 'Arkansas'
234+ },
235+ 'geoId/06' : {
236+ 'data' : {
237+ '2011' : 316667 ,
238+ '2012' : 324116 ,
239+ '2013' : 331853 ,
240+ '2014' : 342818 ,
241+ '2015' : 348979 ,
242+ '2016' : 354806 ,
243+ '2017' : 360645 ,
244+ '2018' : 366331
245+ },
246+ 'place_name' : 'California'
247+ }
248+ })
249+
250+ @mock .patch ('urllib.request.urlopen' , side_effect = request_mock )
251+ def test_bad_dcids (self , urlopen ):
252+ """ Calling get_stats with dcids that do not exist returns empty
253+ results.
254+ """
255+ # Set the API key
256+ dc .set_api_key ('TEST-API-KEY' )
257+
258+ # Call get_stats with one dcid that does not exist
259+ bad_dcids_1 = dc .get_stats (['geoId/05' , 'dc/MadDcid' ], 'dc/0hyp6tkn18vcb' )
260+ self .assertDictEqual (
261+ bad_dcids_1 , {
262+ 'geoId/05' : {
263+ 'data' : {
264+ '2011' : 18136 ,
265+ '2012' : 17279 ,
266+ '2013' : 17459 ,
267+ '2014' : 16966 ,
268+ '2015' : 17173 ,
269+ '2016' : 17041 ,
270+ '2017' : 17783 ,
271+ '2018' : 18003
272+ },
273+ 'place_name' : 'Arkansas'
274+ }
275+ })
276+
277+ # Call get_stats when both dcids do not exist
278+ bad_dcids_2 = dc .get_stats (['dc/MadDcid' , 'dc/MadderDcid' ],
279+ 'dc/0hyp6tkn18vcb' )
280+ self .assertFalse (bad_dcids_2 )
281+
282+ @mock .patch ('urllib.request.urlopen' , side_effect = request_mock )
283+ def test_no_dcids (self , urlopen ):
284+ """ Calling get_stats with no dcids returns empty results. """
285+ # Set the API key
286+ dc .set_api_key ('TEST-API-KEY' )
287+
288+ # Call get_stats with no dcids.
289+ no_dcids = dc .get_stats ([], 'dc/0hyp6tkn18vcb' )
290+ self .assertFalse (no_dcids )
291+
292+
145293if __name__ == '__main__' :
146294 unittest .main ()
0 commit comments