Skip to content

Commit

Permalink
[#547] Fix core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Apr 5, 2013
1 parent 2371c05 commit f871e87
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 312 deletions.
4 changes: 2 additions & 2 deletions ckan/tests/__init__.py
Expand Up @@ -312,11 +312,11 @@ def list(cls):
return [model.Package.get(pkg_index.package_id).name for pkg_index in model.Session.query(model.PackageSearch)]

def setup_test_search_index():
from ckan import plugins
#from ckan import plugins
if not is_search_supported():
raise SkipTest("Search not supported")
search.clear()
plugins.load('synchronous_search')
#plugins.load('synchronous_search')

def is_search_supported():
is_supported_db = not model.engine_is_sqlite()
Expand Down
@@ -1,9 +1,13 @@
[ckan.plugins]
routes_plugin = ckantestplugin:RoutesPlugin
authorizer_plugin = ckantestplugin:AuthorizerPlugin
auth_plugin = ckantestplugin:AuthPlugin
mapper_plugin2 = ckantestplugin:MapperPlugin2
mapper_plugin = ckantestplugin:MapperPlugin
test_observer_plugin = ckantestplugin:PluginObserverPlugin
action_plugin = ckantestplugin:ActionPlugin
auth_plugin = ckantestplugin:AuthPlugin
test_observer_plugin = ckantestplugin:PluginObserverPlugin
test_resource_preview = ckantestplugin:MockResourcePreviewExtension
test_package_controller_plugin = ckantestplugin:MockPackageControllerPlugin
test_json_resource_preview = ckantestplugin:JsonMockResourcePreviewExtension
authorizer_plugin = ckantestplugin:AuthorizerPlugin
mapper_plugin = ckantestplugin:MapperPlugin
test_group_plugin = ckantestplugin:MockGroupControllerPlugin

164 changes: 148 additions & 16 deletions ckan/tests/ckantestplugin/ckantestplugin/__init__.py
@@ -1,12 +1,13 @@
from ckan.plugins import SingletonPlugin, implements
from ckan.plugins import IMapper, IRoutes, IPluginObserver, IActions, IAuthFunctions
from ckan.tests.mock_plugin import MockSingletonPlugin
from collections import defaultdict

import ckan.plugins as p
import ckan.tests.mock_plugin as mock_plugin

class MapperPlugin(SingletonPlugin):
implements(IMapper, inherit=True)

def __init__(self):
class MapperPlugin(p.SingletonPlugin):
p.implements(p.IMapper, inherit=True)

def __init__(self, *args, **kw):
self.added = []
self.deleted = []

Expand All @@ -17,12 +18,12 @@ def before_delete(self, mapper, conn, instance):
self.deleted.append(instance)

class MapperPlugin2(MapperPlugin):
implements(IMapper)
p.implements(p.IMapper)

class RoutesPlugin(SingletonPlugin):
implements(IRoutes, inherit=True)
class RoutesPlugin(p.SingletonPlugin):
p.implements(p.IRoutes, inherit=True)

def __init__(self):
def __init__(self, *args, **kw):
self.calls_made = []

def before_map(self, map):
Expand All @@ -34,18 +35,149 @@ def after_map(self, map):
return map


class PluginObserverPlugin(MockSingletonPlugin):
implements(IPluginObserver)
class PluginObserverPlugin(mock_plugin.MockSingletonPlugin):
p.implements(p.IPluginObserver)

class ActionPlugin(SingletonPlugin):
implements(IActions)
class ActionPlugin(p.SingletonPlugin):
p.implements(p.IActions)

def get_actions(self):
return {'status_show': lambda context, data_dict: {}}

class AuthPlugin(SingletonPlugin):
implements(IAuthFunctions)
class AuthPlugin(p.SingletonPlugin):
p.implements(p.IAuthFunctions)

def get_auth_functions(self):
return {'package_list': lambda context, data_dict: {}}

class MockGroupControllerPlugin(p.SingletonPlugin):
p.implements(p.IGroupController)

def __init__(self, *args, **kw):
self.calls = defaultdict(int)

def read(self, entity):
self.calls['read'] += 1

def create(self, entity):
self.calls['create'] += 1

def edit(self, entity):
self.calls['edit'] += 1

def authz_add_role(self, object_role):
self.calls['authz_add_role'] += 1

def authz_remove_role(self, object_role):
self.calls['authz_remove_role'] += 1

def delete(self, entity):
self.calls['delete'] += 1

def before_view(self, data_dict):
self.calls['before_view'] += 1
return data_dict


class MockPackageControllerPlugin(p.SingletonPlugin):
p.implements(p.IPackageController)

def __init__(self, *args, **kw):
self.calls = defaultdict(int)

def read(self, entity):
self.calls['read'] += 1

def create(self, entity):
self.calls['create'] += 1

def edit(self, entity):
self.calls['edit'] += 1

def authz_add_role(self, object_role):
self.calls['authz_add_role'] += 1

def authz_remove_role(self, object_role):
self.calls['authz_remove_role'] += 1

def delete(self, entity):
self.calls['delete'] += 1

def before_search(self, search_params):
self.calls['before_search'] += 1
return search_params

def after_search(self, search_results, search_params):
self.calls['after_search'] += 1
return search_results

def before_index(self, data_dict):
self.calls['before_index'] += 1
return data_dict

def before_view(self, data_dict):
self.calls['before_view'] += 1
return data_dict

def after_create(self, context, data_dict):
self.calls['after_create'] += 1
self.id_in_dict = 'id' in data_dict

return data_dict

def after_update(self, context, data_dict):
self.calls['after_update'] += 1
return data_dict

def after_delete(self, context, data_dict):
self.calls['after_delete'] += 1
return data_dict

def after_show(self, context, data_dict):
self.calls['after_show'] += 1
return data_dict

def update_facet_titles(self, facet_titles):
return facet_titles



class MockResourcePreviewExtension(mock_plugin.MockSingletonPlugin):
p.implements(p.IResourcePreview)

def __init__(self, *args, **kw):
self.calls = defaultdict(int)

def can_preview(self, data_dict):
assert(isinstance(data_dict['resource'], dict))
assert(isinstance(data_dict['package'], dict))
assert('on_same_domain' in data_dict['resource'])

self.calls['can_preview'] += 1
return data_dict['resource']['format'].lower() == 'mock'

def setup_template_variables(self, context, data_dict):
self.calls['setup_template_variables'] += 1

def preview_template(self, context, data_dict):
assert(isinstance(data_dict['resource'], dict))
assert(isinstance(data_dict['package'], dict))

self.calls['preview_templates'] += 1
return 'tests/mock_resource_preview_template.html'


class JsonMockResourcePreviewExtension(MockResourcePreviewExtension):
def can_preview(self, data_dict):
super(JsonMockResourcePreviewExtension, self).can_preview(data_dict)
return data_dict['resource']['format'].lower() == 'json'

def preview_template(self, context, data_dict):
super(JsonMockResourcePreviewExtension, self).preview_template(context, data_dict)
self.calls['preview_templates'] += 1
return 'tests/mock_json_resource_preview_template.html'


# importing this file loads all these extensions by default
# so clean up the extensions
p.plugins_update()
6 changes: 5 additions & 1 deletion ckan/tests/ckantestplugin/setup.py
@@ -1,6 +1,5 @@
# After editing this file run python setup.py egg_info in this directory
from setuptools import setup, find_packages
import sys, os

version = '0.0'

Expand Down Expand Up @@ -30,6 +29,11 @@
'test_observer_plugin=ckantestplugin:PluginObserverPlugin',
'action_plugin=ckantestplugin:ActionPlugin',
'auth_plugin=ckantestplugin:AuthPlugin',
'test_group_plugin=ckantestplugin:MockGroupControllerPlugin',
'test_package_controller_plugin=ckantestplugin:MockPackageControllerPlugin',
'test_resource_preview=ckantestplugin:MockResourcePreviewExtension',
'test_json_resource_preview=ckantestplugin:JsonMockResourcePreviewExtension',

]
}
)
4 changes: 0 additions & 4 deletions ckan/tests/functional/api/model/test_package.py
Expand Up @@ -3,7 +3,6 @@
from nose.tools import assert_equal, assert_raises

