From a4c87570b9c104ff189d3de5b8dc84250f3a6ab8 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Sun, 1 Nov 2020 21:42:02 +0300 Subject: [PATCH] Improve documentation and tests --- plugins/modules/mysql_query.py | 5 +++++ .../test_mysql_query/tasks/mysql_query_initial.yml | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/modules/mysql_query.py b/plugins/modules/mysql_query.py index cacca2d..5b8f92c 100644 --- a/plugins/modules/mysql_query.py +++ b/plugins/modules/mysql_query.py @@ -20,6 +20,7 @@ options: query: description: - SQL query to run. Multiple queries can be passed using YAML list syntax. + - Must be a string or YAML list. type: raw required: yes positional_args: @@ -146,6 +147,10 @@ def main(): check_hostname = module.params['check_hostname'] config_file = module.params['config_file'] query = module.params["query"] + + if not isinstance(query, str) and not isinstance(query, list): + module.fail_json("the query option value must be string or list, passed %s" % type(query)) + if module.params["single_transaction"]: autocommit = False else: diff --git a/tests/integration/targets/test_mysql_query/tasks/mysql_query_initial.yml b/tests/integration/targets/test_mysql_query/tasks/mysql_query_initial.yml index f9d2843..779c3d4 100644 --- a/tests/integration/targets/test_mysql_query/tasks/mysql_query_initial.yml +++ b/tests/integration/targets/test_mysql_query/tasks/mysql_query_initial.yml @@ -243,7 +243,12 @@ <<: *mysql_params login_db: '{{ test_db }}' query: 'CREATE TABLE {{ test_table3 }} (id int, story text)' - register: result + + - name: Add data to {{ test_table3 }} + mysql_query: + <<: *mysql_params + login_db: '{{ test_db }}' + query: "INSERT INTO {{ test_table3 }} (id, story) VALUES (1, 'first'), (2, 'second')" - name: Select from {{ test_table3 }} mysql_query: @@ -252,6 +257,10 @@ query: 'SELECT id, story FROM {{ test_table3 }}' register: result + - assert: + that: + - result.rowcount == [2] + - name: Drop db {{ test_db }} mysql_query: <<: *mysql_params