Skip to content

Commit

Permalink
[#2374] Made tag search pageable. Moved logic tag tests into its own …
Browse files Browse the repository at this point in the history
…file. Centralised related testing fixtures.
  • Loading branch information
David Read committed May 4, 2012
1 parent c943549 commit 5179455
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 547 deletions.
62 changes: 62 additions & 0 deletions ckan/lib/create_test_data.py
Expand Up @@ -628,6 +628,68 @@ def reset(cls):
def get_all_data(cls):
return cls.pkg_names + list(cls.group_names) + cls.tag_names + cls.user_refs

@classmethod
def make_some_vocab_tags(cls):
import ckan.model as model
model.repo.new_revision()

# Create a couple of vocabularies.
genre_vocab = model.Vocabulary(u'genre')
model.Session.add(genre_vocab)
composers_vocab = model.Vocabulary(u'composers')
model.Session.add(composers_vocab)

# Create some additional free tags for tag search tests.
tolkien_tag = model.Tag(name="tolkien")
model.Session.add(tolkien_tag)
toledo_tag = model.Tag(name="toledo")
model.Session.add(toledo_tag)
tolerance_tag = model.Tag(name="tolerance")
model.Session.add(tolerance_tag)
tollbooth_tag = model.Tag(name="tollbooth")
model.Session.add(tollbooth_tag)
# We have to add free tags to a package or they won't show up in tag results.
model.Package.get('warandpeace').add_tags((tolkien_tag, toledo_tag,
tolerance_tag, tollbooth_tag))

# Create some tags that belong to vocabularies.
sonata_tag = model.Tag(name=u'sonata', vocabulary_id=genre_vocab.id)
model.Session.add(sonata_tag)

bach_tag = model.Tag(name=u'Bach', vocabulary_id=composers_vocab.id)
model.Session.add(bach_tag)

neoclassical_tag = model.Tag(name='neoclassical',
vocabulary_id=genre_vocab.id)
model.Session.add(neoclassical_tag)

neofolk_tag = model.Tag(name='neofolk', vocabulary_id=genre_vocab.id)
model.Session.add(neofolk_tag)

neomedieval_tag = model.Tag(name='neomedieval',
vocabulary_id=genre_vocab.id)
model.Session.add(neomedieval_tag)

neoprog_tag = model.Tag(name='neoprog',
vocabulary_id=genre_vocab.id)
model.Session.add(neoprog_tag)

neopsychedelia_tag = model.Tag(name='neopsychedelia',
vocabulary_id=genre_vocab.id)
model.Session.add(neopsychedelia_tag)

neosoul_tag = model.Tag(name='neosoul', vocabulary_id=genre_vocab.id)
model.Session.add(neosoul_tag)

nerdcore_tag = model.Tag(name='nerdcore', vocabulary_id=genre_vocab.id)
model.Session.add(nerdcore_tag)

model.Package.get('warandpeace').add_tag(bach_tag)
model.Package.get('annakarenina').add_tag(sonata_tag)

model.Session.commit()



search_items = [{'name':'gils',
'title':'Government Information Locator Service',
Expand Down
17 changes: 10 additions & 7 deletions ckan/logic/action/get.py
Expand Up @@ -321,7 +321,7 @@ def tag_list(context, data_dict):
check_access('tag_list', context, data_dict)

if query:
tags = _tag_search(context, data_dict)
tags, count = _tag_search(context, data_dict)
else:
tags = model.Tag.all(vocab_id_or_name)

Expand Down Expand Up @@ -921,7 +921,8 @@ def resource_search(context, data_dict):
return {'count': count, 'results': results}

def _tag_search(context, data_dict):
'''Return a list of tag objects that contain the given string.
'''Return a list of tag objects that contain the given string and
the full count (for paging).
The query string should be provided in the data_dict with key 'query' or
'q'.
Expand All @@ -930,6 +931,7 @@ def _tag_search(context, data_dict):
searched. If a 'vocabulary_id' is provided in the data_dict then tags
belonging to the given vocabulary (id or name) will be searched instead.
Use 'offset' and 'limit' parameters to page through results.
'''
model = context['model']

Expand Down Expand Up @@ -963,15 +965,16 @@ def _tag_search(context, data_dict):
terms.append(value)

if not len(terms):
return []
return [], 0

for term in terms:
escaped_term = misc.escape_sql_like_special_characters(term, escape='\\')
q = q.filter(model.Tag.name.ilike('%' + escaped_term + '%'))

count = q.count()
q = q.offset(offset)
q = q.limit(limit)
return q.all()
return q.all(), count

def tag_search(context, data_dict):
'''Return a list of tag dictionaries that contain the given string.
Expand All @@ -987,8 +990,8 @@ def tag_search(context, data_dict):
and 'results' (the list of tag dicts).
'''
tags = _tag_search(context, data_dict)
return {'count': len(tags),
tags, count = _tag_search(context, data_dict)
return {'count': count,
'results': [table_dictize(tag, context) for tag in tags]}

def tag_autocomplete(context, data_dict):
Expand All @@ -1003,7 +1006,7 @@ def tag_autocomplete(context, data_dict):
'''
check_access('tag_autocomplete', context, data_dict)
matching_tags = _tag_search(context, data_dict)
matching_tags, count = _tag_search(context, data_dict)
if matching_tags:
return [tag.name for tag in matching_tags]
else:
Expand Down
9 changes: 9 additions & 0 deletions ckan/tests/__init__.py
Expand Up @@ -349,3 +349,12 @@ def assert_in(a, b):
assert a in b, '%r was not in %r' % (a, b)
def assert_not_in(a, b):
assert a not in b, '%r was in %r' % (a, b)

class StatusCodes:
STATUS_200_OK = 200
STATUS_201_CREATED = 201
STATUS_400_BAD_REQUEST = 400
STATUS_403_ACCESS_DENIED = 403
STATUS_404_NOT_FOUND = 404
STATUS_409_CONFLICT = 409

0 comments on commit 5179455

Please sign in to comment.