mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
250 lines
6.8 KiB
YAML
250 lines
6.8 KiB
YAML
|
# Test code for mysql_query module
|
||
|
# Copyright: (c) 2020, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru>
|
||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||
|
- vars:
|
||
|
mysql_parameters: &mysql_params
|
||
|
login_unix_socket: '{{ mysql_socket }}'
|
||
|
login_user: '{{ root_user }}'
|
||
|
login_password: '{{ root_pass }}'
|
||
|
|
||
|
block:
|
||
|
|
||
|
- name: Create db {{ test_db }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
query: 'CREATE DATABASE {{ test_db }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['CREATE DATABASE {{ test_db }}']
|
||
|
|
||
|
- name: Create {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'CREATE TABLE {{ test_table1 }} (id int)'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['CREATE TABLE {{ test_table1 }} (id int)']
|
||
|
|
||
|
- name: Insert test data
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query:
|
||
|
- 'INSERT INTO {{ test_table1 }} VALUES (1), (2)'
|
||
|
- 'INSERT INTO {{ test_table1 }} VALUES (3)'
|
||
|
single_transaction: yes
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.rowcount == [2, 1]
|
||
|
- result.executed_queries == ['INSERT INTO {{ test_table1 }} VALUES (1), (2)', 'INSERT INTO {{ test_table1 }} VALUES (3)']
|
||
|
|
||
|
- name: Check data in {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ['SELECT * FROM {{ test_table1 }}']
|
||
|
- result.rowcount == [3]
|
||
|
- result.query_result[0][0].id == 1
|
||
|
- result.query_result[0][1].id == 2
|
||
|
- result.query_result[0][2].id == 3
|
||
|
|
||
|
- name: Check data in {{ test_table1 }} using positional args
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }} WHERE id = %s'
|
||
|
positional_args:
|
||
|
- 1
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ["SELECT * FROM {{ test_table1 }} WHERE id = 1"]
|
||
|
- result.rowcount == [1]
|
||
|
- result.query_result[0][0].id == 1
|
||
|
|
||
|
- name: Check data in {{ test_table1 }} using named args
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }} WHERE id = %(some_id)s'
|
||
|
named_args:
|
||
|
some_id: 1
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ["SELECT * FROM {{ test_table1 }} WHERE id = 1"]
|
||
|
- result.rowcount == [1]
|
||
|
- result.query_result[0][0].id == 1
|
||
|
|
||
|
- name: Update data in {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'UPDATE {{ test_table1 }} SET id = %(new_id)s WHERE id = %(current_id)s'
|
||
|
named_args:
|
||
|
current_id: 1
|
||
|
new_id: 0
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['UPDATE {{ test_table1 }} SET id = 0 WHERE id = 1']
|
||
|
- result.rowcount == [1]
|
||
|
|
||
|
- name: Check the prev update - row with value 1 does not exist anymore
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }} WHERE id = %(some_id)s'
|
||
|
named_args:
|
||
|
some_id: 1
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ['SELECT * FROM {{ test_table1 }} WHERE id = 1']
|
||
|
- result.rowcount == [0]
|
||
|
|
||
|
- name: Check the prev update - row with value - exist
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }} WHERE id = %(some_id)s'
|
||
|
named_args:
|
||
|
some_id: 0
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ['SELECT * FROM {{ test_table1 }} WHERE id = 0']
|
||
|
- result.rowcount == [1]
|
||
|
|
||
|
- name: Update data in {{ test_table1 }} again
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'UPDATE {{ test_table1 }} SET id = %(new_id)s WHERE id = %(current_id)s'
|
||
|
named_args:
|
||
|
current_id: 1
|
||
|
new_id: 0
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ['UPDATE {{ test_table1 }} SET id = 0 WHERE id = 1']
|
||
|
- result.rowcount == [0]
|
||
|
|
||
|
- name: Delete data from {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query:
|
||
|
- 'DELETE FROM {{ test_table1 }} WHERE id = 0'
|
||
|
- 'SELECT * FROM {{ test_table1 }} WHERE id = 0'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['DELETE FROM {{ test_table1 }} WHERE id = 0', 'SELECT * FROM {{ test_table1 }} WHERE id = 0']
|
||
|
- result.rowcount == [1, 0]
|
||
|
|
||
|
- name: Delete data from {{ test_table1 }} again
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'DELETE FROM {{ test_table1 }} WHERE id = 0'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is not changed
|
||
|
- result.executed_queries == ['DELETE FROM {{ test_table1 }} WHERE id = 0']
|
||
|
- result.rowcount == [0]
|
||
|
|
||
|
- name: Truncate {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query:
|
||
|
- 'TRUNCATE {{ test_table1 }}'
|
||
|
- 'SELECT * FROM {{ test_table1 }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['TRUNCATE {{ test_table1 }}', 'SELECT * FROM {{ test_table1 }}']
|
||
|
- result.rowcount == [0, 0]
|
||
|
|
||
|
- name: Rename {{ test_table1 }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'RENAME TABLE {{ test_table1 }} TO {{ test_table2 }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['RENAME TABLE {{ test_table1 }} TO {{ test_table2 }}']
|
||
|
- result.rowcount == [0]
|
||
|
|
||
|
- name: Check the prev rename
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table1 }}'
|
||
|
register: result
|
||
|
ignore_errors: yes
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result.failed == true
|
||
|
|
||
|
- name: Check the prev rename
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
login_db: '{{ test_db }}'
|
||
|
query: 'SELECT * FROM {{ test_table2 }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result.rowcount == [0]
|
||
|
|
||
|
- name: Drop db {{ test_db }}
|
||
|
mysql_query:
|
||
|
<<: *mysql_params
|
||
|
query: 'DROP DATABASE {{ test_db }}'
|
||
|
register: result
|
||
|
|
||
|
- assert:
|
||
|
that:
|
||
|
- result is changed
|
||
|
- result.executed_queries == ['DROP DATABASE {{ test_db }}']
|