22
33from nose .tools import assert_equals , assert_in
44import mock
5+ import json
56
67from ckanext .datastore .tests .helpers import DatastoreFunctionalTestBase
78import ckan .tests .helpers as helpers
@@ -428,6 +429,21 @@ def test_dump_xml(self):
428429 )
429430 assert_equals (content , expected_content )
430431
432+ @helpers .change_config ('ckan.datastore.search.rows_max' , '3' )
433+ def test_dump_with_low_rows_max (self ):
434+ resource = factories .Resource ()
435+ data = {
436+ 'resource_id' : resource ['id' ],
437+ 'force' : True ,
438+ 'records' : [{u'record' : str (num )} for num in range (12 )],
439+ }
440+ helpers .call_action ('datastore_create' , ** data )
441+
442+ app = self ._get_test_app ()
443+ response = app .get ('/datastore/dump/{0}' .format (str (resource ['id' ])))
444+ assert_equals (get_csv_record_values (response .body ),
445+ range (12 ))
446+
431447 @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 5 )
432448 def test_dump_pagination (self ):
433449 resource = factories .Resource ()
@@ -440,12 +456,10 @@ def test_dump_pagination(self):
440456
441457 app = self ._get_test_app ()
442458 response = app .get ('/datastore/dump/{0}' .format (str (resource ['id' ])))
443- assert_equals (
444- '_id,record\r \n '
445- '1,0\n 2,1\n 3,2\n 4,3\n 5,4\n 6,5\n 7,6\n 8,7\n 9,8\n 10,9\n '
446- '11,10\n 12,11\n ' ,
447- response .body )
459+ assert_equals (get_csv_record_values (response .body ),
460+ range (12 ))
448461
462+ @helpers .change_config ('ckan.datastore.search.rows_max' , '7' )
449463 @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 5 )
450464 def test_dump_pagination_csv_with_limit (self ):
451465 resource = factories .Resource ()
@@ -456,14 +470,62 @@ def test_dump_pagination_csv_with_limit(self):
456470 }
457471 helpers .call_action ('datastore_create' , ** data )
458472
473+ app = self ._get_test_app ()
474+ response = app .get ('/datastore/dump/{0}?limit=11' .format (
475+ str (resource ['id' ])))
476+ assert_equals (get_csv_record_values (response .body ),
477+ range (11 ))
478+
479+ @helpers .change_config ('ckan.datastore.search.rows_max' , '7' )
480+ @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 6 )
481+ def test_dump_pagination_csv_with_limit_same_as_paginate (self ):
482+ resource = factories .Resource ()
483+ data = {
484+ 'resource_id' : resource ['id' ],
485+ 'force' : True ,
486+ 'records' : [{u'record' : str (num )} for num in range (12 )],
487+ }
488+ helpers .call_action ('datastore_create' , ** data )
489+
459490 app = self ._get_test_app ()
460491 response = app .get ('/datastore/dump/{0}?limit=6' .format (
461492 str (resource ['id' ])))
462- assert_equals (
463- '_id,record\r \n '
464- '1,0\n 2,1\n 3,2\n 4,3\n 5,4\n 6,5\n ' ,
465- response .body )
493+ assert_equals (get_csv_record_values (response .body ),
494+ range (6 ))
466495
496+ @helpers .change_config ('ckan.datastore.search.rows_max' , '6' )
497+ @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 5 )
498+ def test_dump_pagination_with_rows_max (self ):
499+ resource = factories .Resource ()
500+ data = {
501+ 'resource_id' : resource ['id' ],
502+ 'force' : True ,
503+ 'records' : [{u'record' : str (num )} for num in range (12 )],
504+ }
505+ helpers .call_action ('datastore_create' , ** data )
506+
507+ app = self ._get_test_app ()
508+ response = app .get ('/datastore/dump/{0}?limit=7' .format (str (resource ['id' ])))
509+ assert_equals (get_csv_record_values (response .body ),
510+ range (7 ))
511+
512+ @helpers .change_config ('ckan.datastore.search.rows_max' , '6' )
513+ @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 6 )
514+ def test_dump_pagination_with_rows_max_same_as_paginate (self ):
515+ resource = factories .Resource ()
516+ data = {
517+ 'resource_id' : resource ['id' ],
518+ 'force' : True ,
519+ 'records' : [{u'record' : str (num )} for num in range (12 )],
520+ }
521+ helpers .call_action ('datastore_create' , ** data )
522+
523+ app = self ._get_test_app ()
524+ response = app .get ('/datastore/dump/{0}?limit=7' .format (str (resource ['id' ])))
525+ assert_equals (get_csv_record_values (response .body ),
526+ range (7 ))
527+
528+ @helpers .change_config ('ckan.datastore.search.rows_max' , '7' )
467529 @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 5 )
468530 def test_dump_pagination_json_with_limit (self ):
469531 resource = factories .Resource ()
@@ -477,9 +539,32 @@ def test_dump_pagination_json_with_limit(self):
477539 app = self ._get_test_app ()
478540 response = app .get ('/datastore/dump/{0}?limit=6&format=json' .format (
479541 str (resource ['id' ])))
480- assert_equals (
481- '{\n "fields": [{"type":"int","id":"_id"},'
482- '{"type":"int4","id":"record"}],\n '
483- ' "records": [\n [1,0],\n [2,1],\n [3,2],\n [4,3],\n '
484- ' [5,4],\n [6,5]\n ]}\n ' ,
485- response .body )
542+ assert_equals (get_json_record_values (response .body ),
543+ range (6 ))
544+
545+ @helpers .change_config ('ckan.datastore.search.rows_max' , '6' )
546+ @mock .patch ('ckanext.datastore.controller.PAGINATE_BY' , 5 )
547+ def test_dump_pagination_json_with_rows_max (self ):
548+ resource = factories .Resource ()
549+ data = {
550+ 'resource_id' : resource ['id' ],
551+ 'force' : True ,
552+ 'records' : [{u'record' : str (num )} for num in range (12 )],
553+ }
554+ helpers .call_action ('datastore_create' , ** data )
555+
556+ app = self ._get_test_app ()
557+ response = app .get ('/datastore/dump/{0}?limit=7&format=json' .format (
558+ str (resource ['id' ])))
559+ assert_equals (get_json_record_values (response .body ),
560+ range (7 ))
561+
562+
563+ def get_csv_record_values (response_body ):
564+ return [int (record .split (',' )[1 ])
565+ for record in response_body .split ()[1 :]]
566+
567+
568+ def get_json_record_values (response_body ):
569+ return [record [1 ]
570+ for record in json .loads (response_body )['records' ]]
0 commit comments