diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-08 11:28:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-08 11:28:14 +0000 |
commit | b678a621c57a6d3fdfac14bdbbef0ed743ab1742 (patch) | |
tree | 5481c14ce75dfda9c55721de033992b45ab0e1dc /test/features/db_utils.py | |
parent | Initial commit. (diff) | |
download | mycli-b678a621c57a6d3fdfac14bdbbef0ed743ab1742.tar.xz mycli-b678a621c57a6d3fdfac14bdbbef0ed743ab1742.zip |
Adding upstream version 1.22.2.upstream/1.22.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/features/db_utils.py')
-rw-r--r-- | test/features/db_utils.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/test/features/db_utils.py b/test/features/db_utils.py new file mode 100644 index 0000000..c29dedb --- /dev/null +++ b/test/features/db_utils.py @@ -0,0 +1,87 @@ +import pymysql + + +def create_db(hostname='localhost', username=None, password=None, + dbname=None): + """Create test database. + + :param hostname: string + :param username: string + :param password: string + :param dbname: string + :return: + + """ + cn = pymysql.connect( + host=hostname, + user=username, + password=password, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor + ) + + with cn.cursor() as cr: + cr.execute('drop database if exists ' + dbname) + cr.execute('create database ' + dbname) + + cn.close() + + cn = create_cn(hostname, password, username, dbname) + return cn + + +def create_cn(hostname, password, username, dbname): + """Open connection to database. + + :param hostname: + :param password: + :param username: + :param dbname: string + :return: psycopg2.connection + + """ + cn = pymysql.connect( + host=hostname, + user=username, + password=password, + db=dbname, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor + ) + + return cn + + +def drop_db(hostname='localhost', username=None, password=None, + dbname=None): + """Drop database. + + :param hostname: string + :param username: string + :param password: string + :param dbname: string + + """ + cn = pymysql.connect( + host=hostname, + user=username, + password=password, + db=dbname, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor + ) + + with cn.cursor() as cr: + cr.execute('drop database if exists ' + dbname) + + close_cn(cn) + + +def close_cn(cn=None): + """Close connection. + + :param connection: pymysql.connection + + """ + if cn: + cn.close() |