Skip to content

Commit

Permalink
tests: add a test cases for _parse_version()
Browse files Browse the repository at this point in the history
Refactor _parse_version() to make it more testable.
Add unit tests to validate its behavior.
  • Loading branch information
davvid committed Dec 18, 2024
1 parent 1fe2e9f commit 5b679a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 7 additions & 2 deletions qtpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,20 @@ def _parse_int(value):


def _parse_version(version):
"""Parse a version string into a tuple of ints"""
"""Parse a version into a comparable object"""
try:
from packaging.version import parse as _packaging_version_parse
except ImportError:
return tuple(_parse_int(x) for x in version.split("."))
return _parse_version_internal(version)
else:
return _packaging_version_parse(version)


def _parse_version_internal(version):
"""Parse a version string into a tuple of ints"""
return tuple(_parse_int(x) for x in version.split("."))


# Unless `FORCE_QT_API` is set, use previously imported Qt Python bindings
if not os.environ.get("FORCE_QT_API"):
if "PyQt5" in sys.modules:
Expand Down
27 changes: 26 additions & 1 deletion qtpy/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import pytest

from qtpy import API_NAMES, QtCore, QtGui, QtWidgets
from qtpy import (
API_NAMES,
QtCore,
QtGui,
QtWidgets,
_parse_version,
_parse_version_internal,
)
from qtpy.tests.utils import pytest_importorskip

with contextlib.suppress(Exception):
Expand Down Expand Up @@ -141,3 +148,21 @@ def test_qt_api_environ(api):
raise AssertionError('QtPy imported despite bad QT_API')
"""
subprocess.check_call([sys.executable, "-Oc", cmd], env=env)


@pytest.mark.parametrize(
"first,second",
[("1.2.3", "1.2.3.1"), ("1.2.3", "1.10.0")],
)
def test_parse_version(first, second):
"""Verify the behavior of _parse_version()"""
assert _parse_version(first) < _parse_version(second)


@pytest.mark.parametrize(
"value,expect",
[("1.2.3", (1, 2, 3)), ("1.x.3", (1, 0, 3))],
)
def test_parse_version_internal(value, expect):
"""Verify the behavior of _parse_version_internal()"""
assert _parse_version_internal(value) == expect

0 comments on commit 5b679a5

Please sign in to comment.