Skip to content

Commit

Permalink
[#790] pep8, fix tests by preventing plugins from loading during import
Browse files Browse the repository at this point in the history
  • Loading branch information
joetsoi committed Jan 7, 2014
1 parent befa723 commit 7e84c18
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
32 changes: 18 additions & 14 deletions ckanext/example_idatasetform/new_tests/test_example_idatasetform.py
Expand Up @@ -3,8 +3,10 @@
import ckan.plugins as plugins
import ckan.new_tests.helpers as helpers

import ckanext.example_idatasetform.plugin_v4 as v4
import ckanext.example_idatasetform.plugin as v5
import ckanext.example_idatasetform as idf
#do not import plugins classes (e.g example_idatasetform.plugin_v4)
#it causes the plugins to get registered and loaded before the tests are run


class ExampleIDatasetFormPluginBase(object):
'''Version 1, 2 and 3 of the plugin are basically the same, so this class
Expand All @@ -23,14 +25,14 @@ def test_package_create(self):

def test_package_update(self):
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text')
custom_text='this is my custom text')
result = helpers.call_action('package_update', name='test_package',
custom_text='this is my updated text')
nt.assert_equals('this is my updated text', result['custom_text'])

def test_package_show(self):
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text')
custom_text='this is my custom text')
result = helpers.call_action('package_show', name_or_id='test_package')
nt.assert_equals('this is my custom text', result['custom_text'])

Expand Down Expand Up @@ -67,6 +69,7 @@ def teardown_class(cls):
super(TestVersion3, cls).teardown_class()
plugins.unload('example_idatasetform_v3')


class TestIDatasetFormPluginVersion4(object):
@classmethod
def setup_class(cls):
Expand All @@ -81,15 +84,15 @@ def teardown_class(cls):
helpers.reset_db()

def test_package_create(self):
v4.create_country_codes()
idf.plugin_v4.create_country_codes()
result = helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text',
country_code='uk')
nt.assert_equals('this is my custom text', result['custom_text'])
nt.assert_equals([u'uk'], result['country_code'])

def test_package_create_wrong_country_code(self):
v4.create_country_codes()
idf.plugin_v4.create_country_codes()
nt.assert_raises(plugins.toolkit.ValidationError,
helpers.call_action,
'package_create',
Expand All @@ -98,7 +101,7 @@ def test_package_create_wrong_country_code(self):
country_code='notcode')

def test_package_update(self):
v4.create_country_codes()
idf.plugin_v4.create_country_codes()
helpers.call_action('package_create', name='test_package',
custom_text='this is my custom text',
country_code='uk')
Expand All @@ -108,6 +111,7 @@ def test_package_update(self):
nt.assert_equals('this is my updated text', result['custom_text'])
nt.assert_equals([u'ie'], result['country_code'])


class TestIDatasetFormPlugin(object):
@classmethod
def setup_class(cls):
Expand All @@ -122,7 +126,7 @@ def teardown_class(cls):
helpers.reset_db()

def test_package_create(self):
v5.create_country_codes()
idf.plugin.create_country_codes()
result = helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
Expand All @@ -131,10 +135,10 @@ def test_package_create(self):
'custom_resource_text': 'my custom resource',
}])
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
result['resources'][0]['custom_resource_text'])

def test_package_update(self):
v5.create_country_codes()
idf.plugin.create_country_codes()
helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
Expand All @@ -155,10 +159,10 @@ def test_package_update(self):
nt.assert_equals('this is my updated text', result['custom_text'])
nt.assert_equals([u'ie'], result['country_code'])
nt.assert_equals('updated custom resource',
result['resources'][0]['custom_resource_text'])
result['resources'][0]['custom_resource_text'])

def test_package_show(self):
v5.create_country_codes()
idf.plugin.create_country_codes()
helpers.call_action(
'package_create', name='test_package',
custom_text='this is my custom text', country_code='uk',
Expand All @@ -169,6 +173,6 @@ def test_package_show(self):
)
result = helpers.call_action('package_show', name_or_id='test_package')
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
result['resources'][0]['custom_resource_text'])
nt.assert_equals('my custom resource',
result['resources'][0]['custom_resource_text'])
result['resources'][0]['custom_resource_text'])
9 changes: 5 additions & 4 deletions ckanext/example_idatasetform/plugin_v1.py
@@ -1,6 +1,7 @@
import ckan.plugins as p
import ckan.plugins.toolkit as tk


class ExampleIDatasetFormPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
p.implements(p.IDatasetForm)

Expand All @@ -9,8 +10,8 @@ def create_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).create_package_schema()
#our custom field
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
})
return schema

Expand All @@ -19,15 +20,15 @@ def update_package_schema(self):
#our custom field
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
tk.get_converter('convert_to_extras')]
})
return schema

def show_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
schema.update({
'custom_text': [tk.get_converter('convert_from_extras'),
tk.get_validator('ignore_missing')]
tk.get_validator('ignore_missing')]
})
return schema

Expand Down
11 changes: 6 additions & 5 deletions ckanext/example_idatasetform/plugin_v2.py
@@ -1,6 +1,7 @@
import ckan.plugins as p
import ckan.plugins.toolkit as tk


class ExampleIDatasetFormPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
p.implements(p.IDatasetForm)
p.implements(p.IConfigurer)
Expand All @@ -10,8 +11,8 @@ def create_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).create_package_schema()
#our custom field
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
})
return schema

Expand All @@ -20,15 +21,15 @@ def update_package_schema(self):
#our custom field
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
tk.get_converter('convert_to_extras')]
})
return schema

def show_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
schema.update({
'custom_text': [tk.get_converter('convert_from_extras'),
tk.get_validator('ignore_missing')]
tk.get_validator('ignore_missing')]
})
return schema

Expand All @@ -42,7 +43,7 @@ def package_types(self):
# This plugin doesn't handle any special package types, it just
# registers itself as the default (above).
return []

def update_config(self, config):
# Add this plugin's templates dir to CKAN's extra_template_paths, so
# that CKAN will use this plugin's custom templates.
Expand Down
5 changes: 3 additions & 2 deletions ckanext/example_idatasetform/plugin_v3.py
Expand Up @@ -2,13 +2,14 @@
import ckan.plugins as p
import ckan.plugins.toolkit as tk


class ExampleIDatasetFormPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
p.implements(p.IDatasetForm)

def _modify_package_schema(self, schema):
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
tk.get_converter('convert_to_extras')]
})
return schema

Expand All @@ -26,7 +27,7 @@ def show_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
schema.update({
'custom_text': [tk.get_converter('convert_from_extras'),
tk.get_validator('ignore_missing')]
tk.get_validator('ignore_missing')]
})
return schema

Expand Down
21 changes: 13 additions & 8 deletions ckanext/example_idatasetform/plugin_v4.py
@@ -1,6 +1,7 @@
import ckan.plugins as p
import ckan.plugins.toolkit as tk


def create_country_codes():
user = tk.get_action('get_site_user')({'ignore_auth': True}, {})
context = {'user': user['name']}
Expand All @@ -14,15 +15,17 @@ def create_country_codes():
data = {'name': tag, 'vocabulary_id': vocab['id']}
tk.get_action('tag_create')(context, data)


def country_codes():
create_country_codes()
try:
country_codes = tk.get_action('tag_list')(
data_dict={'vocabulary_id': 'country_codes'})
tag_list = tk.get_action('tag_list')
country_codes = tag_list(data_dict={'vocabulary_id': 'country_codes'})
return country_codes
except tk.ObjectNotFound:
return None


class ExampleIDatasetFormPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
p.implements(p.IDatasetForm)
p.implements(p.IConfigurer)
Expand All @@ -34,19 +37,21 @@ def get_helpers(self):
def _modify_package_schema(self, schema):
schema.update({
'custom_text': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_extras')]
tk.get_converter('convert_to_extras')]
})
schema.update({
'country_code': [tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_tags')('country_codes')]
})
'country_code': [
tk.get_validator('ignore_missing'),
tk.get_converter('convert_to_tags')('country_codes')
]
})
return schema

def show_package_schema(self):
schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
schema.update({
'custom_text': [tk.get_converter('convert_from_extras'),
tk.get_validator('ignore_missing')]
tk.get_validator('ignore_missing')]
})

schema['tags']['__extras'].append(tk.get_converter('free_tags_only'))
Expand Down Expand Up @@ -77,7 +82,7 @@ def package_types(self):
# This plugin doesn't handle any special package types, it just
# registers itself as the default (above).
return []

#update config
def update_config(self, config):
# Add this plugin's templates dir to CKAN's extra_template_paths, so
Expand Down
2 changes: 1 addition & 1 deletion doc/extensions/adding-custom-fields.rst
Expand Up @@ -219,7 +219,7 @@ Update ``_modify_package_schema`` and
.. literalinclude:: ../../ckanext/example_idatasetform/plugin_v4.py
:start-after: return {'country_codes': country_codes}
:end-before: def create_package_schema(self):
:emphasize-lines: 8,19-24
:emphasize-lines: 9,21-26

We are adding our tag to our plugin's schema. A converter is required to
convert the field in to our tag in a similar way to how we converted our field
Expand Down

0 comments on commit 7e84c18

Please sign in to comment.