Skip to content

Commit

Permalink
Simplify call signature of main test method
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-saeon committed Jul 3, 2018
1 parent 594ac78 commit 44542e8
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 485 deletions.
26 changes: 21 additions & 5 deletions ckanext/metadata/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
import ckan.plugins.toolkit as tk
import ckan.model as ckan_model
import ckanext.metadata.model.setup as ckanext_setup
from ckanext.metadata import model as ckanext_model

_model_map = {
'organization': ckan_model.Group,
'infrastructure': ckan_model.Group,
'metadata_collection': ckan_model.Group,
'metadata_record': ckan_model.Package,
'metadata_model': ckanext_model.MetadataModel,
'metadata_schema': ckanext_model.MetadataSchema,
'workflow_state': ckanext_model.WorkflowState,
'workflow_transition': ckanext_model.WorkflowTransition,
'workflow_metric': ckanext_model.WorkflowMetric,
'workflow_rule': ckanext_model.WorkflowRule,
}


def make_uuid():
Expand Down Expand Up @@ -120,9 +134,10 @@ def setup(self):
self.normal_user = ckan_factories.User()
self.sysadmin_user = ckan_factories.Sysadmin()

def _test_action(self, method, model_name, model_class=None, exception_class=None, sysadmin=False, check_auth=False, **kwargs):
def _test_action(self, action_name, should_error=False, exception_class=tk.ValidationError,
sysadmin=False, check_auth=False, **kwargs):

