Skip to content

Commit

Permalink
Merge pull request #6 from open-data/test-plugin-subclass
Browse files Browse the repository at this point in the history
Allow subclassing plugins to override methods
  • Loading branch information
wardi committed Nov 28, 2014
2 parents 73bc09f + f518f90 commit bf1d150
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 53 deletions.
10 changes: 9 additions & 1 deletion bin/travis-script
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#!/bin/sh

set -e
cd ckanext/scheming/tests
nosetests --with-pylons=test.ini --nologcapture --with-coverage --cover-package=ckanext.scheming ckanext.scheming.tests

nosetests --with-pylons=test_subclass.ini --nologcapture \
ckanext.scheming.tests.test_dataset_display \
ckanext.scheming.tests.test_form \
ckanext.scheming.tests.test_dataset_logic
nosetests --with-pylons=test.ini --nologcapture \
--with-coverage --cover-package=ckanext.scheming \
ckanext.scheming.tests
18 changes: 14 additions & 4 deletions ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class _SchemingMixin(object):
_helpers_loaded = False
_template_dir_added = False

@classmethod
def _store_instance(cls, self):
cls.instance = self

def get_helpers(self):
if _SchemingMixin._helpers_loaded:
return {}
Expand Down Expand Up @@ -68,6 +64,8 @@ def update_config(self, config):
if self.instance:
# reloading plugins, probably in WebTest
_SchemingMixin._helpers_loaded = False
# record our plugin instance in a place where our helpers
# can find it:
self._store_instance(self)
self._add_template_directory(config)

