Skip to content

Commit

Permalink
[#1457][tests] Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jan 16, 2012
1 parent 0479996 commit 49519f4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 37 deletions.
69 changes: 64 additions & 5 deletions ckan/tests/functional/api/test_action.py
Expand Up @@ -12,6 +12,10 @@
from ckan.tests import setup_test_search_index, search_related
from ckan.logic import get_action, NotAuthorized

from ckan import plugins
from ckan.plugins import SingletonPlugin, implements, IPackageController


class TestAction(WsgiAppCase):

STATUS_200_OK = 200
Expand Down Expand Up @@ -283,7 +287,7 @@ def test_06_tag_list(self):
number_of_russian_packages = len(res_obj['result'][russian_index]['packages']) # warandpeace, annakarenina (moo?)
number_of_tolstoy_packages = len(res_obj['result'][tolstoy_index]['packages']) # annakarenina
number_of_flexible_packages = len(res_obj['result'][flexible_index]['packages']) # warandpeace, annakarenina (moo?)

# Assert we have the correct number of packages, independantly of
# whether the "moo" package may exist or not.
assert number_of_russian_packages - number_of_tolstoy_packages == 1
Expand Down Expand Up @@ -652,7 +656,7 @@ def test_15_tag_autocomplete_tag_with_spaces(self):

def test_15_tag_autocomplete_tag_with_foreign_characters(self):
"""Asserts autocomplete finds tags that contain foreign characters"""

CreateTestData.create_arbitrary([{
'name': u'package-with-tag-that-has-a-foreign-character-1',
'tags': [u'greek beta \u03b2'],
Expand All @@ -667,7 +671,7 @@ def test_15_tag_autocomplete_tag_with_foreign_characters(self):

def test_15_tag_autocomplete_tag_with_punctuation(self):
"""Asserts autocomplete finds tags that contain punctuation"""

CreateTestData.create_arbitrary([{
'name': u'package-with-tag-that-has-a-fullstop-1',
'tags': [u'fullstop.'],
Expand All @@ -684,7 +688,7 @@ def test_15_tag_autocomplete_tag_with_capital_letters(self):
"""
Asserts autocomplete finds tags that contain capital letters
"""

CreateTestData.create_arbitrary([{
'name': u'package-with-tag-that-has-a-capital-letter-1',
'tags': [u'CAPITAL idea old chap'],
Expand Down Expand Up @@ -1095,6 +1099,7 @@ def test_31_bad_request_format_not_json(self):
status=400)
assert "Bad request - Bad request data: Request data JSON decoded to '' but it needs to be a dictionary." in res.body, res.body


class TestActionPackageSearch(WsgiAppCase):

@classmethod
Expand Down Expand Up @@ -1179,10 +1184,64 @@ def test_4_sort_by_metadata_modified(self):
pkg_params = '%s=1' % json.dumps(pkg_dict)
res = self.app.post('/api/action/package_update', params=pkg_params,
extra_environ={'Authorization': 'tester'})

res = self.app.post('/api/action/package_search', params=search_params)
result = json.loads(res.body)['result']
result_names = [r['name'] for r in result['results']]
assert result_names == ['warandpeace', 'annakarenina'], result_names

class MockPackageSearchPlugin(SingletonPlugin):
implements(IPackageController, inherit=True)

def before_search(self, search_params):
if 'extras' in search_params and 'ext_avoid' in search_params['extras']:
assert 'q' in search_params
return search_params

def after_search(self, search_results, search_params):

assert 'results' in search_results
assert 'count' in search_results
assert 'facets' in search_results

if 'extras' in search_params and 'ext_avoid' in search_params['extras']:
# Remove results with a certain value
avoid = search_params['extras']['ext_avoid']

for i,result in enumerate(search_results['results']):
if avoid.lower() in result['name'].lower() or avoid.lower() in result['title'].lower():
search_results['results'].pop(i)
search_results['count'] -= 1

return search_results

class TestSearchPluginInterface(WsgiAppCase):

@classmethod
def setup_class(cls):
setup_test_search_index()
CreateTestData.create()

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()

def test_search_plugin_interface_search(self):
plugin = MockPackageSearchPlugin()
plugins.load(plugin)


avoid = 'Tolstoy'
search_params = '%s=1' % json.dumps({
'q': '*:*',
'extras' : {'ext_avoid':avoid}
})

res = self.app.post('/api/action/package_search', params=search_params)

results_dict = json.loads(res.body)['result']
for result in results_dict['results']:
assert not avoid.lower() in result['title'].lower()

assert results_dict['count'] == 1
plugins.unload(plugin)

0 comments on commit 49519f4

Please sign in to comment.