From b80127dd406e478a1f0c4b28d5e880c9ccaaec1e Mon Sep 17 00:00:00 2001 From: beeankha Date: Mon, 4 May 2020 21:10:21 -0400 Subject: [PATCH] Fix up unit tests (no more double mocking) --- awx_collection/test/awx/test_module_utils.py | 51 +++++++++----------- awx_collection/test/awx/test_project.py | 21 ++++---- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/awx_collection/test/awx/test_module_utils.py b/awx_collection/test/awx/test_module_utils.py index 47cec72945..fca370ff81 100644 --- a/awx_collection/test/awx/test_module_utils.py +++ b/awx_collection/test/awx/test_module_utils.py @@ -33,15 +33,14 @@ def test_version_warning(collection_import, silence_warning): TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule cli_data = {'ANSIBLE_MODULE_ARGS': {}} 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('ansible.module_utils.urls.Request.open', new=mock_ping_response): - my_module = TowerModule(argument_spec=dict()) - my_module._COLLECTION_VERSION = "1.0.0" - my_module._COLLECTION_TYPE = "not-junk" - my_module.collection_to_version['not-junk'] = 'not-junk' - my_module.get_endpoint('ping') - mock_warn.assert_called_once_with( + with mock.patch.object(sys, 'argv', testargs): + with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response): + my_module = TowerModule(argument_spec=dict()) + my_module._COLLECTION_VERSION = "1.0.0" + my_module._COLLECTION_TYPE = "not-junk" + my_module.collection_to_version['not-junk'] = 'not-junk' + my_module.get_endpoint('ping') + silence_warning.assert_called_once_with( 'You are running collection version 1.0.0 but connecting to tower version 1.2.3' ) @@ -50,20 +49,19 @@ def test_type_warning(collection_import, silence_warning): TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule cli_data = {'ANSIBLE_MODULE_ARGS': {}} 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('ansible.module_utils.urls.Request.open', new=mock_ping_response): - my_module = TowerModule(argument_spec={}) - my_module._COLLECTION_VERSION = "1.2.3" - my_module._COLLECTION_TYPE = "junk" - my_module.collection_to_version['junk'] = 'junk' - my_module.get_endpoint('ping') - mock_warn.assert_called_once_with( + with mock.patch.object(sys, 'argv', testargs): + with mock.patch('ansible.module_utils.urls.Request.open', new=mock_ping_response): + my_module = TowerModule(argument_spec={}) + my_module._COLLECTION_VERSION = "1.2.3" + my_module._COLLECTION_TYPE = "junk" + my_module.collection_to_version['junk'] = 'junk' + my_module.get_endpoint('ping') + silence_warning.assert_called_once_with( '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 TowerModule = collection_import('plugins.module_utils.tower_api').TowerModule data = { @@ -82,14 +80,13 @@ def test_duplicate_config(collection_import): cli_data = {'ANSIBLE_MODULE_ARGS': 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): - argument_spec = dict( - name=dict(required=True), - zig=dict(type='str'), - ) - DuplicateTestTowerModule(argument_spec=argument_spec) - mock_warn.assert_called_once_with( + with mock.patch.object(sys, 'argv', testargs): + argument_spec = dict( + name=dict(required=True), + zig=dict(type='str'), + ) + DuplicateTestTowerModule(argument_spec=argument_spec) + silence_warning.assert_called_once_with( 'The parameter(s) tower_username were provided at the same time as ' 'tower_config_file. Precedence may be unstable, ' 'we suggest either using config file or params.' diff --git a/awx_collection/test/awx/test_project.py b/awx_collection/test/awx/test_project.py index 8bce0d18c5..c5f0ab718d 100644 --- a/awx_collection/test/awx/test_project.py +++ b/awx_collection/test/awx/test_project.py @@ -9,17 +9,16 @@ from awx.main.models import Project @pytest.mark.django_db -def test_create_project(run_module, admin_user, organization): - with mock.patch('ansible.module_utils.basic.AnsibleModule.warn') as mock_warn: - result = run_module('tower_project', dict( - name='foo', - organization=organization.name, - scm_type='git', - scm_url='https://foo.invalid', - wait=False, - scm_update_cache_timeout=5 - ), admin_user) - mock_warn.assert_has_calls( +def test_create_project(run_module, admin_user, organization, silence_warning): + result = run_module('tower_project', dict( + name='foo', + organization=organization.name, + scm_type='git', + scm_url='https://foo.invalid', + wait=False, + scm_update_cache_timeout=5 + ), admin_user) + silence_warning.assert_has_calls( [mock.call('scm_update_cache_timeout will be ignored since scm_update_on_launch ' 'was not set to true')])