Skip to content

Commit

Permalink
Change tests (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Jan 30, 2021
1 parent d0e60b2 commit 9755f39
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 95 deletions.
14 changes: 9 additions & 5 deletions awesomeversion/awesomeversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, version: Union[str, float, int, "AwesomeVersion"]) -> None:
if isinstance(version, AwesomeVersion):
self._version = version._version
else:
self._version = version
self._version = str(version)
if isinstance(self._version, str):
self._version = self._version.strip()
str.__init__(self._version)
Expand Down Expand Up @@ -93,9 +93,9 @@ def section(self, idx: int) -> int:
@property
def string(self) -> str:
"""Return a string representaion of the version."""
if self._version.endswith("."):
self._version = self._version[:-1]
version = RE_VERSION.match(str(self._version)).group(2)
if version.endswith("."):
version = version[:-1]
return version

@property
Expand Down Expand Up @@ -134,7 +134,9 @@ def sections(self) -> int:
def modifier(self) -> str:
"""Return the modifier of the version if any."""
if self.strategy == AwesomeVersionStrategy.SEMVER:
match = RE_MODIFIER.match(RE_SEMVER.match(self.string).group(4))
match = RE_MODIFIER.match(RE_SEMVER.match(self.string).group(4) or "")
elif self.strategy == AwesomeVersionStrategy.SPECIALCONTAINER:
return None
else:
match = RE_MODIFIER.match(self.string.split(".")[-1])
return match.group(2) if match else None
Expand All @@ -143,7 +145,9 @@ def modifier(self) -> str:
def modifier_type(self) -> str:
"""Return the modifier type of the version if any."""
if self.strategy == AwesomeVersionStrategy.SEMVER:
match = RE_MODIFIER.match(RE_SEMVER.match(self.string).group(4))
match = RE_MODIFIER.match(RE_SEMVER.match(self.string).group(4) or "")
elif self.strategy == AwesomeVersionStrategy.SPECIALCONTAINER:
return None
else:
match = RE_MODIFIER.match(self.string.split(".")[-1])
return match.group(3) if match else None
Expand Down
1 change: 0 additions & 1 deletion awesomeversion/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def check(self) -> bool:
_LOGGER.debug(
"Comparing '%s' against '%s' with '%s'", ver_a, ver_b, handler.__name__
)
print(ver_a, ver_b, handler.__name__)
if len(compare.strategy) == 0 or (
self.ver_a.strategy in compare.strategy
and self.ver_b.strategy in compare.strategy
Expand Down
137 changes: 73 additions & 64 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,79 @@
from awesomeversion.exceptions import AwesomeVersionCompare


@pytest.mark.parametrize(
"version_a,version_b",
[
("2021.2.0", "2021.2.0.dev20210118"),
("2021.2.0b0", "2021.2.0.dev20210118"),
("2021.2.0", "2021.2.0b0"),
("2020.12.1", "2020.12.0"),
("2", "1"),
("2", 1),
(2, "1"),
(2, 1),
("2020", "2019"),
("1.2.3.4", "1.2.3"),
("2020.1", "2020"),
("2020.2.0", "2020.1.1."),
("1.2.3.4.5.6.7.8.9", "1"),
("2020.12.0", "2020.12.dev1602"),
("2020.12.dev1603", "2020.12.dev1602"),
("2021.1.0", "2021.1.0b0"),
("2021.1.0", "2021.1.0b0"),
("2021.1.0", "2021.1.0b1"),
("2021.1.0", "2021.1.0dev20210101"),
("2021.1.0b0", "2021.1.0a0"),
("2021.1.0b1", "2021.1.0b0"),
("2021.2.0", "2021.1.0b0"),
("2021.2.0b0", "2021.1.0"),
("beta", "stable"),
("dev", "latest"),
("latest", "2020.21.1"),
("latest", "beta"),
("1.0.0-alpha0", "1.0.0-beta1"),
("1.0.0-beta", "1.0.0-beta.1"),
("1.0.0-beta.2", "1.0.0-beta.1"),
("1.0.0-alpha0", "1.0.0-rc1"),
("1.0.0-alpha", "1.0.0-beta"),
("1.0.0", "1.0.0-beta"),
("2020", "2019"),
("2020", "2019"),
("2020", "2019"),
("2020", "2019"),
],
)
def test_compare(version_a, version_b):
"""Test compare."""
ver_a = AwesomeVersion(version_a)
ver_b = AwesomeVersion(version_b)

assert ver_a > ver_b
assert ver_a > ver_b
assert ver_a >= ver_b
assert ver_a != ver_b
assert ver_a > version_b
assert ver_a > version_b
assert ver_a >= version_b
assert ver_a != version_b
assert version_a > ver_b
assert version_a > ver_b
assert version_a >= ver_b
assert version_a != ver_b
assert ver_b < ver_a
assert ver_b <= ver_a
assert ver_b < version_a
assert ver_b <= version_a

if str(version_a).endswith("."):
version_a = version_a[:-1]
if str(version_b).endswith("."):
version_b = version_b[:-1]

assert ver_a.string == str(version_a)
assert ver_b.string == str(version_b)


def test_invalid_compare():
"""Test invalid compare."""
invalid = None
Expand All @@ -28,67 +101,3 @@ def test_invalid_compare():

with pytest.raises(AwesomeVersionCompare, match="Can't compare unknown"):
assert AwesomeVersion("2020.12.1") < AwesomeVersion("string")


def test_compare():
"""Test compare."""
assert AwesomeVersion("2021.2.0") > AwesomeVersion("2021.2.0.dev20210118")
assert AwesomeVersion("2021.2.0b0") > AwesomeVersion("2021.2.0.dev20210118")
assert AwesomeVersion("2021.2.0") > AwesomeVersion("2021.2.0b0")
assert AwesomeVersion("2021.2.0b0") == AwesomeVersion("2021.2.0b0")
assert AwesomeVersion("2021.2.0") != AwesomeVersion("2021.2.0b0")
assert AwesomeVersion("2020.12.1") > AwesomeVersion("2020.12.0")
assert AwesomeVersion("2") > AwesomeVersion(1)
assert AwesomeVersion("2020") > AwesomeVersion("2019")
assert AwesomeVersion("1.2.3.4") > AwesomeVersion("1.2.3")
assert AwesomeVersion("2020.1") > AwesomeVersion("2020")
assert not AwesomeVersion("0.97.0") > AwesomeVersion("2020.12.1")
assert AwesomeVersion("2020.2.0") > AwesomeVersion("2020.1.1.")
assert AwesomeVersion("2021.1.0") > AwesomeVersion("2021.1.0b0")
assert AwesomeVersion("2021.2.0") > AwesomeVersion("2021.1.0b0")
assert AwesomeVersion("2021.1.0") > AwesomeVersion("2021.1.0b0")
assert AwesomeVersion("2021.2.0b0") > AwesomeVersion("2021.1.0")
assert AwesomeVersion("2021.1.0") > AwesomeVersion("2021.1.0b1")
assert AwesomeVersion("2021.1.0b0") > AwesomeVersion("2021.1.0a0")
assert AwesomeVersion("2021.1.0b1") > AwesomeVersion("2021.1.0b0")
assert AwesomeVersion("2021.1.0") > AwesomeVersion("2021.1.0dev20210101")
assert AwesomeVersion("2020.12.dev1603") > AwesomeVersion("2020.12.dev1602")
assert not AwesomeVersion("2020.12.dev1602") > AwesomeVersion("2020.12.dev1603")
assert AwesomeVersion("2020.12.0") > AwesomeVersion("2020.12.dev1602")

assert AwesomeVersion("2020.12.0") < AwesomeVersion("2020.12.1")
assert AwesomeVersion("2019") < AwesomeVersion("2020")

assert not AwesomeVersion("2019") > AwesomeVersion("2020")
assert not AwesomeVersion("2021.1.0b0") > AwesomeVersion("2021.1.0")
assert AwesomeVersion("2021.1.0") > AwesomeVersion("2021.1.0b0")
assert AwesomeVersion("1.2.3.4.5.6.7.8.9") > AwesomeVersion("1")

assert AwesomeVersion("latest") > AwesomeVersion("2020.21.1")
assert AwesomeVersion("dev") > AwesomeVersion("latest")
assert AwesomeVersion("latest") > AwesomeVersion("beta")
assert AwesomeVersion("beta") > AwesomeVersion("stable")

assert AwesomeVersion("1") >= AwesomeVersion("1")
assert AwesomeVersion("2") >= AwesomeVersion("1")

assert AwesomeVersion("1") <= AwesomeVersion("1")
assert AwesomeVersion("1") <= AwesomeVersion("2")

assert AwesomeVersion("2") != AwesomeVersion("1")

assert AwesomeVersion("2") > "1"
assert AwesomeVersion("1") < 2
assert AwesomeVersion("2") == 2
assert AwesomeVersion("2") == "2"

version = AwesomeVersion("2021.2.0b1")
assert version >= AwesomeVersion("0.112.0")
assert version == "2021.2.0b1"

assert AwesomeVersion("1.0.0-alpha0") > AwesomeVersion("1.0.0-beta1")
assert AwesomeVersion("1.0.0-beta") > AwesomeVersion("1.0.0-beta.1")
assert AwesomeVersion("1.0.0-beta.2") > AwesomeVersion("1.0.0-beta.1")
assert AwesomeVersion("1.0.0-alpha0") > AwesomeVersion("1.0.0-rc1")
assert AwesomeVersion("1.0.0-alpha") > AwesomeVersion("1.0.0-beta")
assert AwesomeVersion("1.0.0") > AwesomeVersion("1.0.0-beta")
29 changes: 19 additions & 10 deletions tests/test_equals.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
"""Test equals."""
import pytest

from awesomeversion import AwesomeVersion


def test_equals():
@pytest.mark.parametrize(
"version",
[
("1"),
("2020.12.1"),
("2020"),
("2021.2.0b0"),
("2021.2.0.dev20210118"),
],
)
def test_equals(version):
"""Test equals."""
assert AwesomeVersion("2020.12.1") == AwesomeVersion("2020.12.1")
assert AwesomeVersion("2020") == AwesomeVersion("2020")
assert AwesomeVersion("2020") == AwesomeVersion("v2020")
assert AwesomeVersion("2020") == AwesomeVersion("V2020")
assert AwesomeVersion("2020") != AwesomeVersion("2021")
assert str(AwesomeVersion("2021.2.0b0")) == "2021.2.0b0"
assert AwesomeVersion("2021.2.0b0").string == "2021.2.0b0"
assert str(AwesomeVersion("2021.2.0.dev20210118")) == "2021.2.0.dev20210118"
assert AwesomeVersion("2021.2.0.dev20210118").string == "2021.2.0.dev20210118"
version_object = AwesomeVersion(version)
assert version_object == version
assert str(version_object) == version
assert version_object.string == version
assert version_object == AwesomeVersion(f"v{version}")
assert version_object == AwesomeVersion(f"v{version}")
31 changes: 16 additions & 15 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""Test match."""
import pytest

from awesomeversion.match import version_strategy
from awesomeversion import AwesomeVersion
from awesomeversion.strategy import AwesomeVersionStrategy

test_data = [
("123", AwesomeVersionStrategy.BUILDVER),
("20.1.0", AwesomeVersionStrategy.CALVER),
("0.118.0", AwesomeVersionStrategy.SEMVER),
("1.2.3.4.5", AwesomeVersionStrategy.SIMPLEVER),
("string", AwesomeVersionStrategy.UNKNOWN),
("latest", AwesomeVersionStrategy.SPECIALCONTAINER),
("stable", AwesomeVersionStrategy.SPECIALCONTAINER),
("beta", AwesomeVersionStrategy.SPECIALCONTAINER),
("dev", AwesomeVersionStrategy.SPECIALCONTAINER),
]


@pytest.mark.parametrize("version,strategy", test_data)
@pytest.mark.parametrize(
"version,strategy",
[
("0.118.0", AwesomeVersionStrategy.SEMVER),
("1.2.3.4.5", AwesomeVersionStrategy.SIMPLEVER),
("123", AwesomeVersionStrategy.BUILDVER),
("20.1.0", AwesomeVersionStrategy.CALVER),
("beta", AwesomeVersionStrategy.SPECIALCONTAINER),
("dev", AwesomeVersionStrategy.SPECIALCONTAINER),
("latest", AwesomeVersionStrategy.SPECIALCONTAINER),
("stable", AwesomeVersionStrategy.SPECIALCONTAINER),
("string", AwesomeVersionStrategy.UNKNOWN),
],
)
def test_strategy_match(version, strategy):
"""Test that the version matches the expected strategy."""
assert version_strategy(version) == strategy
assert AwesomeVersion(version).strategy == strategy
26 changes: 26 additions & 0 deletions tests/test_version_scheme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test test_version_scheme."""
import pytest

from awesomeversion import AwesomeVersion
from awesomeversion.strategy import AwesomeVersionStrategy


@pytest.mark.parametrize(
"version,strategy,dev,beta,modifier,modifier_type",
[
("0.118.0", AwesomeVersionStrategy.SEMVER, False, False, None, None),
("1.0.0-beta.1", AwesomeVersionStrategy.SEMVER, False, True, "beta.1", "beta"),
("v1.0.0-beta.1", AwesomeVersionStrategy.SEMVER, False, True, "beta.1", "beta"),
("2021.2.0.dev1", AwesomeVersionStrategy.CALVER, True, False, "dev1", "dev"),
("stable", AwesomeVersionStrategy.SPECIALCONTAINER, False, False, None, None),
],
)
def test_version_scheme(version, strategy, dev, beta, modifier, modifier_type):
"""Test that the version matches the expected scheme."""
version_object = AwesomeVersion(version)
assert str(version_object) == version
assert version_object.strategy == strategy
assert version_object.dev == dev
assert version_object.beta == beta
assert version_object.modifier == modifier
assert version_object.modifier_type == modifier_type

0 comments on commit 9755f39

Please sign in to comment.