Skip to content

Commit

Permalink
fix convert_unicode deprecation warning
Browse files Browse the repository at this point in the history
refs #681
refs #671
  • Loading branch information
rsyring committed Mar 9, 2019
1 parent bdc569b commit 70a6d65
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
6 changes: 5 additions & 1 deletion flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from flask_sqlalchemy.model import Model
from ._compat import itervalues, string_types, to_str, xrange
from .model import DefaultMeta
from . import utils

__version__ = '2.3.2'

Expand Down Expand Up @@ -564,7 +565,10 @@ def get_engine(self):
return rv

def get_options(self, sa_url, echo):
options = {'convert_unicode': True}
options = {}
if utils.sqlalchemy_version('<', '1.3'):
options['convert_unicode'] = True

self._sa.apply_pool_defaults(self._app, options)
self._sa.apply_driver_hacks(self._app, sa_url, options)
if echo:
Expand Down
20 changes: 20 additions & 0 deletions flask_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

from packaging import version
import sqlalchemy


def sqlalchemy_version(op, val):
sa_ver = version.parse(sqlalchemy.__version__)
target_ver = version.parse(val)

assert op in ('<', '>', '<=', '>=', '=='), 'op {} not supported'.format(op)

if op == '<':
return sa_ver < target_ver
if op == '>':
return sa_ver > target_ver
if op == '<=':
return sa_ver <= target_ver
if op == '>=':
return sa_ver >= target_ver
return sa_ver == target_ver
17 changes: 14 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ def test_uri_binds_warning(self, app, recwarn):
' "sqlite:///:memory:".'
assert recwarn[0].message.args[0] == expect

def test_engine_creation_ok(self, app):
def test_engine_creation_ok(self, app, recwarn):
""" create_engine() isn't called until needed. Let's make sure we can do that without
errors.
errors or warnings.
"""
assert fsa.SQLAlchemy(app).get_engine()
assert len(recwarn) == 0


@pytest.fixture
Expand Down Expand Up @@ -103,7 +104,17 @@ def test_engine_echo_true(self, m_create_engine, app_nr):
args, options = m_create_engine.call_args
assert options['echo'] is True

def test_convert_unicode_default(self, m_create_engine, app_nr):
@mock.patch.object(fsa.utils, 'sqlalchemy')
def test_convert_unicode_default_sa_13(self, m_sqlalchemy, m_create_engine, app_nr):
m_sqlalchemy.__version__ = '1.3'
fsa.SQLAlchemy(app_nr).get_engine()

args, options = m_create_engine.call_args
assert 'convert_unicode' not in options

@mock.patch.object(fsa.utils, 'sqlalchemy')
def test_convert_unicode_default_pre_sa_13(self, m_sqlalchemy, m_create_engine, app_nr):
m_sqlalchemy.__version__ = '1.2.99'
fsa.SQLAlchemy(app_nr).get_engine()

args, options = m_create_engine.call_args
Expand Down
24 changes: 24 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mock

from flask_sqlalchemy import utils


class TestSQLAlchemyVersion:

@mock.patch.object(utils, 'sqlalchemy')
def test_sqlalchemy_version(self, m_sqlalchemy):
m_sqlalchemy.__version__ = '1.3'

assert not utils.sqlalchemy_version('<', '1.3')
assert not utils.sqlalchemy_version('>', '1.3')
assert utils.sqlalchemy_version('<=', '1.3')
assert utils.sqlalchemy_version('==', '1.3')
assert utils.sqlalchemy_version('>=', '1.3')

m_sqlalchemy.__version__ = '1.2.99'

assert utils.sqlalchemy_version('<', '1.3')
assert not utils.sqlalchemy_version('>', '1.3')
assert utils.sqlalchemy_version('<=', '1.3')
assert not utils.sqlalchemy_version('==', '1.3')
assert not utils.sqlalchemy_version('>=', '1.3')

0 comments on commit 70a6d65

Please sign in to comment.