Commits

johnbywater  committed 94b0e25

Changed API v2 package search results to return IDs (was names).

  • Participants
  • Parent commits 16b227a

Comments (0)

Files changed (5)

File ckan/controllers/rest.py

 
 class BaseRestController(BaseController):
 
-    def index(self):
-        return render('rest/index.html')
+    ref_package_with_attr = 'id'
 
     def _list_package_refs(self, packages):
-        raise Exception, "Method not implemented."
+        return [getattr(p, self.ref_package_with_attr) for p in packages]
 
     def _get_pkg(self, id):
         pkg = model.Session.query(model.Package).get(id)
             # Todo: Make sure package names can't be changed to look like package IDs?
         return pkg
 
+    def index(self):
+        return render('rest/index.html')
+
     def list(self, register, subregister=None, id=None):
         if register == 'revision':
             revs = model.Session.query(model.Revision).all()
             options = SearchOptions(params)
             options.search_tags = False
             options.return_objects = False
+            options.ref_entity_with_attr = self.ref_package_with_attr
             username = self._get_username()
             results = make_search().run(options, username)
             return self._finish_ok(results)
             return ''
 
 class RestController(BaseRestController):
+    # Implements CKAN API Version 1.
 
-    def _list_package_refs(self, packages):
-        return [package.name for package in packages]
+    ref_package_with_attr = 'name'
 
 
+

File ckan/controllers/rest2.py

 
 class Rest2Controller(BaseRestController):
 
-    def _list_package_refs(self, packages):
-        return [package.id for package in packages]
+    ref_package_with_attr = 'id'
 

File ckan/lib/search.py

     order_by = 'rank'
     all_fields = False
     return_objects = False
+    ref_entity_with_attr = 'name'
 
     def __init__(self, kw_dict):
         if not kw_dict.keys():
                     results.append(result)
                 self._results['results'] = results
             else:
-                self._results['results'] = [entity.name for entity in self._results['results']]
+                attr_name = self._options.ref_entity_with_attr
+                self._results['results'] = [getattr(entity, attr_name) for entity in self._results['results']]
     
     def index_package(self, package):
         pass
                     "res_url", "text", "urls", "indexed_ts"]
 
     def __init__(self, solr_url=None):
-        if solr_url is None: 
+        if solr_url is None:
             solr_url = config.get('solr_url', 'http://localhost:8983/solr')
         # import inline to avoid external dependency 
         from solr import SolrConnection # == solrpy 

File ckan/tests/functional/test_rest.py

             assert license['url'] == license.url
 
 
+# For CKAN API Version 1.
 class TestRest(RestTestCase):
-    # Tests for CKAN API v1.0.
 
     api_version = '1'
 
                                       [])
 
 
-class TestSearch(TestController):
+class BaseSearchCase(TestController):
+
+    api_version = ''
+
+    @classmethod
+    def offset(self, path):
+        assert self.api_version, "API version is missing."
+        return '/api/%s%s' % (self.api_version, path)
+
     @classmethod
     def setup_class(self):
         try:
             pass
         model.Session.remove()
         CreateTestData.create()
-        self.base_url = '/api/search/package'
+        self.base_url = self.offset('/search/package')
 
     @classmethod
     def teardown_class(self):
         offset = self.base_url + '?q=%s' % self.testpackagevalues['name']
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'testpkg' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict['count']
 
+    def assert_package_search_results(self, results, names=[u'testpkg']):
+        raise Exception, "Method not implemented."
+
     def test_02_post_q(self):
         offset = self.base_url
         query = {'q':'testpkg'}
         res = self.app.post(offset, params=query, status=200)
         res_dict = json.loads(res.body)
-        assert u'testpkg' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict['count']
 
     def test_03_uri_qjson(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'testpkg' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict['count']
 
     def test_04_post_qjson(self):
         offset = self.base_url
         res = self.app.post(offset, params=json_query, status=200)
         res_dict = json.loads(res.body)
-        assert u'testpkg' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict['count']
 
     def test_05_uri_qjson_tags(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 1, res_dict
         
     def test_05_uri_qjson_tags_multiple(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 1, res_dict
 
     def test_06_uri_q_tags(self):
         offset = self.base_url + '?q=%s' % query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 1, res_dict['count']
 
     def test_07_uri_qjson_tags(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 1, res_dict
 
     def test_07_uri_qjson_tags_multiple(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 1, res_dict
 
     def test_07_uri_qjson_tags_reverse(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
-        assert u'annakarenina' in res_dict['results'], res_dict['results']
+        self.assert_package_search_results(res_dict['results'], names=[u'annakarenina'])
         assert res_dict['count'] == 2, res_dict
 
     def test_07_uri_qjson_extras(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict
 
     def test_07_uri_qjson_extras_2(self):
         offset = self.base_url + '?qjson=%s' % json_query
         res = self.app.get(offset, status=200)
         res_dict = json.loads(res.body)
+        self.assert_package_search_results(res_dict['results'])
         assert res_dict['count'] == 1, res_dict
         
         
         assert t == datetime.datetime(2012, 3, 4, 5, 6, 7, 890123), t
 
 
+# For CKAN API Version 1.
+class TestSearch(BaseSearchCase):
+
+    api_version = '1'
+
+    def assert_package_search_results(self, results, names=[u'testpkg']):
+        for name in names:
+            assert name in results, (name, results)
+
+
 class TestApiMisc(TestController):
     @classmethod
     def setup_class(self):

File ckan/tests/functional/test_rest2.py

 from ckan.tests.functional.test_rest import *
 
+# For CKAN API Version 2.
 class TestRest2(RestTestCase):
 
     api_version = '2'
         package = self.get_package_by_name(self.fixture_package_name)
         assert package.id in packages, (package.id, packages)
 
+
+# For CKAN API Version 2.
+class TestSearch2(BaseSearchCase):
+
+    api_version = '2'
+
+    def assert_package_search_results(self, results, names=[u'testpkg']):
+        for name in names:
+            package = self.get_package_by_name(name)
+            assert package.id in results, (package.id, results)
+