Expand Down Expand Up @@ -137,6 +135,10 @@ class SchemingDatasetsPlugin(p.SingletonPlugin, DefaultDatasetForm,
FALLBACK_OPTION = 'scheming.dataset_fallback'
SCHEMA_TYPE_FIELD = 'dataset_type'

@classmethod
def _store_instance(cls, self):
SchemingDatasetsPlugin.instance = self

def read_template(self):
return 'scheming/package/read.html'

Expand Down Expand Up @@ -217,6 +219,10 @@ class SchemingGroupsPlugin(p.SingletonPlugin, _GroupOrganizationMixin,
FALLBACK_OPTION = 'scheming.group_fallback'
SCHEMA_TYPE_FIELD = 'group_type'

@classmethod
def _store_instance(cls, self):
SchemingGroupsPlugin.instance = self

def about_template(self):
return 'scheming/group/about.html'

Expand All @@ -243,6 +249,10 @@ class SchemingOrganizationsPlugin(p.SingletonPlugin, _GroupOrganizationMixin,
SCHEMA_TYPE_FIELD = 'organization_type'
UNSPECIFIED_GROUP_TYPE = 'organization'

@classmethod
def _store_instance(cls, self):
SchemingOrganizationsPlugin.instance = self

def about_template(self):
return 'scheming/organization/about.html'

Expand Down
6 changes: 6 additions & 0 deletions ckanext/scheming/tests/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ckan.plugins as p

from ckanext.scheming.plugins import SchemingDatasetsPlugin

class SchemingTestSubclass(SchemingDatasetsPlugin):
pass
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nose.tools import assert_true

from ckan.new_tests.factories import Sysadmin, Dataset, Organization, Group
from ckan.new_tests.factories import Sysadmin, Dataset
from ckan.new_tests.helpers import FunctionalTestBase, submit_and_follow


Expand Down Expand Up @@ -35,32 +35,3 @@ def test_resource_displays_custom_fields(self):
response = app.get(url='/dataset/set-two/resource/' +
d['resources'][0]['id'])
assert_true('Camels in Photo' in response.body)


class TestOrganizationDisplay(FunctionalTestBase):
def test_organization_displays_custom_fields(self):
user = Sysadmin()
Organization(
user=user,
name='org-one',
department_id='3008',
)

app = self._get_test_app()
response = app.get(url='/organization/about/org-one')
assert_true('Department ID' in response.body)


class TestGroupDisplay(FunctionalTestBase):
def test_group_displays_custom_fields(self):
user = Sysadmin()
Group(
user=user,
name='group-one',
bookface='theoneandonly',
)

app = self._get_test_app()
response = app.get(url='/group/about/group-one')
assert_true('Bookface' in response.body)

20 changes: 20 additions & 0 deletions ckanext/scheming/tests/test_dataset_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from nose.tools import assert_raises
from ckanapi import LocalCKAN, NotFound

class TestDatasetSchemaLists(object):
def test_dataset_schema_list(self):
lc = LocalCKAN('visitor')
dataset_schemas = lc.action.scheming_dataset_schema_list()
assert 'camel-photos' in dataset_schemas

def test_dataset_schema_show(self):
lc = LocalCKAN('visitor')
schema = lc.action.scheming_dataset_schema_show(type='camel-photos')
assert schema['dataset_fields'][2]['label'] == 'Humps'

def test_dataset_schema_not_found(self):
lc = LocalCKAN('visitor')
assert_raises(NotFound,
lc.action.scheming_dataset_schema_show,
type='ernie')

33 changes: 33 additions & 0 deletions ckanext/scheming/tests/test_group_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from nose.tools import assert_true

from ckan.new_tests.factories import Sysadmin, Organization, Group
from ckan.new_tests.helpers import FunctionalTestBase


class TestOrganizationDisplay(FunctionalTestBase):
def test_organization_displays_custom_fields(self):
user = Sysadmin()
Organization(
user=user,
name='org-one',
department_id='3008',
)

app = self._get_test_app()
response = app.get(url='/organization/about/org-one')
assert_true('Department ID' in response.body)


class TestGroupDisplay(FunctionalTestBase):
def test_group_displays_custom_fields(self):
user = Sysadmin()
Group(
user=user,
name='group-one',
bookface='theoneandonly',
)

app = self._get_test_app()
response = app.get(url='/group/about/group-one')
assert_true('Bookface' in response.body)

Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
from nose.tools import assert_raises, assert_equals
from ckanapi import LocalCKAN, NotFound

class TestSchemaLists(object):
def test_dataset_schema_list(self):
lc = LocalCKAN('visitor')
dataset_schemas = lc.action.scheming_dataset_schema_list()
assert 'camel-photos' in dataset_schemas

def test_dataset_schema_show(self):
lc = LocalCKAN('visitor')
schema = lc.action.scheming_dataset_schema_show(type='camel-photos')
assert schema['dataset_fields'][2]['label'] == 'Humps'

def test_dataset_schema_not_found(self):
lc = LocalCKAN('visitor')
assert_raises(NotFound,
lc.action.scheming_dataset_schema_show,
type='ernie')

class TestGroupSchemaLists(object):
def test_group_schema_list(self):
lc = LocalCKAN('visitor')
group_schemas = lc.action.scheming_group_schema_list()
Expand Down
55 changes: 55 additions & 0 deletions ckanext/scheming/tests/test_subclass.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[DEFAULT]
debug = true
smtp_server = localhost
error_email_from = paste@localhost

[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 5000

[app:main]
use = config:../../../links/ckan/test-core.ini

ckan.plugins = scheming_test_subclass
scheming.dataset_schemas = ckanext.scheming:camel_photos.json

ckan.site_logo = /img/logo_64px_wide.png
ckan.favicon = /images/icons/ckan.ico
ckan.gravatar_default = identicon

ckan.legacy_templates = no


# Logging configuration
[loggers]
keys = root, ckan, sqlalchemy

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console

[logger_ckan]
qualname = ckan
handlers =
level = INFO

[logger_sqlalchemy]
handlers =
qualname = sqlalchemy.engine
level = WARN

[handler_console]
class = StreamHandler
args = (sys.stdout,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
url='',
license='MIT',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
namespace_packages=['ckanext', 'ckanext.scheming'],
namespace_packages=['ckanext'],
include_package_data=True,
zip_safe=False,
install_requires=[
Expand All @@ -28,6 +28,7 @@
scheming_datasets=ckanext.scheming.plugins:SchemingDatasetsPlugin
scheming_groups=ckanext.scheming.plugins:SchemingGroupsPlugin
scheming_organizations=ckanext.scheming.plugins:SchemingOrganizationsPlugin
scheming_test_subclass=ckanext.scheming.tests.plugins:SchemingTestSubclass
[paste.paster_command]
scheming=ckanext.scheming.commands:SchemingCommand
Expand Down

0 comments on commit bf1d150

Please sign in to comment.