Skip to content

Commit

Permalink
[#1117] Mock ckan.model in user_name_validator() tests
Browse files Browse the repository at this point in the history
Now that user_name_validator() has been refactored to call
model.User.get() instead of doing its own SQLAlchemy (commit c6b953),
it's really easy to mock ckan.model in the user_name_validator() unit
tests by just mocking the single method ckan.model.User.get().

This means the user_name_validator() unit tests no longer touch the disk
or db or bring in ckan.model.
  • Loading branch information
Sean Hammond committed Jul 26, 2013
1 parent c6b9534 commit 93d0d81
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions ckan/new_tests/logic/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'''
import copy

import mock
import nose.tools

import ckan.new_tests.helpers as helpers
Expand Down Expand Up @@ -109,7 +110,6 @@ def test_user_name_validator_with_non_string_value(self):
'''
import ckan.logic.validators as validators
import ckan.model as model
import ckan.lib.navl.dictization_functions as df

non_string_values = [
Expand All @@ -126,13 +126,12 @@ def test_user_name_validator_with_non_string_value(self):
lambda x: x**2,
]

key = ('name',)
context = {
'model': model,
'session': model.Session,
'user': '',
}
# Mock ckan.model.
mock_model = mock.MagicMock()
# model.User.get(some_user_id) needs to return None for this test.
mock_model.User.get.return_value = None

key = ('name',)
for non_string_value in non_string_values:
data = test_data.validator_data_dict()
data[key] = non_string_value
Expand All @@ -144,7 +143,8 @@ def test_user_name_validator_with_non_string_value(self):
original_errors = copy.deepcopy(errors)

with nose.tools.assert_raises(df.Invalid):
validators.user_name_validator(key, data, errors, context)
validators.user_name_validator(key, data, errors,
context={'model': mock_model})

assert data == original_data, ("user_name_validator shouldn't "
'modify the data dict')
Expand All @@ -158,28 +158,25 @@ def test_user_name_validator_with_a_name_that_already_exists(self):
'''
import ckan.logic.validators as validators
import ckan.model as model

existing_user = helpers.call_action('user_create',
**test_data.typical_user())
# Mock ckan.model. model.User.get('user_name') will return another mock
# object rather than None, which will simulate an existing user with
# the same user name in the database.
mock_model = mock.MagicMock()

# Try to create another user with the same name as the existing user.
data = test_data.validator_data_dict()
key = ('name',)
data[key] = existing_user['name']
data[key] = 'user_name'
errors = test_data.validator_errors_dict()
errors[key] = []
context = {
'model': model,
'session': model.Session,
'user': '',
}

# Make copies of the data and errors dicts for asserting later.
original_data = copy.deepcopy(data)
original_errors = copy.deepcopy(errors)

result = validators.user_name_validator(key, data, errors, context)
# Try to create another user with the same name as the existing user.
result = validators.user_name_validator(key, data, errors,
context={'model': mock_model})

assert result is None, ("user_name_validator() shouldn't return "
"anything")
Expand All @@ -201,24 +198,25 @@ def test_user_name_validator_successful(self):
'''user_name_validator() should do nothing if given a valid name.'''

import ckan.logic.validators as validators
import ckan.model as model

data = test_data.validator_data_dict()
key = ('name',)
data[key] = 'new_user_name'
errors = test_data.validator_errors_dict()
errors[key] = []
context = {
'model': model,
'session': model.Session,
'user': '',
}

# Mock ckan.model.
mock_model = mock.MagicMock()
# model.User.get(user_name) should return None, to simulate that no
# user with that name exists in the database.
mock_model.User.get.return_value = None

# Make copies of the data and errors dicts for asserting later.
original_data = copy.deepcopy(data)
original_errors = copy.deepcopy(errors)

result = validators.user_name_validator(key, data, errors, context)
result = validators.user_name_validator(key, data, errors,
context={'model': mock_model})

assert result is None, ("user_name_validator() shouldn't return "
'anything')
Expand All @@ -227,4 +225,8 @@ def test_user_name_validator_successful(self):
'the data dict')

assert errors == original_errors, ("user_name_validator shouldn't "
'modify the errors dict')
'modify the errors dict if given a'
'valid user name')

# TODO: Test user_name_validator()'s behavior when there's a 'user_obj' in
# the context dict.

0 comments on commit 93d0d81

Please sign in to comment.