from ckan.lib.create_test_data import CreateTestData
from ckan import plugins
import ckan.lib.search as search
from ckan.lib.search.common import SolrSettings

Expand Down Expand Up @@ -268,7 +267,6 @@ def test_register_post_indexerror(self):
bad_solr_url = 'http://127.0.0.1/badsolrurl'
original_settings = SolrSettings.get()[0]
try:
plugins.load('synchronous_search')
SolrSettings.init(bad_solr_url)

assert not self.get_package_by_name(self.package_fixture_data['name'])
Expand Down Expand Up @@ -659,14 +657,12 @@ def test_entity_update_indexerror(self):
bad_solr_url = 'http://127.0.0.1/badsolrurl'
original_settings = SolrSettings.get()[0]
try:
plugins.load('synchronous_search')
SolrSettings.init(bad_solr_url)

assert_raises(
search.SearchIndexError, self.assert_package_update_ok, 'name', 'post'
)
finally:
plugins.unload('synchronous_search')
SolrSettings.init(original_settings)

def test_package_update_delete_resource(self):
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/functional/api/test_util.py
Expand Up @@ -163,5 +163,5 @@ def test_status(self):
assert_equal(res['locale_default'], 'en')

assert_equal(type(res['extensions']), list)
expected_extensions = set(('stats', 'test_tag_vocab_plugin'))
expected_extensions = set(('stats',))
assert_equal(set(res['extensions']), expected_extensions)
56 changes: 15 additions & 41 deletions ckan/tests/functional/test_group.py
Expand Up @@ -17,35 +17,7 @@
from base import FunctionalTestCase
from ckan.tests import search_related, is_search_supported