method_name = model_name + '_' + method
model, method = action_name.rsplit('_', 1)
user = self.sysadmin_user if sysadmin else self.normal_user
context = {
'user': user['name'],
Expand All @@ -131,7 +146,7 @@ def _test_action(self, method, model_name, model_class=None, exception_class=Non

obj = None
try:
result = call_action(method_name, context, **kwargs)
result = call_action(action_name, context, **kwargs)
except exception_class, e:
if exception_class is tk.ValidationError:
result = e.error_dict
Expand All @@ -140,14 +155,15 @@ def _test_action(self, method, model_name, model_class=None, exception_class=Non
except Exception, e:
assert False, "Unexpected exception %s: %s" % (type(e), e)
else:
if exception_class:
if should_error:
assert False, str(exception_class) + " was not raised"
finally:
# close the session to ensure that we're not just getting the obj from
# memory but are reloading it from the DB
ckan_model.Session.close_all()

if not exception_class:
if not should_error:
model_class = _model_map[model]
if method in ('create', 'update', 'show'):
assert 'id' in result
obj = model_class.get(result['id'])
Expand Down
39 changes: 12 additions & 27 deletions ckanext/metadata/tests/test_infrastructure_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# encoding: utf-8

from ckan import model as ckan_model
from ckan.plugins import toolkit as tk
from ckan.tests.helpers import call_action

from ckanext.metadata import model as ckanext_model
Expand All @@ -22,8 +20,7 @@ def test_create_valid(self):
'title': 'Test Infrastructure',
'description': 'This is a test infrastructure',
}
result, obj = self._test_action('create', 'infrastructure',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('infrastructure_create', **input_dict)
assert obj.type == 'infrastructure'
assert obj.is_organization == False
assert_object_matches_dict(obj, input_dict)
Expand All @@ -33,30 +30,25 @@ def test_create_valid_sysadmin_setid(self):
'id': make_uuid(),
'name': 'test-infrastructure',
}
result, obj = self._test_action('create', 'infrastructure',
model_class=ckan_model.Group,
sysadmin=True, check_auth=True, **input_dict)
result, obj = self._test_action('infrastructure_create', sysadmin=True, check_auth=True, **input_dict)
assert obj.type == 'infrastructure'
assert obj.is_organization == False
assert_object_matches_dict(obj, input_dict)

def test_create_invalid_duplicate_name(self):
infrastructure = ckanext_factories.Infrastructure()
result, obj = self._test_action('create', 'infrastructure',
exception_class=tk.ValidationError,
result, obj = self._test_action('infrastructure_create', should_error=True,
name=infrastructure['name'])
assert_error(result, 'name', 'Group name already exists in database')

def test_create_invalid_nonsysadmin_setid(self):
result, obj = self._test_action('create', 'infrastructure',
exception_class=tk.ValidationError, check_auth=True,
result, obj = self._test_action('infrastructure_create', should_error=True, check_auth=True,
id=make_uuid())
assert_error(result, 'id', 'The input field id was not expected.')

def test_create_invalid_sysadmin_duplicate_id(self):
infrastructure = ckanext_factories.Infrastructure()
result, obj = self._test_action('create', 'infrastructure',
exception_class=tk.ValidationError, sysadmin=True, check_auth=True,
result, obj = self._test_action('infrastructure_create', should_error=True, sysadmin=True, check_auth=True,
id=infrastructure['id'])
assert_error(result, 'id', 'Already exists: Group')

Expand All @@ -68,8 +60,7 @@ def test_update_valid(self):
'title': 'Updated Test Infrastructure',
'description': 'Updated test infrastructure',
}
result, obj = self._test_action('update', 'infrastructure',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('infrastructure_update', **input_dict)
assert obj.type == 'infrastructure'
assert obj.is_organization == False
assert_object_matches_dict(obj, input_dict)
Expand All @@ -80,8 +71,7 @@ def test_update_valid_partial(self):
'id': infrastructure['id'],
'title': 'Updated Test Infrastructure',
}
result, obj = self._test_action('update', 'infrastructure',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('infrastructure_update', **input_dict)
assert obj.type == 'infrastructure'
assert obj.is_organization == False
assert obj.title == input_dict['title']
Expand All @@ -95,8 +85,7 @@ def test_update_invalid_duplicate_name(self):
'id': infrastructure1['id'],
'name': infrastructure2['name'],
}
result, obj = self._test_action('update', 'infrastructure',
exception_class=tk.ValidationError, **input_dict)
result, obj = self._test_action('infrastructure_update', should_error=True, **input_dict)
assert_error(result, 'name', 'Group name already exists in database')

def test_update_invalid_hierarchy_not_allowed(self):
Expand All @@ -106,29 +95,25 @@ def test_update_invalid_hierarchy_not_allowed(self):
'id': infrastructure1['id'],
'groups': [{'name': infrastructure2['name']}],
}
result, obj = self._test_action('update', 'infrastructure',
exception_class=tk.ValidationError, **input_dict)
result, obj = self._test_action('infrastructure_update', should_error=True, **input_dict)
assert_error(result, '__junk', 'The input field .*groups.* was not expected.')

def test_delete_valid(self):
infrastructure = ckanext_factories.Infrastructure()
self._test_action('delete', 'infrastructure',
model_class=ckan_model.Group,
self._test_action('infrastructure_delete',
id=infrastructure['id'])

def test_delete_with_dependencies(self):
infrastructure = ckanext_factories.Infrastructure()
metadata_model = ckanext_factories.MetadataModel(infrastructure_id=infrastructure['id'])
metadata_record = ckanext_factories.MetadataRecord(infrastructures=[{'id': infrastructure['id']}])

result, obj = self._test_action('delete', 'infrastructure',
exception_class=tk.ValidationError,
result, obj = self._test_action('infrastructure_delete', should_error=True,
id=infrastructure['id'])
assert_error(result, 'message', 'Infrastructure has dependent metadata records')
assert ckanext_model.MetadataModel.get(metadata_model['id']).state == 'active'

call_action('metadata_record_delete', id=metadata_record['id'])
self._test_action('delete', 'infrastructure',
model_class=ckan_model.Group,
self._test_action('infrastructure_delete',
id=infrastructure['id'])
assert ckanext_model.MetadataModel.get(metadata_model['id']).state == 'deleted'
53 changes: 17 additions & 36 deletions ckanext/metadata/tests/test_metadata_collection_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# encoding: utf-8

from ckan import model as ckan_model
from ckan.plugins import toolkit as tk
from ckan.tests import factories as ckan_factories
from ckan.tests.helpers import call_action

Expand All @@ -25,8 +23,7 @@ def test_create_valid(self):
'description': 'This is a test metadata collection',
'organization_id': organization['id'],
}
result, obj = self._test_action('create', 'metadata_collection',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('metadata_collection_create', **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
assert_group_has_extra(obj.id, 'organization_id', input_dict['organization_id'])
Expand All @@ -39,8 +36,7 @@ def test_create_valid_organization_byname(self):
'name': 'test-metadata-collection',
'organization_id': organization['name'],
}
result, obj = self._test_action('create', 'metadata_collection',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('metadata_collection_create', **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
assert obj.name == input_dict['name']
Expand All @@ -53,9 +49,7 @@ def test_create_valid_sysadmin_setid(self):
'name': 'test-metadata-collection',
'organization_id': organization['id'],
}
result, obj = self._test_action('create', 'metadata_collection',
model_class=ckan_model.Group,
sysadmin=True, check_auth=True, **input_dict)
result, obj = self._test_action('metadata_collection_create', sysadmin=True, check_auth=True, **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
assert_group_has_extra(obj.id, 'organization_id', input_dict['organization_id'])
Expand All @@ -64,35 +58,30 @@ def test_create_valid_sysadmin_setid(self):

def test_create_invalid_duplicate_name(self):
metadata_collection = ckanext_factories.MetadataCollection()
result, obj = self._test_action('create', 'metadata_collection',
exception_class=tk.ValidationError,
result, obj = self._test_action('metadata_collection_create', should_error=True,
name=metadata_collection['name'])
assert_error(result, 'name', 'Group name already exists in database')

def test_create_invalid_nonsysadmin_setid(self):
result, obj = self._test_action('create', 'metadata_collection',
exception_class=tk.ValidationError, check_auth=True,
result, obj = self._test_action('metadata_collection_create', should_error=True, check_auth=True,
id=make_uuid())
assert_error(result, 'id', 'The input field id was not expected.')

def test_create_invalid_sysadmin_duplicate_id(self):
metadata_collection = ckanext_factories.MetadataCollection()
result, obj = self._test_action('create', 'metadata_collection',
exception_class=tk.ValidationError, sysadmin=True, check_auth=True,
result, obj = self._test_action('metadata_collection_create', should_error=True, sysadmin=True, check_auth=True,
id=metadata_collection['id'])
assert_error(result, 'id', 'Already exists: Group')

def test_create_invalid_bad_organization(self):
result, obj = self._test_action('create', 'metadata_collection',
exception_class=tk.ValidationError,
result, obj = self._test_action('metadata_collection_create', should_error=True,
organization_id='foo')
assert_error(result, 'organization_id', 'Not found: Organization')

def test_create_invalid_deleted_organization(self):
organization = ckan_factories.Organization()
call_action('organization_delete', id=organization['id'])
result, obj = self._test_action('create', 'metadata_collection',
exception_class=tk.ValidationError,
result, obj = self._test_action('metadata_collection_create', should_error=True,
organization_id=organization['id'])
assert_error(result, 'organization_id', 'Not found: Organization')

Expand All @@ -104,8 +93,7 @@ def test_update_valid(self):
'title': 'Updated Test Metadata Collection',
'description': 'Updated test metadata collection',
}
result, obj = self._test_action('update', 'metadata_collection',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('metadata_collection_update', **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
assert_object_matches_dict(obj, input_dict)
Expand All @@ -117,8 +105,7 @@ def test_update_valid_partial(self):
'id': metadata_collection['id'],
'title': 'Updated Test Metadata Collection',
}
result, obj = self._test_action('update', 'metadata_collection',
model_class=ckan_model.Group, **input_dict)
result, obj = self._test_action('metadata_collection_update', **input_dict)
assert obj.type == 'metadata_collection'
assert obj.is_organization == False
assert obj.title == input_dict['title']
Expand All @@ -133,8 +120,7 @@ def test_update_invalid_duplicate_name(self):
'id': metadata_collection1['id'],
'name': metadata_collection2['name'],
}
result, obj = self._test_action('update', 'metadata_collection',
exception_class=tk.ValidationError, **input_dict)
result, obj = self._test_action('metadata_collection_update', should_error=True, **input_dict)
assert_error(result, 'name', 'Group name already exists in database')

def test_update_invalid_hierarchy_not_allowed(self):
Expand All @@ -144,8 +130,7 @@ def test_update_invalid_hierarchy_not_allowed(self):
'id': metadata_collection1['id'],
'groups': [{'name': metadata_collection2['name']}],
}
result, obj = self._test_action('update', 'metadata_collection',
exception_class=tk.ValidationError, **input_dict)
result, obj = self._test_action('metadata_collection_update', should_error=True, **input_dict)
assert_error(result, '__junk', 'The input field .*groups.* was not expected.')

def test_update_invalid_cannot_change_organization(self):
Expand All @@ -155,14 +140,12 @@ def test_update_invalid_cannot_change_organization(self):
'id': metadata_collection['id'],
'organization_id': organization['id'],
}
result, obj = self._test_action('update', 'metadata_collection',
exception_class=tk.ValidationError, **input_dict)
result, obj = self._test_action('metadata_collection_update', should_error=True, **input_dict)
assert_error(result, 'organization_id', 'The input field organization_id was not expected.')

def test_delete_valid(self):
metadata_collection = ckanext_factories.MetadataCollection()
self._test_action('delete', 'metadata_collection',
model_class=ckan_model.Group,
self._test_action('metadata_collection_delete',
id=metadata_collection['id'])

def test_delete_with_dependencies(self):
Expand All @@ -171,12 +154,10 @@ def test_delete_with_dependencies(self):
owner_org=metadata_collection['organization_id'],
metadata_collection_id=metadata_collection['id'])

result, obj = self._test_action('delete', 'metadata_collection',
exception_class=tk.ValidationError,
result, obj = self._test_action('metadata_collection_delete', should_error=True,
id=metadata_collection['id'])
assert_error(result, 'message', 'Metadata collection has dependent metadata records')

call_action('metadata_record_delete', id=metadata_record['id'])
self._test_action('delete', 'metadata_collection',
model_class=ckan_model.Group,
id=metadata_collection['id'])
self._test_action('metadata_collection_delete',
id=metadata_collection['id'])
Loading

0 comments on commit 44542e8

Please sign in to comment.