Fix config validator and add tests
This commit is contained in:
parent
803b228d1c
commit
9d83322591
|
@ -89,7 +89,7 @@ def harvest_source_type_exists(value,context):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def harvest_source_config_validator(key,data,errors,context):
|
def harvest_source_config_validator(key,data,errors,context):
|
||||||
harvester_type = data.get(('type',),'')
|
harvester_type = data.get(('source_type',),'')
|
||||||
for harvester in PluginImplementations(IHarvester):
|
for harvester in PluginImplementations(IHarvester):
|
||||||
info = harvester.info()
|
info = harvester.info()
|
||||||
if info['name'] == harvester_type:
|
if info['name'] == harvester_type:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
import copy
|
import copy
|
||||||
import ckan
|
import ckan
|
||||||
import paste
|
import paste
|
||||||
|
@ -32,7 +33,7 @@ class HarvestSourceActionBase(object):
|
||||||
"notes": "Test source action desc",
|
"notes": "Test source action desc",
|
||||||
"source_type": "test",
|
"source_type": "test",
|
||||||
"frequency": "MANUAL",
|
"frequency": "MANUAL",
|
||||||
"config": "bb"
|
"config": json.dumps({"custom_option":["a","b"]})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,6 +80,25 @@ class HarvestSourceActionBase(object):
|
||||||
assert 'frequency' in result
|
assert 'frequency' in result
|
||||||
assert u'Frequency {0} not recognised'.format(wrong_frequency) in result['frequency'][0]
|
assert u'Frequency {0} not recognised'.format(wrong_frequency) in result['frequency'][0]
|
||||||
|
|
||||||
|
def test_invalid_wrong_configuration(self):
|
||||||
|
|
||||||
|
source_dict = copy.deepcopy(self.default_source_dict)
|
||||||
|
source_dict['config'] = 'not_json'
|
||||||
|
|
||||||
|
result = tests.call_action_api(self.app, self.action,
|
||||||
|
apikey=self.sysadmin['apikey'], status=409, **source_dict)
|
||||||
|
|
||||||
|
assert 'config' in result
|
||||||
|
assert u'Error parsing the configuration options: No JSON object could be decoded' in result['config'][0]
|
||||||
|
|
||||||
|
source_dict['config'] = json.dumps({'custom_option': 'not_a_list'})
|
||||||
|
|
||||||
|
result = tests.call_action_api(self.app, self.action,
|
||||||
|
apikey=self.sysadmin['apikey'], status=409, **source_dict)
|
||||||
|
|
||||||
|
assert 'config' in result
|
||||||
|
assert u'Error parsing the configuration options: custom_option must be a list' in result['config'][0]
|
||||||
|
|
||||||
|
|
||||||
class TestHarvestSourceActionCreate(HarvestSourceActionBase):
|
class TestHarvestSourceActionCreate(HarvestSourceActionBase):
|
||||||
|
|
||||||
|
@ -140,7 +160,7 @@ class TestHarvestSourceActionUpdate(HarvestSourceActionBase):
|
||||||
"notes": "Test source action desc updated",
|
"notes": "Test source action desc updated",
|
||||||
"source_type": "test",
|
"source_type": "test",
|
||||||
"frequency": "MONTHLY",
|
"frequency": "MONTHLY",
|
||||||
"config": "cc"
|
"config": json.dumps({"custom_option":["c","d"]})
|
||||||
})
|
})
|
||||||
|
|
||||||
result = tests.call_action_api(self.app, 'harvest_source_update',
|
result = tests.call_action_api(self.app, 'harvest_source_update',
|
||||||
|
|
|
@ -13,6 +13,22 @@ class TestHarvester(SingletonPlugin):
|
||||||
def info(self):
|
def info(self):
|
||||||
return {'name': 'test', 'title': 'test', 'description': 'test'}
|
return {'name': 'test', 'title': 'test', 'description': 'test'}
|
||||||
|
|
||||||
|
def validate_config(self,config):
|
||||||
|
if not config:
|
||||||
|
return config
|
||||||
|
|
||||||
|
try:
|
||||||
|
config_obj = json.loads(config)
|
||||||
|
|
||||||
|
if 'custom_option' in config_obj:
|
||||||
|
if not isinstance(config_obj['custom_option'],list):
|
||||||
|
raise ValueError('custom_option must be a list')
|
||||||
|
|
||||||
|
except ValueError,e:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
def gather_stage(self, harvest_job):
|
def gather_stage(self, harvest_job):
|
||||||
|
|
||||||
if harvest_job.source.url.startswith('basic_test'):
|
if harvest_job.source.url.startswith('basic_test'):
|
||||||
|
|
Loading…
Reference in New Issue