1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/plugins/modules/database/saphana/test_hana_query.py
patchback[bot] 5b68665571
Add module hana_query to make SAP HANA administration easier. (#2623) (#2651)
* new

* move link

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* add more interesting return value in test

* remove unused objects

* removed unneeded function

* extend test output

* Update tests/unit/plugins/modules/database/saphana/test_hana_query.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Rainer Leber <rainer.leber@sva.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit b79969da68)

Co-authored-by: rainerleber <39616583+rainerleber@users.noreply.github.com>
2021-05-27 19:07:04 +02:00

66 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Rainer Leber (@rainerleber) <rainerleber@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible_collections.community.general.plugins.modules import hana_query
from ansible_collections.community.general.tests.unit.plugins.modules.utils import (
AnsibleExitJson,
AnsibleFailJson,
ModuleTestCase,
set_module_args,
)
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible.module_utils import basic
def get_bin_path(*args, **kwargs):
"""Function to return path of hdbsql"""
return "/usr/sap/HDB/HDB01/exe/hdbsql"
class Testhana_query(ModuleTestCase):
"""Main class for testing hana_query module."""
def setUp(self):
"""Setup."""
super(Testhana_query, self).setUp()
self.module = hana_query
self.mock_get_bin_path = patch.object(basic.AnsibleModule, 'get_bin_path', get_bin_path)
self.mock_get_bin_path.start()
self.addCleanup(self.mock_get_bin_path.stop) # ensure that the patching is 'undone'
def tearDown(self):
"""Teardown."""
super(Testhana_query, self).tearDown()
def test_without_required_parameters(self):
"""Failure must occurs when all parameters are missing."""
with self.assertRaises(AnsibleFailJson):
set_module_args({})
self.module.main()
def test_hana_query(self):
"""Check that result is processed."""
set_module_args({
'sid': "HDB",
'instance': "01",
'encrypted': False,
'host': "localhost",
'user': "SYSTEM",
'password': "1234Qwer",
'database': "HDB",
'query': "SELECT * FROM users;"
})
with patch.object(basic.AnsibleModule, 'run_command') as run_command:
run_command.return_value = 0, 'username,name\n testuser,test user \n myuser, my user \n', ''
with self.assertRaises(AnsibleExitJson) as result:
hana_query.main()
self.assertEqual(result.exception.args[0]['query_result'], [[
{'username': 'testuser', 'name': 'test user'},
{'username': 'myuser', 'name': 'my user'},
]])
self.assertEqual(run_command.call_count, 1)