From 543a995e7945441ab6ba2b08a71c5eb0e39084e0 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Thu, 4 Jul 2024 09:48:18 +0200 Subject: [PATCH] Fix unit tests, add a changelog fragment, make formatting consistent (#66) --- changelogs/fragments/1-client.yml | 2 ++ plugins/doc_fragments/client_inst_opts.py | 4 +++- plugins/modules/clickhouse_client.py | 4 +++- .../targets/clickhouse_client/tasks/initial.yml | 13 +++++++------ tests/unit/plugins/module_utils/test_clickhouse.py | 3 ++- 5 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/1-client.yml diff --git a/changelogs/fragments/1-client.yml b/changelogs/fragments/1-client.yml new file mode 100644 index 0000000..a1c9ef1 --- /dev/null +++ b/changelogs/fragments/1-client.yml @@ -0,0 +1,2 @@ +minor_changes: +- clickhouse_client - added the ``flatten_tested`` argument (https://github.com/ansible-collections/community.clickhouse/pull/63). diff --git a/plugins/doc_fragments/client_inst_opts.py b/plugins/doc_fragments/client_inst_opts.py index 7f0cf93..34b0cf9 100644 --- a/plugins/doc_fragments/client_inst_opts.py +++ b/plugins/doc_fragments/client_inst_opts.py @@ -51,10 +51,12 @@ class ModuleDocFragment(object): flatten_nested: description: - - Sets the C(flatten_nested) setting on session before running the query. + - Sets the C(flatten_nested) setting on session before + running the query. type: int choices: [0, 1] version_added: '0.5.0' + requirements: [ 'clickhouse-driver' ] notes: diff --git a/plugins/modules/clickhouse_client.py b/plugins/modules/clickhouse_client.py index c9156ca..6642e5d 100644 --- a/plugins/modules/clickhouse_client.py +++ b/plugins/modules/clickhouse_client.py @@ -283,9 +283,11 @@ def main(): # Substitute query params if needed for future return substituted_query = get_substituted_query(module, client, query, execute_kwargs) - # Execute query + # If support of arbitrary levels of nesting is needed when executing the main query if flatten_nested == 0: execute_query(module, client, "SET flatten_nested = 0", execute_kwargs) + + # Execute query result = execute_query(module, client, query, execute_kwargs) # Convert values not supported by ansible-core diff --git a/tests/integration/targets/clickhouse_client/tasks/initial.yml b/tests/integration/targets/clickhouse_client/tasks/initial.yml index 53c9aee..754c3e1 100644 --- a/tests/integration/targets/clickhouse_client/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_client/tasks/initial.yml @@ -72,21 +72,22 @@ that: - result.result == [["one"], ["two"], ["three"]] -- name: Load a Schema with Nested Fields +# https://github.com/ansible-collections/community.clickhouse/pull/63/ +- name: Load a schema with nested fields community.clickhouse.clickhouse_client: flatten_nested: 0 - execute: "CREATE TABLE foo.nested ( `coordinates` Nested(x int, y int )) ENGINE = Memory;" + execute: "CREATE TABLE foo.nested (`coordinates` Nested(x int, y int )) ENGINE = Memory" -- name: Load Nested Data +- name: Load nested data community.clickhouse.clickhouse_client: - execute: 'insert into foo.nested (coordinates) VALUES (([(10,20), (11,21)]));' + execute: "INSERT INTO foo.nested (coordinates) VALUES (([(10,20), (11,21)]))" -- name: Read Nested data +- name: Read nested data register: nested_result community.clickhouse.clickhouse_client: execute: 'select * from foo.nested;' -- name: Check Nested Data exists +- name: Check nested data exists ansible.builtin.assert: that: - nested_result.result[0][0][0][0] == 10 diff --git a/tests/unit/plugins/module_utils/test_clickhouse.py b/tests/unit/plugins/module_utils/test_clickhouse.py index 8a580bf..11a8b3d 100644 --- a/tests/unit/plugins/module_utils/test_clickhouse.py +++ b/tests/unit/plugins/module_utils/test_clickhouse.py @@ -37,7 +37,8 @@ def test_client_common_argument_spec(): 'login_user': {'type': 'str', 'default': None}, 'login_host': {'type': 'str', 'default': 'localhost'}, 'login_password': {'type': 'str', 'default': None, 'no_log': True}, - 'client_kwargs': {'type': 'dict', 'default': {}} + 'client_kwargs': {'type': 'dict', 'default': {}}, + 'flatten_nested': {'type': 'int', 'choices': [0, 1]}, } assert client_common_argument_spec() == EXPECTED