Fix up unit tests (no more double mocking)

This commit is contained in:
beeankha
2020-05-04 21:10:21 -04:00
committed by AlanCoding
parent 09c10a6f59
commit b80127dd40
2 changed files with 34 additions and 38 deletions

View File

@@ -33,7 +33,6 @@ def test_version_warning(collection_import, silence_warning):
TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}} cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)] testargs = ['module_file2.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs): with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response): with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response):
my_module = TowerModule(argument_spec=dict()) my_module = TowerModule(argument_spec=dict())
@@ -41,7 +40,7 @@ def test_version_warning(collection_import, silence_warning):
my_module._COLLECTION_TYPE = "not-junk" my_module._COLLECTION_TYPE = "not-junk"
my_module.collection_to_version['not-junk'] = 'not-junk' my_module.collection_to_version['not-junk'] = 'not-junk'
my_module.get_endpoint('ping') my_module.get_endpoint('ping')
mock_warn.assert_called_once_with( silence_warning.assert_called_once_with(
'You are running collection version 1.0.0 but connecting to tower version 1.2.3' 'You are running collection version 1.0.0 but connecting to tower version 1.2.3'
) )
@@ -50,7 +49,6 @@ def test_type_warning(collection_import, silence_warning):
TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule
cli_data = {'ANSIBLE_MODULE_ARGS': {}} cli_data = {'ANSIBLE_MODULE_ARGS': {}}
testargs = ['module_file2.py', json.dumps(cli_data)] testargs = ['module_file2.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs): with mock.patch.object(sys, 'argv', testargs):
with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response): with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response):
my_module = TowerModule(argument_spec={}) my_module = TowerModule(argument_spec={})
@@ -58,12 +56,12 @@ def test_type_warning(collection_import, silence_warning):
my_module._COLLECTION_TYPE = "junk" my_module._COLLECTION_TYPE = "junk"
my_module.collection_to_version['junk'] = 'junk' my_module.collection_to_version['junk'] = 'junk'
my_module.get_endpoint('ping') my_module.get_endpoint('ping')
mock_warn.assert_called_once_with( silence_warning.assert_called_once_with(
'You are using the junk version of this collection but connecting to not-junk' 'You are using the junk version of this collection but connecting to not-junk'
) )
def test_duplicate_config(collection_import): def test_duplicate_config(collection_import, silence_warning):
# imports done here because of PATH issues unique to this test suite # imports done here because of PATH issues unique to this test suite
TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule
data = { data = {
@@ -82,14 +80,13 @@ def test_duplicate_config(collection_import):
cli_data = {'ANSIBLE_MODULE_ARGS': data} cli_data = {'ANSIBLE_MODULE_ARGS': data}
testargs = ['module_file.py', json.dumps(cli_data)] testargs = ['module_file.py', json.dumps(cli_data)]
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
with mock.patch.object(sys, 'argv', testargs): with mock.patch.object(sys, 'argv', testargs):
argument_spec = dict( argument_spec = dict(
name=dict(required=True), name=dict(required=True),
zig=dict(type='str'), zig=dict(type='str'),
) )
DuplicateTestTowerModule(argument_spec=argument_spec) DuplicateTestTowerModule(argument_spec=argument_spec)
mock_warn.assert_called_once_with( silence_warning.assert_called_once_with(
'The parameter(s) tower_username were provided at the same time as ' 'The parameter(s) tower_username were provided at the same time as '
'tower_config_file. Precedence may be unstable, ' 'tower_config_file. Precedence may be unstable, '
'we suggest either using config file or params.' 'we suggest either using config file or params.'

View File

@@ -9,8 +9,7 @@ from awx.main.models import Project
@pytest.mark.django_db @pytest.mark.django_db
def test_create_project(run_module, admin_user, organization): def test_create_project(run_module, admin_user, organization, silence_warning):
with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn:
result = run_module('tower_project', dict( result = run_module('tower_project', dict(
name='foo', name='foo',
organization=organization.name, organization=organization.name,
@@ -19,7 +18,7 @@ def test_create_project(run_module, admin_user, organization):
wait=False, wait=False,
scm_update_cache_timeout=5 scm_update_cache_timeout=5
), admin_user) ), admin_user)
mock_warn.assert_has_calls( silence_warning.assert_has_calls(
[mock.call('scm_update_cache_timeout will be ignored since scm_update_on_launch ' [mock.call('scm_update_cache_timeout will be ignored since scm_update_on_launch '
'was not set to true')]) 'was not set to true')])