Skip to content

Commit

Permalink
[#1117] Add new_tests dir with first new logic tests
Browse files Browse the repository at this point in the history
Eventually (before this branch is merged into master) new_tests should
become tests and the current tests should become legacy_tests.
  • Loading branch information
Sean Hammond committed Jul 17, 2013
1 parent f91e7e6 commit 73308db
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
Empty file added ckan/new_tests/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions 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',
}
49 changes: 49 additions & 0 deletions 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)
Empty file.
Empty file.
79 changes: 79 additions & 0 deletions 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

0 comments on commit 73308db

Please sign in to comment.