From 7e84c18750ed58b1335709b8f242d397865ac010 Mon Sep 17 00:00:00 2001 From: joetsoi Date: Tue, 7 Jan 2014 02:37:11 +0000 Subject: [PATCH] [#790] pep8, fix tests by preventing plugins from loading during import --- .../new_tests/test_example_idatasetform.py | 32 +++++++++++-------- ckanext/example_idatasetform/plugin_v1.py | 9 +++--- ckanext/example_idatasetform/plugin_v2.py | 11 ++++--- ckanext/example_idatasetform/plugin_v3.py | 5 +-- ckanext/example_idatasetform/plugin_v4.py | 21 +++++++----- doc/extensions/adding-custom-fields.rst | 2 +- 6 files changed, 46 insertions(+), 34 deletions(-) diff --git a/ckanext/example_idatasetform/new_tests/test_example_idatasetform.py b/ckanext/example_idatasetform/new_tests/test_example_idatasetform.py index 5784264b231..a7aaccf955f 100644 --- a/ckanext/example_idatasetform/new_tests/test_example_idatasetform.py +++ b/ckanext/example_idatasetform/new_tests/test_example_idatasetform.py @@ -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 @@ -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']) @@ -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): @@ -81,7 +84,7 @@ 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') @@ -89,7 +92,7 @@ def test_package_create(self): 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', @@ -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') @@ -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): @@ -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', @@ -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', @@ -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', @@ -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']) diff --git a/ckanext/example_idatasetform/plugin_v1.py b/ckanext/example_idatasetform/plugin_v1.py index f3b7f76321d..183e12ff28e 100644 --- a/ckanext/example_idatasetform/plugin_v1.py +++ b/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) @@ -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 @@ -19,7 +20,7 @@ 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 @@ -27,7 +28,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 diff --git a/ckanext/example_idatasetform/plugin_v2.py b/ckanext/example_idatasetform/plugin_v2.py index c1fa7f71f8a..9741d8be7ff 100644 --- a/ckanext/example_idatasetform/plugin_v2.py +++ b/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) @@ -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 @@ -20,7 +21,7 @@ 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 @@ -28,7 +29,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 @@ -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. diff --git a/ckanext/example_idatasetform/plugin_v3.py b/ckanext/example_idatasetform/plugin_v3.py index a272ca538b5..6430becf7b7 100644 --- a/ckanext/example_idatasetform/plugin_v3.py +++ b/ckanext/example_idatasetform/plugin_v3.py @@ -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 @@ -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 diff --git a/ckanext/example_idatasetform/plugin_v4.py b/ckanext/example_idatasetform/plugin_v4.py index 437defaa9a1..1639459c069 100644 --- a/ckanext/example_idatasetform/plugin_v4.py +++ b/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']} @@ -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) @@ -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')) @@ -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 diff --git a/doc/extensions/adding-custom-fields.rst b/doc/extensions/adding-custom-fields.rst index f8256052bda..58aeec0fd17 100644 --- a/doc/extensions/adding-custom-fields.rst +++ b/doc/extensions/adding-custom-fields.rst @@ -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