class MockGroupControllerPlugin(SingletonPlugin):
implements(IGroupController)

def __init__(self):
from collections import defaultdict
self.calls = defaultdict(int)

def read(self, entity):
self.calls['read'] += 1

def create(self, entity):
self.calls['create'] += 1

def edit(self, entity):
self.calls['edit'] += 1

def authz_add_role(self, object_role):
self.calls['authz_add_role'] += 1

def authz_remove_role(self, object_role):
self.calls['authz_remove_role'] += 1

def delete(self, entity):
self.calls['delete'] += 1

def before_view(self, data_dict):
self.calls['before_view'] += 1
return data_dict
import ckan.tests.test_plugins as test_plugins


class TestGroup(FunctionalTestCase):
Expand All @@ -55,6 +27,7 @@ def setup_class(self):
search.clear()
model.Session.remove()
CreateTestData.create()
test_plugins.install_ckantestplugin()

@classmethod
def teardown_class(self):
Expand Down Expand Up @@ -215,14 +188,14 @@ def test_read_non_existent(self):
res = self.app.get(offset, status=404)

def test_read_plugin_hook(self):
plugin = MockGroupControllerPlugin()
plugins.load(plugin)
plugins.load('test_group_plugin')
name = u'david'
offset = url_for(controller='group', action='read', id=name)
res = self.app.get(offset, status=200,
extra_environ={'REMOTE_USER': 'testsysadmin'})
assert plugin.calls['read'] == 1, plugin.calls
plugins.unload(plugin)
p = plugins.get_plugin('test_group_plugin')
assert p.calls['read'] == 1, p.calls
plugins.unload('test_group_plugin')

def test_read_and_authorized_to_edit(self):
name = u'david'
Expand Down Expand Up @@ -288,6 +261,7 @@ def setup_class(self):
model.repo.new_revision()
model.Session.add(model.Package(name=self.packagename))
model.repo.commit_and_remove()
test_plugins.install_ckantestplugin()


@classmethod
Expand Down Expand Up @@ -388,8 +362,7 @@ def test_4_new_duplicate_package(self):
assert_equal(pkg_names, [self.packagename])

def test_edit_plugin_hook(self):
plugin = MockGroupControllerPlugin()
plugins.load(plugin)
plugins.load('test_group_plugin')
offset = url_for(controller='group', action='edit', id=self.groupname)
res = self.app.get(offset, status=200,
extra_environ={'REMOTE_USER': 'testsysadmin'})
Expand All @@ -398,8 +371,9 @@ def test_edit_plugin_hook(self):
form['title'] = "huhuhu"
res = form.submit('save', status=302,
extra_environ={'REMOTE_USER': 'testsysadmin'})
assert plugin.calls['edit'] == 1, plugin.calls
plugins.unload(plugin)
p = plugins.get_plugin('test_group_plugin')
assert p.calls['edit'] == 1, p.calls
plugins.unload('test_group_plugin')

def test_edit_image_url(self):
group = model.Group.by_name(self.groupname)
Expand Down Expand Up @@ -596,8 +570,7 @@ def test_3_new_duplicate_group(self):
assert 'class="field_error"' in res, res

def test_new_plugin_hook(self):
plugin = MockGroupControllerPlugin()
plugins.load(plugin)
plugins.load('test_group_plugin')
offset = url_for(controller='group', action='new')
res = self.app.get(offset, status=200,
extra_environ={'REMOTE_USER': 'testsysadmin'})
Expand All @@ -606,8 +579,9 @@ def test_new_plugin_hook(self):
form['title'] = "huhuhu"
res = form.submit('save', status=302,
extra_environ={'REMOTE_USER': 'testsysadmin'})
assert plugin.calls['create'] == 1, plugin.calls
plugins.unload(plugin)
p = plugins.get_plugin('test_group_plugin')
assert p.calls['create'] == 1, p.calls
plugins.unload('test_group_plugin')

def test_new_bad_param(self):
offset = url_for(controller='group', action='new',
Expand Down

0 comments on commit f871e87

Please sign in to comment.