Skip to content

Commit

Permalink
remove external dependency for parsing SA version
Browse files Browse the repository at this point in the history
  • Loading branch information
rsyring committed Mar 26, 2019
1 parent 5dc58a1 commit 56d408c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion flask_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@

from pkg_resources import parse_version
import sqlalchemy


def parse_version(v):
"""
Take a string version and conver it to a tuple (for easier comparison), e.g.:
"1.2.3" --> (1, 2, 3)
"1.2" --> (1, 2, 0)
"1" --> (1, 0, 0)
"""
parts = v.split(".")
# Pad the list to make sure there is three elements so that we get major, minor, point
# comparisons that default to "0" if not given. I.e. "1.2" --> (1, 2, 0)
parts = (parts + 3 * ['0'])[:3]
return tuple(int(x) for x in parts)


def sqlalchemy_version(op, val):
sa_ver = parse_version(sqlalchemy.__version__)
target_ver = parse_version(val)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class TestSQLAlchemyVersion:
def test_parse_version(self):
assert utils.parse_version('1.2.3') == (1, 2, 3)
assert utils.parse_version('1.2') == (1, 2, 0)
assert utils.parse_version('1') == (1, 0, 0)

@mock.patch.object(utils, 'sqlalchemy')
def test_sqlalchemy_version(self, m_sqlalchemy):
Expand Down

0 comments on commit 56d408c

Please sign in to comment.