From e615b6b28d0b2f9643f99266a7199907c62caa46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Sat, 12 Nov 2016 00:51:17 +0100 Subject: [PATCH] replaced mysql related code with pulgin out of extracted source --- setup.py | 2 +- src/pytest_dbfixtures/conf/dbfixtures.conf | 12 -- src/pytest_dbfixtures/factories/__init__.py | 3 - src/pytest_dbfixtures/factories/mysql.py | 158 ------------------ .../factories/mysql_client.py | 100 ----------- src/pytest_dbfixtures/plugin.py | 17 -- tests/test_mysql.py | 42 ----- 7 files changed, 1 insertion(+), 333 deletions(-) delete mode 100644 src/pytest_dbfixtures/factories/mysql.py delete mode 100644 src/pytest_dbfixtures/factories/mysql_client.py delete mode 100644 tests/test_mysql.py diff --git a/setup.py b/setup.py index c7ba740..62ccb3e 100644 --- a/setup.py +++ b/setup.py @@ -69,7 +69,7 @@ def read(fname): 'pytest-xdist==1.15.0', 'Mock==2.0.0', ], - 'mysql': ['mysqlclient'], + 'mysql': ['pytest-mysql'], 'postgresql': ['pytest-postgresql'], 'mongodb': ['pytest-mongo'], 'elasticsearch': ['pytest-elasticsearch'], diff --git a/src/pytest_dbfixtures/conf/dbfixtures.conf b/src/pytest_dbfixtures/conf/dbfixtures.conf index fa0c41a..a7085db 100644 --- a/src/pytest_dbfixtures/conf/dbfixtures.conf +++ b/src/pytest_dbfixtures/conf/dbfixtures.conf @@ -11,15 +11,3 @@ rabbit: params: '' host: '127.0.0.1' port: 5673 - -mysql: - mysql_init: /usr/bin/mysql_install_db - mysql_server: /usr/bin/mysqld_safe - mysql_admin: /usr/bin/mysqladmin - mysql_client: /usr/bin/mysql - host: 'localhost' - port: 3307 - db: tests - user: root - password: '' - params: '' diff --git a/src/pytest_dbfixtures/factories/__init__.py b/src/pytest_dbfixtures/factories/__init__.py index b9352f7..4cee761 100644 --- a/src/pytest_dbfixtures/factories/__init__.py +++ b/src/pytest_dbfixtures/factories/__init__.py @@ -18,14 +18,11 @@ # along with pytest-dbfixtures. If not, see . from pytest_dbfixtures.factories.redis import redis_proc, redisdb -from pytest_dbfixtures.factories.mysql import mysql_proc -from pytest_dbfixtures.factories.mysql_client import mysql from pytest_dbfixtures.factories.rabbitmq import rabbitmq_proc from pytest_dbfixtures.factories.rabbitmq_client import rabbitmq __all__ = [ redis_proc, redisdb, - mysql_proc, mysql, rabbitmq, rabbitmq_proc, ] diff --git a/src/pytest_dbfixtures/factories/mysql.py b/src/pytest_dbfixtures/factories/mysql.py deleted file mode 100644 index c68fd8e..0000000 --- a/src/pytest_dbfixtures/factories/mysql.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright (C) 2013 by Clearcode -# and associates (see AUTHORS). - -# This file is part of pytest-dbfixtures. - -# pytest-dbfixtures is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# pytest-dbfixtures is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public License -# along with pytest-dbfixtures. If not, see . - -import os -import shutil -import subprocess -from tempfile import mkdtemp - -import pytest -from path import path - -from pytest_dbfixtures.executors import TCPExecutor -from pytest_dbfixtures.port import get_port -from pytest_dbfixtures.utils import get_config - - -def remove_mysql_directory(datadir): - """ - Check mysql directory. Recursively delete a directory tree if exist. - - :param str datadir: path to datadir - - """ - if os.path.isdir(datadir): - shutil.rmtree(datadir) - - -def init_mysql_directory(mysql_init, datadir, tmpdir): - """ - #. Remove mysql directory if exist. - #. `Initialize MySQL data directory - `_ - - :param str mysql_init: mysql_init executable - :param str datadir: path to datadir - :param str tmpdir: path to tmpdir - - """ - remove_mysql_directory(datadir) - init_directory = ( - mysql_init, - '--user=%s' % os.getenv('USER'), - '--datadir=%s' % datadir, - '--tmpdir=%s' % tmpdir, - ) - subprocess.check_output(' '.join(init_directory), shell=True) - - -def mysql_proc(executable=None, admin_executable=None, init_executable=None, - host=None, port=-1, params=None, logs_prefix=''): - """ - Mysql server process factory. - - :param str executable: path to mysql executable - :param str admin_executable: path to mysql_admin executable - :param str init_executable: path to mysql_init executable - :param str host: hostname - :param str|int|tuple|set|list port: - exact port (e.g. '8000', 8000) - randomly selected port (None) - any random available port - [(2000,3000)] or (2000,3000) - random available port from a given range - [{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports - [(2000,3000), {4002,4003}] -random of given range and set - :param str params: additional command-line mysqld parameters - :param str logs_prefix: prefix for log filename - :rtype: func - :returns: function which makes a redis process - - """ - - @pytest.fixture(scope='session') - def mysql_proc_fixture(request): - """ - #. Get config. - #. Initialize MySQL data directory - #. `Start a mysqld server - `_ - #. Stop server and remove directory after tests. - `See `_ - - :param FixtureRequest request: fixture request object - :rtype: pytest_dbfixtures.executors.TCPExecutor - :returns: tcp executor - - """ - config = get_config(request) - mysql_exec = executable or config.mysql.mysql_server - mysql_admin_exec = admin_executable or config.mysql.mysql_admin - mysql_init = init_executable or config.mysql.mysql_init - mysql_port = get_port(port) or get_port(config.mysql.port) - mysql_host = host or config.mysql.host - mysql_params = params or config.mysql.params - - tmpdir = path(mkdtemp(prefix="pytest-mysql-")) - datadir = tmpdir / 'mysqldata_{port}'.format(port=mysql_port) - pidfile = tmpdir / 'mysql-server.{port}.pid'.format(port=mysql_port) - unixsocket = tmpdir / 'mysql.{port}.sock'.format(port=mysql_port) - logsdir = path(request.config.getvalue('logsdir')) - logfile_path = logsdir / '{prefix}mysql-server.{port}.log'.format( - prefix=logs_prefix, - port=mysql_port - ) - - init_mysql_directory(mysql_init, datadir, tmpdir) - - mysql_executor = TCPExecutor( - ''' - {mysql_server} --datadir={datadir} --pid-file={pidfile} - --port={port} --socket={socket} --log-error={logfile_path} - --tmpdir={tmpdir} --skip-syslog {params} - ''' - .format( - mysql_server=mysql_exec, - port=mysql_port, - datadir=datadir, - pidfile=pidfile, - socket=unixsocket, - logfile_path=logfile_path, - params=mysql_params, - tmpdir=tmpdir, - ), - host=mysql_host, - port=mysql_port, - ) - mysql_executor.socket_path = unixsocket - mysql_executor.start() - - def stop_server_and_remove_directory(): - shutdown_server = ( - mysql_admin_exec, - '--socket=%s' % unixsocket, - '--user=%s' % config.mysql.user, - 'shutdown' - ) - subprocess.check_output(' '.join(shutdown_server), shell=True) - mysql_executor.stop() - remove_mysql_directory(tmpdir) - - request.addfinalizer(stop_server_and_remove_directory) - - return mysql_executor - - return mysql_proc_fixture diff --git a/src/pytest_dbfixtures/factories/mysql_client.py b/src/pytest_dbfixtures/factories/mysql_client.py deleted file mode 100644 index 6a3bc6c..0000000 --- a/src/pytest_dbfixtures/factories/mysql_client.py +++ /dev/null @@ -1,100 +0,0 @@ -# Copyright (C) 2013 by Clearcode -# and associates (see AUTHORS). - -# This file is part of pytest-dbfixtures. - -# pytest-dbfixtures is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# pytest-dbfixtures is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public License -# along with pytest-dbfixtures. If not, see . -import pytest - -from pytest_dbfixtures.utils import get_config, try_import, get_process_fixture - - -def mysql(process_fixture_name, user=None, passwd=None, db=None, - charset='utf8', collation='utf8_general_ci'): - """ - Factory. Create connection to mysql. If you want you can give a scope, - default is 'session'. - - For charset and collation meaning, - see `Database Character Set and Collation - `_ - - :param str process_fixture_name: process fixture name - :param str user: mysql server user - :param str passwd: mysql server's password - :param str db: database's name - :param str charset: MySQL characterset to use by default - for *tests* database - :param str collation: MySQL collation to use by default - for *tests* database - - :returns: function ``mysql_fixture`` with suit scope - :rtype: func - """ - - @pytest.fixture - def mysql_fixture(request): - """ - #. Get config. - #. Try to import MySQLdb package. - #. Connect to mysql server. - #. Create database. - #. Use proper database. - #. Drop database after tests. - - :param FixtureRequest request: fixture request object - - :rtype: MySQLdb.connections.Connection - :returns: connection to database - """ - proc_fixture = get_process_fixture(request, process_fixture_name) - - config = get_config(request) - mysql_host = proc_fixture.host - mysql_user = user or config.mysql.user - mysql_passwd = passwd or config.mysql.password - mysql_db = db or config.mysql.db - - unixsocket = proc_fixture.socket_path - - MySQLdb, config = try_import( - 'MySQLdb', request, pypi_package='mysqlclient' - ) - - mysql_conn = MySQLdb.connect( - host=mysql_host, - unix_socket=unixsocket, - user=mysql_user, - passwd=mysql_passwd, - ) - - mysql_conn.query( - '''CREATE DATABASE {name} - DEFAULT CHARACTER SET {charset} - DEFAULT COLLATE {collation}''' - .format( - name=mysql_db, charset=charset, collation=collation - ) - ) - mysql_conn.query('USE %s' % mysql_db) - - def drop_database(): - mysql_conn.query('DROP DATABASE IF EXISTS %s' % mysql_db) - mysql_conn.close() - - request.addfinalizer(drop_database) - - return mysql_conn - - return mysql_fixture diff --git a/src/pytest_dbfixtures/plugin.py b/src/pytest_dbfixtures/plugin.py index 89508ca..5a6322a 100644 --- a/src/pytest_dbfixtures/plugin.py +++ b/src/pytest_dbfixtures/plugin.py @@ -15,11 +15,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with pytest-dbfixtures. If not, see . -import warnings - - from path import path -import pytest from pytest_dbfixtures import factories @@ -84,19 +80,6 @@ def pytest_load_initial_conftests(early_config, parser, args): redis_proc = factories.redis_proc() redisdb = factories.redisdb('redis_proc') -mysql_proc = factories.mysql_proc() -mysql = factories.mysql('mysql_proc') - - -@pytest.fixture -def mysqldb(mysql): - warnings.warn( - '`mysqldb` fixture is deprecated. Please use `mysql` instead.', - DeprecationWarning, - 2 - ) - return mysql - rabbitmq_proc = factories.rabbitmq_proc() rabbitmq = factories.rabbitmq('rabbitmq_proc') diff --git a/tests/test_mysql.py b/tests/test_mysql.py deleted file mode 100644 index 6687f91..0000000 --- a/tests/test_mysql.py +++ /dev/null @@ -1,42 +0,0 @@ -from pytest_dbfixtures import factories - - -query = '''CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), - species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);''' - - -def test_proc(mysql_proc): - assert mysql_proc.running() - - -def test_mysql(mysql): - cursor = mysql.cursor() - cursor.execute(query) - mysql.commit() - cursor.close() - - -mysql_proc2 = factories.mysql_proc(port=3308, params='--skip-sync-frm') -mysql2 = factories.mysql('mysql_proc2') - - -def test_mysql_newfixture(mysql, mysql2): - cursor = mysql.cursor() - cursor.execute(query) - mysql.commit() - cursor.close() - - cursor = mysql2.cursor() - cursor.execute(query) - mysql2.commit() - cursor.close() - - -mysql_rand_proc = factories.mysql_proc(port=None, params='--skip-sync-frm') -mysql_rand = factories.mysql('mysql_proc2') - - -def test_random_port(mysql_rand): - """Tests if mysql fixture can be started on random port""" - mysql = mysql_rand - mysql.cursor()