| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from typing import Callable | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
|
|
||
| class ComparableVersion: | ||
| def __init__(self, version): | ||
| self.version = Version(version) | ||
|
|
||
| def __lt__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x < y) | ||
|
|
||
| def __le__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x <= y) | ||
|
|
||
| def __eq__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x == y) | ||
|
|
||
| def __ne__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x != y) | ||
|
|
||
| def __gt__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x > y) | ||
|
|
||
| def __ge__(self, other: str): | ||
| return self._apply_op(other, lambda x, y: x >= y) | ||
|
|
||
| def _apply_op(self, other: str, op: Callable[[Version, Version], bool]): | ||
| other = Version(other) | ||
| return op(self.version, other) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import pytest | ||
| from packaging.version import InvalidVersion | ||
|
|
||
| from testcontainers.core.version import ComparableVersion | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def version(): | ||
| return ComparableVersion("1.0.0") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", False), ("1.0.0", False), ("1.1.0", True)]) | ||
| def test_lt(version, other_version, expected): | ||
| assert (version < other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", False), ("1.0.0", True), ("1.1.0", True)]) | ||
| def test_le(version, other_version, expected): | ||
| assert (version <= other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", False), ("1.0.0", True), ("1.1.0", False)]) | ||
| def test_eq(version, other_version, expected): | ||
| assert (version == other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", True), ("1.0.0", False), ("1.1.0", True)]) | ||
| def test_ne(version, other_version, expected): | ||
| assert (version != other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", True), ("1.0.0", False), ("1.1.0", False)]) | ||
| def test_gt(version, other_version, expected): | ||
| assert (version > other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("other_version, expected", [("0.9.0", True), ("1.0.0", True), ("1.1.0", False)]) | ||
| def test_ge(version, other_version, expected): | ||
| assert (version >= other_version) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "invalid_version", | ||
| [ | ||
| "invalid", | ||
| "1..0", | ||
| ], | ||
| ) | ||
| def test_invalid_version_raises_error(invalid_version): | ||
| with pytest.raises(InvalidVersion): | ||
| ComparableVersion(invalid_version) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "invalid_version", | ||
| [ | ||
| "invalid", | ||
| "1..0", | ||
| ], | ||
| ) | ||
| def test_comparison_with_invalid_version_raises_error(version, invalid_version): | ||
| with pytest.raises(InvalidVersion): | ||
| assert version < invalid_version | ||
|
|
||
| with pytest.raises(InvalidVersion): | ||
| assert version <= invalid_version | ||
|
|
||
| with pytest.raises(InvalidVersion): | ||
| assert version == invalid_version | ||
|
|
||
| with pytest.raises(InvalidVersion): | ||
| assert version != invalid_version | ||
|
|
||
| with pytest.raises(InvalidVersion): | ||
| assert version > invalid_version | ||
|
|
||
| with pytest.raises(InvalidVersion): | ||
| assert version >= invalid_version |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.