diff --git a/ckanext/harvest/logic/action/update.py b/ckanext/harvest/logic/action/update.py index 4649112..f8dbaa8 100644 --- a/ckanext/harvest/logic/action/update.py +++ b/ckanext/harvest/logic/action/update.py @@ -246,34 +246,6 @@ def harvest_source_index_clear(context, data_dict): return {'id': harvest_source_id} -def harvest_source_index_clear(context, data_dict): - - check_access('harvest_source_clear', context, data_dict) - harvest_source_id = data_dict.get('id') - - source = HarvestSource.get(harvest_source_id) - if not source: - log.error('Harvest source %s does not exist', harvest_source_id) - raise NotFound('Harvest source %s does not exist' % harvest_source_id) - - harvest_source_id = source.id - - conn = make_connection() - query = ''' +%s:"%s" +site_id:"%s" ''' % ( - 'harvest_source_id', harvest_source_id, config.get('ckan.site_id')) - try: - conn.delete_query(query) - if asbool(config.get('ckan.search.solr_commit', 'true')): - conn.commit() - except Exception, e: - log.exception(e) - raise SearchIndexError(e) - finally: - conn.close() - - return {'id': harvest_source_id} - - def harvest_objects_import(context, data_dict): ''' Reimports the existing harvest objects, specified by either source_id, diff --git a/ckanext/harvest/tests/test_action.py b/ckanext/harvest/tests/test_action.py index 7aed649..674f714 100644 --- a/ckanext/harvest/tests/test_action.py +++ b/ckanext/harvest/tests/test_action.py @@ -2,6 +2,7 @@ import json import copy import factories import unittest +from nose.tools import assert_equal try: from ckan.tests import factories as ckan_factories @@ -127,6 +128,30 @@ class FunctionalTestBaseWithoutClearBetweenTests(object): pass +SOURCE_DICT = { + "url": "http://test.action.com", + "name": "test-source-action", + "title": "Test source action", + "notes": "Test source action desc", + "source_type": "test-for-action", + "frequency": "MANUAL", + "config": json.dumps({"custom_option": ["a", "b"]}) +} + + +class ActionBase(object): + @classmethod + def setup_class(cls): + reset_db() + harvest_model.setup() + if not p.plugin_loaded('test_action_harvester'): + p.load('test_action_harvester') + + @classmethod + def teardown_class(cls): + p.unload('test_action_harvester') + + class HarvestSourceActionBase(FunctionalTestBaseWithoutClearBetweenTests): @classmethod @@ -136,15 +161,7 @@ class HarvestSourceActionBase(FunctionalTestBaseWithoutClearBetweenTests): cls.sysadmin = ckan_factories.Sysadmin() - cls.default_source_dict = { - "url": "http://test.action.com", - "name": "test-source-action", - "title": "Test source action", - "notes": "Test source action desc", - "source_type": "test-for-action", - "frequency": "MANUAL", - "config": json.dumps({"custom_option": ["a", "b"]}) - } + cls.default_source_dict = SOURCE_DICT if not p.plugin_loaded('test_action_harvester'): p.load('test_action_harvester') @@ -287,6 +304,27 @@ class TestHarvestSourceActionUpdate(HarvestSourceActionBase): assert source.type == source_dict['source_type'] +class TestActions(ActionBase): + def test_harvest_source_clear(self): + source = factories.HarvestSourceObj(**SOURCE_DICT) + job = factories.HarvestJobObj(source=source) + dataset = ckan_factories.Dataset() + object_ = factories.HarvestObjectObj(job=job, source=source, + package_id=dataset['id']) + + context = {'model': model, 'session': model.Session, + 'ignore_auth': True, 'user': ''} + result = toolkit.get_action('harvest_source_clear')( + context, {'id': source.id}) + + assert_equal(result, {'id': source.id}) + source = harvest_model.HarvestSource.get(source.id) + assert source + assert_equal(harvest_model.HarvestJob.get(job.id), None) + assert_equal(harvest_model.HarvestObject.get(object_.id), None) + assert_equal(model.Package.get(dataset['id']), None) + + class TestHarvestObject(unittest.TestCase): @classmethod def setup_class(cls):