11# -*- coding: utf-8 -*-
22import csv
33import json
4- from datetime import datetime
54
65from nose .tools import eq_
76
@@ -294,21 +293,28 @@ def _test_cache_control(self):
294293 'Bad or no cache-control: %r' % response .get ('cache-control' , '' ))
295294
296295
297- class TestJSON (StatsTest , amo .tests .ESTestCase ):
296+ class TestResponses (StatsTest , amo .tests .ESTestCase ):
298297 es = True
299298
300299 def setUp (self ):
301- super (TestJSON , self ).setUp ()
300+ super (TestResponses , self ).setUp ()
302301 self .index ()
303302
303+ def csv_eq (self , response , expected ):
304+ # Drop the first 4 lines, which contain the header comment.
305+ content = response .content .splitlines ()[4 :]
306+ # Strip any extra spaces from the expected content.
307+ expected = [line .strip () for line in expected .splitlines ()]
308+ self .assertListEqual (content , expected )
309+
304310 def index (self ):
305311 updates = UpdateCount .objects .values_list ('id' , flat = True )
306312 tasks .index_update_counts (list (updates ))
307313 downloads = DownloadCount .objects .values_list ('id' , flat = True )
308314 tasks .index_download_counts (list (downloads ))
309315 self .refresh ('update_counts' )
310316
311- def test_usage (self ):
317+ def test_usage_json (self ):
312318 r = self .get_view_response ('stats.usage_series' , group = 'day' ,
313319 format = 'json' )
314320 eq_ (r .status_code , 200 )
@@ -317,7 +323,16 @@ def test_usage(self):
317323 {'count' : 1000 , 'date' : '2009-06-01' , 'end' : '2009-06-01' },
318324 ])
319325
320- def test_usage_by_app (self ):
326+ def test_usage_csv (self ):
327+ r = self .get_view_response ('stats.usage_series' , group = 'day' ,
328+ format = 'csv' )
329+ eq_ (r .status_code , 200 )
330+ self .csv_eq (r ,
331+ """date,count
332+ 2009-06-02,1500
333+ 2009-06-01,1000""" )
334+
335+ def test_usage_by_app_json (self ):
321336 r = self .get_view_response ('stats.apps_series' , group = 'day' ,
322337 format = 'json' )
323338 eq_ (r .status_code , 200 )
@@ -340,7 +355,15 @@ def test_usage_by_app(self):
340355 }
341356 ])
342357
343- def test_usage_by_locale (self ):
358+ def test_usage_by_app_csv (self ):
359+ r = self .get_view_response ('stats.apps_series' , group = 'day' ,
360+ format = 'csv' )
361+ eq_ (r .status_code , 200 )
362+ self .csv_eq (r , """date,count,{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
363+ 2009-06-02,1500,{u'4.0': 1500}
364+ 2009-06-01,1000,{u'4.0': 1000}""" )
365+
366+ def test_usage_by_locale_json (self ):
344367 r = self .get_view_response ('stats.locales_series' , group = 'day' ,
345368 format = 'json' )
346369 eq_ (r .status_code , 200 )
@@ -365,7 +388,15 @@ def test_usage_by_locale(self):
365388 }
366389 ])
367390
368- def test_usage_by_os (self ):
391+ def test_usage_by_locale_csv (self ):
392+ r = self .get_view_response ('stats.locales_series' , group = 'day' ,
393+ format = 'csv' )
394+ eq_ (r .status_code , 200 )
395+ self .csv_eq (r , """date,count,English (US) (en-us),Ελληνικά (el)
396+ 2009-06-02,1500,300,400
397+ 2009-06-01,1000,300,400""" )
398+
399+ def test_usage_by_os_json (self ):
369400 r = self .get_view_response ('stats.os_series' , group = 'day' ,
370401 format = 'json' )
371402 eq_ (r .status_code , 200 )
@@ -390,7 +421,12 @@ def test_usage_by_os(self):
390421 }
391422 ])
392423
393- def test_usage_by_version (self ):
424+ def test_usage_by_os_csv (self ):
425+ r = self .get_view_response ('stats.os_series' , group = 'day' ,
426+ format = 'csv' )
427+ eq_ (r .status_code , 200 )
428+
429+ def test_usage_by_version_json (self ):
394430 r = self .get_view_response ('stats.versions_series' , group = 'day' ,
395431 format = 'json' )
396432 eq_ (r .status_code , 200 )
@@ -415,7 +451,15 @@ def test_usage_by_version(self):
415451 }
416452 ])
417453
418- def test_usage_by_status (self ):
454+ def test_usage_by_version_csv (self ):
455+ r = self .get_view_response ('stats.versions_series' , group = 'day' ,
456+ format = 'csv' )
457+ eq_ (r .status_code , 200 )
458+ self .csv_eq (r , """date,count,2.0,1.0
459+ 2009-06-02,1500,950,550
460+ 2009-06-01,1000,800,200""" )
461+
462+ def test_usage_by_status_json (self ):
419463 r = self .get_view_response ('stats.statuses_series' , group = 'day' ,
420464 format = 'json' )
421465 eq_ (r .status_code , 200 )
@@ -440,6 +484,14 @@ def test_usage_by_status(self):
440484 }
441485 ])
442486
487+ def test_usage_by_status_csv (self ):
488+ r = self .get_view_response ('stats.statuses_series' , group = 'day' ,
489+ format = 'csv' )
490+ eq_ (r .status_code , 200 )
491+ self .csv_eq (r , """date,count,userEnabled,userDisabled
492+ 2009-06-02,1500,1370,130
493+ 2009-06-01,1000,950,50""" )
494+
443495 def test_overview (self ):
444496 r = self .get_view_response ('stats.overview_series' , group = 'day' ,
445497 format = 'json' )
@@ -523,8 +575,7 @@ def test_overview(self):
523575 {'downloads' : 0 , 'updates' : 0 })
524576 next_actual = next (actual )
525577
526-
527- def test_downloads (self ):
578+ def test_downloads_json (self ):
528579 r = self .get_view_response ('stats.downloads_series' , group = 'day' ,
529580 format = 'json' )
530581 eq_ (r .status_code , 200 )
@@ -539,7 +590,21 @@ def test_downloads(self):
539590 {"count" : 10 , "date" : "2009-06-01" , "end" : "2009-06-01" },
540591 ])
541592
542- def test_downloads_sources (self ):
593+ def test_downloads_csv (self ):
594+ r = self .get_view_response ('stats.downloads_series' , group = 'day' ,
595+ format = 'csv' )
596+ eq_ (r .status_code , 200 )
597+ self .csv_eq (r , """date,count
598+ 2009-09-03,10
599+ 2009-08-03,10
600+ 2009-07-03,10
601+ 2009-06-28,10
602+ 2009-06-20,10
603+ 2009-06-12,10
604+ 2009-06-07,10
605+ 2009-06-01,10""" )
606+
607+ def test_downloads_sources_json (self ):
543608 r = self .get_view_response ('stats.sources_series' , group = 'day' ,
544609 format = 'json' )
545610 eq_ (r .status_code , 200 )
@@ -585,3 +650,17 @@ def test_downloads_sources(self):
585650 "data" : {"api" : 2 , "search" : 3 }
586651 }
587652 ])
653+
654+ def test_downloads_sources_csv (self ):
655+ r = self .get_view_response ('stats.sources_series' , group = 'day' ,
656+ format = 'csv' )
657+ eq_ (r .status_code , 200 )
658+ self .csv_eq (r , """date,count,search,api
659+ 2009-09-03,10,3,2
660+ 2009-08-03,10,3,2
661+ 2009-07-03,10,3,2
662+ 2009-06-28,10,3,2
663+ 2009-06-20,10,3,2
664+ 2009-06-12,10,3,2
665+ 2009-06-07,10,3,2
666+ 2009-06-01,10,3,2""" )
0 commit comments