jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/678221 )
Change subject: [bugfix] Context manager depends on pymysql version, not Python release ......................................................................
[bugfix] Context manager depends on pymysql version, not Python release
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 --- M .codecov.yml M pywikibot/data/mysql.py M requirements.txt M tests/__init__.py A tests/mysql_tests.py 5 files changed, 42 insertions(+), 5 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/.codecov.yml b/.codecov.yml index 07adbc0..de5b045 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -15,7 +15,6 @@
ignore: - pywikibot/backports.py - - pywikibot/data/mysql.py - pywikibot/daemonize.py - pywikibot/families/__init__.py - scripts/archive/ diff --git a/pywikibot/data/mysql.py b/pywikibot/data/mysql.py index 4ba0b5f..9ab934b 100644 --- a/pywikibot/data/mysql.py +++ b/pywikibot/data/mysql.py @@ -4,6 +4,7 @@ # # Distributed under the terms of the MIT license. # +from distutils.version import LooseVersion from typing import Optional
import pywikibot @@ -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) @@ -59,13 +60,13 @@ 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)
@@ -76,5 +77,8 @@ 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 diff --git a/requirements.txt b/requirements.txt index 1bf0a27..8b64aec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -46,7 +46,7 @@ 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 diff --git a/tests/__init__.py b/tests/__init__.py index 38c49a0..04f61c3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -107,6 +107,7 @@ 'logentries', 'login', 'mediawikiversion', + 'mysql', 'namespace', 'oauth', 'page', diff --git a/tests/mysql_tests.py b/tests/mysql_tests.py new file mode 100644 index 0000000..9e921be --- /dev/null +++ b/tests/mysql_tests.py @@ -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()