Skip to content

Commit

Permalink
[#3428] start of function create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Feb 23, 2017
1 parent 24e8169 commit 18f0d58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
9 changes: 8 additions & 1 deletion ckan/tests/helpers.py
Expand Up @@ -179,7 +179,8 @@ class FunctionalTestBase(object):
Allows configuration changes by overriding _apply_config_changes and
resetting the CKAN config after your test class has run. It creates a
webtest.TestApp at self.app for your class to use to make HTTP requests
to the CKAN web UI or API.
to the CKAN web UI or API. Also loads plugins defined by
_load_plugins in the class definition.
If you're overriding methods that this class provides, like setup_class()
and teardown_class(), make sure to use super() to call this class's methods
Expand All @@ -196,10 +197,13 @@ def _get_test_app(cls): # leading _ because nose is terrible

@classmethod
def setup_class(cls):
import ckan.plugins as p
# Make a copy of the Pylons config, so we can restore it in teardown.
cls._original_config = dict(config)
cls._apply_config_changes(config)
cls._get_test_app()
for plugin in getattr(cls, '_load_plugins', []):
p.load(plugin)

@classmethod
def _apply_config_changes(cls, cfg):
Expand All @@ -214,6 +218,9 @@ def setup(self):

@classmethod
def teardown_class(cls):
import ckan.plugins as p
for plugin in reversed(getattr(cls, '_load_plugins', [])):
p.unload(plugin)
# Restore the Pylons config to its original values, in case any tests
# changed any config settings.
config.clear()
Expand Down
6 changes: 5 additions & 1 deletion ckanext/datastore/db.py
Expand Up @@ -1432,7 +1432,11 @@ def create_trigger_function(name, definition, or_replace):
name=datastore_helpers.identifier(name),
definition=datastore_helpers.literal_string(definition))

_write_engine_execute(sql)
try:
_write_engine_execute(sql)
except ProgrammingError as pe:
raise ValidationError({
u'definition': [pe.args[0].split('\n')[0].decode('utf8')]})


def drop_function(name, if_exists):
Expand Down
25 changes: 25 additions & 0 deletions ckanext/datastore/tests/test_create.py
Expand Up @@ -19,6 +19,7 @@

import ckanext.datastore.db as db
from ckanext.datastore.tests.helpers import rebuild_all_dbs, set_url_type
from ckan.plugins.toolkit import ValidationError


class TestDatastoreCreateNewTests(object):
Expand Down Expand Up @@ -905,3 +906,27 @@ def test_datastore_create_with_invalid_data_value(self):
assert res_dict['success'] is False
assert res_dict['error']['__type'] == 'Validation Error'
assert res_dict['error']['message'].startswith('The data was invalid')


class TestDatastoreFunctionCreateTests(helpers.FunctionalTestBase):
_load_plugins = [u'datastore']

def test_nop_trigger(self):
helpers.call_action(
u'datastore_function_create',
name=u'test_nop',
or_replace=True,
rettype=u'trigger',
definition=u'BEGIN RETURN NEW; END;')

def test_invalid_definition(self):
try:
helpers.call_action(
u'datastore_function_create',
name=u'test_invalid_def',
rettype=u'trigger',
definition=u'HELLO WORLD')
except ValidationError as ve:
assert_equal(
ve.error_dict['definition'],
[u'(ProgrammingError) syntax error at or near "HELLO"'])

0 comments on commit 18f0d58

Please sign in to comment.