From f316fc99e2b28981da1aa278f136032da6cc3eac Mon Sep 17 00:00:00 2001 From: Timur Gadiev Date: Tue, 1 Apr 2025 17:28:36 +0400 Subject: [PATCH] Add integration tests for 'prettytable' plugin --- plugins/modules/prettytable.py | 2 +- tests/integration/targets/prettytable/aliases | 1 + .../prettytable/files/aligned_table.txt | 5 + .../targets/prettytable/files/basic_table.txt | 6 + .../prettytable/files/custom_headers.txt | 5 + .../targets/prettytable/files/empty_table.txt | 2 + .../prettytable/files/ordered_table.txt | 6 + .../targets/prettytable/meta/main.yml | 1 + .../targets/prettytable/tasks/main.yml | 176 ++++++++++++++++++ 9 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/prettytable/aliases create mode 100644 tests/integration/targets/prettytable/files/aligned_table.txt create mode 100644 tests/integration/targets/prettytable/files/basic_table.txt create mode 100644 tests/integration/targets/prettytable/files/custom_headers.txt create mode 100644 tests/integration/targets/prettytable/files/empty_table.txt create mode 100644 tests/integration/targets/prettytable/files/ordered_table.txt create mode 100644 tests/integration/targets/prettytable/meta/main.yml create mode 100644 tests/integration/targets/prettytable/tasks/main.yml diff --git a/plugins/modules/prettytable.py b/plugins/modules/prettytable.py index c72d21d805..8239ad8b65 100644 --- a/plugins/modules/prettytable.py +++ b/plugins/modules/prettytable.py @@ -119,7 +119,7 @@ EXAMPLES = r''' - name: Bob age: 30 role: user - column_order: + column_order: - name - role - age diff --git a/tests/integration/targets/prettytable/aliases b/tests/integration/targets/prettytable/aliases new file mode 100644 index 0000000000..4434c7969e --- /dev/null +++ b/tests/integration/targets/prettytable/aliases @@ -0,0 +1 @@ +azp/posix/1 \ No newline at end of file diff --git a/tests/integration/targets/prettytable/files/aligned_table.txt b/tests/integration/targets/prettytable/files/aligned_table.txt new file mode 100644 index 0000000000..c0394afe70 --- /dev/null +++ b/tests/integration/targets/prettytable/files/aligned_table.txt @@ -0,0 +1,5 @@ ++------------+-----------------+--------+ +| date | description | amount | ++------------+-----------------+--------+ +| 2023-01-01 | Office supplies | 123.45 | ++------------+-----------------+--------+ diff --git a/tests/integration/targets/prettytable/files/basic_table.txt b/tests/integration/targets/prettytable/files/basic_table.txt new file mode 100644 index 0000000000..29f39077ec --- /dev/null +++ b/tests/integration/targets/prettytable/files/basic_table.txt @@ -0,0 +1,6 @@ ++-------+-----+-------+ +| name | age | role | ++-------+-----+-------+ +| Alice | 25 | admin | +| Bob | 30 | user | ++-------+-----+-------+ diff --git a/tests/integration/targets/prettytable/files/custom_headers.txt b/tests/integration/targets/prettytable/files/custom_headers.txt new file mode 100644 index 0000000000..1630b327db --- /dev/null +++ b/tests/integration/targets/prettytable/files/custom_headers.txt @@ -0,0 +1,5 @@ ++-----------+----------+-----------+ +| User Name | User Age | User Role | ++-----------+----------+-----------+ +| Alice | 25 | admin | ++-----------+----------+-----------+ diff --git a/tests/integration/targets/prettytable/files/empty_table.txt b/tests/integration/targets/prettytable/files/empty_table.txt new file mode 100644 index 0000000000..9b52315d4f --- /dev/null +++ b/tests/integration/targets/prettytable/files/empty_table.txt @@ -0,0 +1,2 @@ +++ +++ diff --git a/tests/integration/targets/prettytable/files/ordered_table.txt b/tests/integration/targets/prettytable/files/ordered_table.txt new file mode 100644 index 0000000000..de7fab56f6 --- /dev/null +++ b/tests/integration/targets/prettytable/files/ordered_table.txt @@ -0,0 +1,6 @@ ++-------+-------+-----+ +| role | name | age | ++-------+-------+-----+ +| admin | Alice | 25 | +| user | Bob | 30 | ++-------+-------+-----+ diff --git a/tests/integration/targets/prettytable/meta/main.yml b/tests/integration/targets/prettytable/meta/main.yml new file mode 100644 index 0000000000..ed97d539c0 --- /dev/null +++ b/tests/integration/targets/prettytable/meta/main.yml @@ -0,0 +1 @@ +--- diff --git a/tests/integration/targets/prettytable/tasks/main.yml b/tests/integration/targets/prettytable/tasks/main.yml new file mode 100644 index 0000000000..6ced925790 --- /dev/null +++ b/tests/integration/targets/prettytable/tasks/main.yml @@ -0,0 +1,176 @@ +--- +- name: Install required libs + pip: + name: prettytable + state: present + delegate_to: localhost + become: false + +# Test basic functionality +- name: Test basic table creation + prettytable: + data: + - name: Alice + age: 25 + role: admin + - name: Bob + age: 30 + role: user + register: basic_result + +- name: Load expected basic table output + set_fact: + expected_basic_table: "{{ lookup('file', 'basic_table.txt') }}" + +- name: Verify basic table output + assert: + that: + - basic_result.pretty_table | trim == expected_basic_table | trim + - not basic_result.failed + +# Test column ordering +- name: Test table with custom column order + prettytable: + data: + - name: Alice + age: 25 + role: admin + - name: Bob + age: 30 + role: user + column_order: + - role + - name + - age + register: ordered_result + +- name: Load expected ordered table output + set_fact: + expected_ordered_table: "{{ lookup('file', 'ordered_table.txt') }}" + +- name: Verify ordered table output + assert: + that: + - ordered_result.pretty_table | trim == expected_ordered_table | trim + - not ordered_result.failed + +# Test custom headers +- name: Test table with custom headers + prettytable: + data: + - name: Alice + age: 25 + role: admin + header_names: + - "User Name" + - "User Age" + - "User Role" + register: headers_result + +- name: Load expected headers table output + set_fact: + expected_headers_table: "{{ lookup('file', 'custom_headers.txt') }}" + +- name: Verify custom headers output + assert: + that: + - headers_result.pretty_table | trim == expected_headers_table | trim + - not headers_result.failed + +# Test column alignments +- name: Test table with alignments + prettytable: + data: + - date: "2023-01-01" + description: "Office supplies" + amount: 123.45 + column_alignments: + amount: right + description: left + date: center + register: aligned_result + +- name: Load expected aligned table output + set_fact: + expected_aligned_table: "{{ lookup('file', 'aligned_table.txt') }}" + +- name: Verify aligned table output + assert: + that: + - aligned_result.pretty_table | trim == expected_aligned_table | trim + - not aligned_result.failed + +# Test error conditions +- name: Test missing data parameter (should fail) + prettytable: {} # Empty dict to test missing required parameter + register: missing_data_result + ignore_errors: true + +- name: Verify error for missing data + vars: + expected_msg: 'missing required arguments: data' + assert: + that: + - missing_data_result.failed + - expected_msg in missing_data_result.msg + +- name: Test invalid data type (should fail) + prettytable: + data: "this is a string" + register: invalid_data_result + ignore_errors: true + +- name: Verify error for invalid data + vars: + expected_msg: "Expected a list of dictionaries, got a string" + assert: + that: + - invalid_data_result.failed + - expected_msg == invalid_data_result.msg + +- name: Test list with invalid items (should fail) + prettytable: + data: + - {"valid": "dict"} + - "invalid string item" + register: invalid_items_result + ignore_errors: true + +- name: Verify error for invalid items + vars: + expected_msg: "All items in the list must be dictionaries, got a string" + assert: + that: + - invalid_items_result.failed + - expected_msg == invalid_items_result.msg + +# Test empty data +- name: Test empty data list + prettytable: + data: [] + register: empty_result + +- name: Load expected empty table output + set_fact: + expected_empty_table: "{{ lookup('file', 'empty_table.txt') }}" + +- name: Verify empty table output + assert: + that: + - empty_result.pretty_table | trim == expected_empty_table | trim + - not empty_result.failed + +# Test invalid alignments +- name: Test invalid alignment value + prettytable: + data: + - col1: value1 + col2: value2 + column_alignments: + col1: invalid + register: invalid_alignment_result + +- name: Verify invalid alignment handling + assert: + that: + - not invalid_alignment_result.failed # Should not fail, just warn