diff --git a/ckan/new_tests/__init__.py b/ckan/new_tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ckan/new_tests/data.py b/ckan/new_tests/data.py new file mode 100644 index 00000000000..d5d76a8f800 --- /dev/null +++ b/ckan/new_tests/data.py @@ -0,0 +1,7 @@ +'''A collection of static data for use in tests. + +''' +TYPICAL_USER = {'name': 'fred', + 'email': 'fred@fred.com', + 'password': 'wilma', + } diff --git a/ckan/new_tests/helpers.py b/ckan/new_tests/helpers.py new file mode 100644 index 00000000000..7ad0bd00781 --- /dev/null +++ b/ckan/new_tests/helpers.py @@ -0,0 +1,49 @@ +'''A collection of test helper functions. + +''' +import ckan.model as model +import ckan.logic as logic + + +def reset_db(): + '''Reset CKAN's database. + + Each test module or class should call this function in its setup method. + + ''' + # Close any database connections that have been left open. + # This prevents CKAN from hanging waiting for some unclosed connection. + model.Session.close_all() + + # Clean out any data from the db. This prevents tests failing due to data + # leftover from other tests or from previous test runs. + model.repo.clean_db() + + # Initialize the db. This prevents CKAN from crashing if the tests are run + # and the test db has not been initialized. + model.repo.init_db() + + +def call_action(action_name, context=None, **kwargs): + '''Call the given ckan.logic.action function with the given context + and params. + + For example: + + call_action('user_create', name='seanh', email='seanh@seanh.com', + password='pass') + + This is just a nicer way for user code to call action functions, nicer than + either calling the action function directly or via get_action(). + + If accepted this function should eventually be moved to + ckan.logic.call_action() and the current get_action() function should be + deprecated. The tests may still need their own wrapper function for + logic.call_action(), e.g. to insert 'ignore_auth': True into the context. + + ''' + if context is None: + context = {} + context.setdefault('user', '127.0.0.1') + context.setdefault('ignore_auth', True) + return logic.get_action(action_name)(context=context, data_dict=kwargs) diff --git a/ckan/new_tests/logic/__init__.py b/ckan/new_tests/logic/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ckan/new_tests/logic/action/__init__.py b/ckan/new_tests/logic/action/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ckan/new_tests/logic/action/test_update.py b/ckan/new_tests/logic/action/test_update.py new file mode 100644 index 00000000000..6a7ecfd0d8f --- /dev/null +++ b/ckan/new_tests/logic/action/test_update.py @@ -0,0 +1,79 @@ +'''Unit tests for ckan/logic/action/update.py. + +''' +import ckan.new_tests.helpers as helpers +import ckan.new_tests.data as data + + +def setup_module(): + helpers.reset_db() + +# Tests for user_update: +# +# - Test for success: +# - Typical values +# - Edge cases +# - If multiple params, test with different combinations +# - Test for failure: +# - Common mistakes +# - Bizarre input +# - Unicode +# - Cover the interface +# - Can we somehow mock user_create? +# +# Correct and incorrect ID + + +def test_user_update(): + '''Test that updating a user's metadata fields works successfully. + + ''' + # Prepare + user = helpers.call_action('user_create', **data.TYPICAL_USER) + + # Execute + user['name'] = 'updated_name' + user['about'] = 'updated_about' + helpers.call_action('user_update', **user) + + # Assert + updated_user = helpers.call_action('user_show', id=user['id']) + # Note that we check each updated field seperately, we don't compare the + # entire dicts, only assert what we're actually testing. + assert user['name'] == updated_user['name'] + assert user['about'] == updated_user['about'] + + +def test_user_update_with_invalid_id(): + pass + + +def test_user_update_with_nonexistent_id(): + pass + + +def test_user_update_with_no_id(): + pass + + +def test_user_update_with_custom_schema(): + pass + + +def test_user_update_with_invalid_name(): + pass + + +def test_user_update_with_invalid_password(): + pass + + +def test_user_update_with_deferred_commit(): + pass + + +# TODO: Valid and invalid values for other fields. + + +def test_user_update_activity_stream(): + pass