Skip to content

Commit

Permalink
[#1117] Don't use with nose.tools.assert_raises
Browse files Browse the repository at this point in the history
Third attempt to fix the new tests in Python 2.6. Looks like using
assert_raises as a context manager doesn't work in Python 2.6 at all.
Just use nose.tools.assert_raises(...) normally instead.
  • Loading branch information
Sean Hammond committed Aug 1, 2013
1 parent a0a2f69 commit cbd8d89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions ckan/new_tests/lib/navl/test_validators.py
Expand Up @@ -40,9 +40,9 @@ def test_ignore_missing_with_value_missing(self):
original_data = copy.deepcopy(data)
original_errors = copy.deepcopy(errors)

with nose.tools.assert_raises(df.StopOnError) as context:
validators.ignore_missing(key=key, data=data, errors=errors,
context={})
nose.tools.assert_raises(df.StopOnError,
validators.ignore_missing, key=key,
data=data, errors=errors, context={})

assert key not in data, ('When given a value of {value} '
'ignore_missing() should remove the item '
Expand Down
30 changes: 16 additions & 14 deletions ckan/new_tests/logic/action/test_update.py
Expand Up @@ -79,16 +79,16 @@ def test_user_update_with_id_that_does_not_exist(self):
user_dict = factories.User.attributes()
user_dict['id'] = "there's no user with this id"

with nose.tools.assert_raises(logic.NotFound) as context:
helpers.call_action('user_update', **user_dict)
nose.tools.assert_raises(logic.NotFound, helpers.call_action,
'user_update', **user_dict)
# TODO: Could assert the actual error message, not just the exception?
# (Could also do this with many of the tests below.)

def test_user_update_with_no_id(self):
user_dict = factories.User.attributes()
assert 'id' not in user_dict
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user_dict)
nose.tools.assert_raises(logic.ValidationError, helpers.call_action,
'user_update', **user_dict)

def test_user_update_with_invalid_name(self):
user = factories.User()
Expand All @@ -97,8 +97,9 @@ def test_user_update_with_invalid_name(self):
'a'*200, 'Hi!', )
for name in invalid_names:
user['name'] = name
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
nose.tools.assert_raises(logic.ValidationError,
helpers.call_action, 'user_update',
**user)

def test_user_update_to_name_that_already_exists(self):
fred = factories.User(name='fred')
Expand All @@ -107,8 +108,8 @@ def test_user_update_to_name_that_already_exists(self):
# Try to update fred and change his user name to bob, which is already
# bob's user name
fred['name'] = bob['name']
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **fred)
nose.tools.assert_raises(logic.ValidationError, helpers.call_action,
'user_update', **fred)

def test_user_update_password(self):
'''Test that updating a user's password works successfully.'''
Expand All @@ -132,8 +133,8 @@ def test_user_update_with_short_password(self):
user = factories.User()

user['password'] = 'xxx' # This password is too short.
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
nose.tools.assert_raises(logic.ValidationError, helpers.call_action,
'user_update', **user)

def test_user_update_with_empty_password(self):
'''If an empty password is passed to user_update, nothing should
Expand All @@ -158,16 +159,17 @@ def test_user_update_with_null_password(self):
user = factories.User()

user['password'] = None
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
nose.tools.assert_raises(logic.ValidationError, helpers.call_action,
'user_update', **user)

def test_user_update_with_invalid_password(self):
user = factories.User()

for password in (False, -1, 23, 30.7):
user['password'] = password
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
nose.tools.assert_raises(logic.ValidationError,
helpers.call_action, 'user_update',
**user)

# TODO: Valid and invalid values for the rest of the user model's fields.

Expand Down
3 changes: 1 addition & 2 deletions ckan/new_tests/logic/test_validators.py
Expand Up @@ -78,8 +78,7 @@ def call_validator(*args, **kwargs):
'''
def call_and_assert(*args, **kwargs):
import ckan.lib.navl.dictization_functions as df
with nose.tools.assert_raises(df.Invalid) as context:
return function(*args, **kwargs)
nose.tools.assert_raises(df.Invalid, function, *args, **kwargs)
return call_and_assert


Expand Down

0 comments on commit cbd8d89

Please sign in to comment.