Skip to content

Commit

Permalink
[bugfix] Context manager depends on pymysql version, not Python release
Browse files Browse the repository at this point in the history
If installing pymysql its version depends on Python release. But if
pymysql is already installed it may be outdated. pymysql >= 1.0.0 gives
a context manager with the connection but older don't. Therefore check
for the pymysql.__version__ and use closing context manager wrapper if
necessary.

- check for pymysql.__version__ instead Python release
- add mysql_tests.py to test this behaviour mentioned above
- yield 'test' if we just run test
- increase min version that defer_connect parameter can be used for
  tests
- add mysql to coverage

Bug: T279753
Change-Id: Ia1b1857f42661e76693e75a0260ac8841d9e6a91
  • Loading branch information
xqt committed Apr 9, 2021
1 parent abc8362 commit 5897777
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
1 change: 0 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ coverage:

ignore:
- pywikibot/backports.py
- pywikibot/data/mysql.py
- pywikibot/daemonize.py
- pywikibot/families/__init__.py
- scripts/archive/
Expand Down
10 changes: 7 additions & 3 deletions pywikibot/data/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# Distributed under the terms of the MIT license.
#
from distutils.version import LooseVersion
from typing import Optional

import pywikibot
Expand All @@ -15,7 +16,7 @@


from pywikibot import config2 as config
from pywikibot.tools import deprecated_args, PYTHON_VERSION
from pywikibot.tools import deprecated_args


@deprecated_args(encoding=None)
Expand Down Expand Up @@ -59,13 +60,13 @@ def mysql_query(query: str, params=None,
database=config.db_name_format.format(dbname),
port=config.db_port,
charset='utf8',
defer_connect=query == 'test', # for tests
**credentials)
if PYTHON_VERSION < (3, 6):
if LooseVersion(pymysql.__version__) < LooseVersion('1.0.0'):
from contextlib import closing
connection = closing(connection)

with connection as conn, conn.cursor() as cursor:

if verbose:
_query = cursor.mogrify(query, params)

Expand All @@ -76,5 +77,8 @@ def mysql_query(query: str, params=None,
for line in _query.splitlines())
pywikibot.output('Executing query:\n' + _query)

if query == 'test': # for tests only
yield query

cursor.execute(query, params)
yield from cursor
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sseclient >= 0.0.18,!=0.0.23,!=0.0.24
mwparserfromhell>=0.5.0

# The mysql generator in pagegenerators depends on PyMySQL
PyMySQL >= 0.6.6, < 1.0.0 ; python_version < '3.6'
PyMySQL >= 0.6.7, < 1.0.0 ; python_version < '3.6'
PyMySQL >= 1.0.0 ; python_version >= '3.6'

# core HTML comparison parser in diff module
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def create_path_func(base_func, subpath):
'logentries',
'login',
'mediawikiversion',
'mysql',
'namespace',
'oauth',
'page',
Expand Down
33 changes: 33 additions & 0 deletions tests/mysql_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for mysql module."""
#
# (C) Pywikibot team, 2021
#
# Distributed under the terms of the MIT license.
#
from contextlib import suppress
from types import GeneratorType

import unittest

from pywikibot import data

from tests.aspects import TestCase, require_modules


@require_modules('pymysql')
class TestMySQL(TestCase):

"""Test data.mysql."""

net = False

def test_mysql(self):
"""Test data.mysql.mysql_query function."""
result = data.mysql.mysql_query('test')
self.assertIsInstance(result, GeneratorType)
self.assertEqual(next(result), 'test')


if __name__ == '__main__': # pragma: no cover
with suppress(SystemExit):
unittest.main()

0 comments on commit 5897777

Please sign in to comment.