Skip to content

Commit

Permalink
[1724][logic, tests] update tag_list to filter by vocabulary
Browse files Browse the repository at this point in the history
  • Loading branch information
johnglover committed Feb 7, 2012
1 parent 7adca28 commit 335535b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ckan/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ def tag_list(context, data_dict):
else:
tags = model.Session.query(model.Tag).all()

# filter by vocabulary
if data_dict.get('vocabulary_name'):
vocab = model.Vocabulary.get(data_dict['vocabulary_name'])
if not vocab:
raise NotFound
tags = [tag for tag in tags if tag.vocabulary_id == vocab.id]
else:
tags = [tag for tag in tags if not tag.vocabulary_id]

tag_list = []
if all_fields:
for tag in tags:
Expand Down
33 changes: 32 additions & 1 deletion ckan/tests/logic/test_tag_vocab.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
from ckan import model
from ckan.logic.converters import convert_to_tags, convert_from_tags, free_tags_only
from ckan.lib.navl.dictization_functions import unflatten
from ckan.lib.create_test_data import CreateTestData
from ckan.tests import WsgiAppCase
from ckan.tests.functional.api import assert_dicts_equal_ignoring_ordering

class TestConverters:
class TestConverters(object):
@classmethod
def setup_class(cls):
# create a new vocabulary
Expand Down Expand Up @@ -52,3 +56,30 @@ def test_free_tags_only(self):
assert len(data) == 2
assert ('tags', 1, 'vocabulary_id') in data.keys()
assert ('tags', 1, '__extras') in data.keys()

class TestAPI(WsgiAppCase):
@classmethod
def setup_class(cls):
CreateTestData.create()
cls.vocab = model.Vocabulary('test-vocab')
model.Session.add(cls.vocab)
cls.vocab_tag = model.Tag('vocab-tag', cls.vocab.id)
model.Session.add(cls.vocab_tag)
model.Session.commit()
model.Session.remove()

def test_tag_list(self):
postparams = '%s=1' % json.dumps({'vocabulary_name': self.vocab.name})
res = self.app.post('/api/action/tag_list', params=postparams)
body = json.loads(res.body)
assert_dicts_equal_ignoring_ordering(
json.loads(res.body),
{'help': 'Returns a list of tags',
'success': True,
'result': [self.vocab_tag.name]}
)

def test_tag_list_invalid_vocab_name_404(self):
postparams = '%s=1' % json.dumps({'vocabulary_name': 'invalid-vocab-name'})
res = self.app.post('/api/action/tag_list', params=postparams, status=404)

0 comments on commit 335535b

Please sign in to comment.