Skip to content
New issue

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

Add in_range helper method #224

Merged
merged 6 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ Argument | Description
`ensure_strategy` | Match the `AwesomeVersion` object against spesific strategies when creating if. If it does not match `AwesomeVersionStrategyException` will be raised
`find_first_match` | If True, the version given will be scanned for the first match of the given `ensure_strategy`. Raises `AwesomeVersionStrategyException` If it is not found for any of the given strategies.

## AwesomeVersion methods

<details>
<summary><code>AwesomeVersion.in_range</code></summary>

This is a helper method to check if the version is in a range.
This method takes two arguments, `lowest` and `highest`, both are required, and returns a boolean.

> **Note** This method is the same as doing `lowest <= AwesomeVersion <= highest`

Example:

```python
from awesomeversion import AwesomeVersion
print(AwesomeVersion("1.2.2").in_range("1.2.1", "1.3"))
> True
print(AwesomeVersion("1.2.0").in_range("1.2.1", "1.3"))
> False
```

</details>

## Example usage

Expand Down
12 changes: 12 additions & 0 deletions awesomeversion/awesomeversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def diff(self, compareto: VersionType) -> AwesomeVersionDiff:
}
)

def in_range(self, lowest: VersionType, highest: VersionType) -> bool:
"""Check if version is in range."""
if isinstance(lowest, (str, float, int)):
lowest = AwesomeVersion(lowest)
if isinstance(highest, (str, float, int)):
highest = AwesomeVersion(highest)
if not isinstance(lowest, AwesomeVersion):
raise AwesomeVersionCompareException("Lowest version is not valid")
if not isinstance(highest, AwesomeVersion):
raise AwesomeVersionCompareException("Highest version is not valid")
return lowest <= self <= highest

def section(self, idx: int) -> int:
"""Return the value of the specified section of the version."""
if self.sections >= (idx + 1):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ force_sort_within_sections = true
profile = "black"

[tool.pylint.'MESSAGES CONTROL']
disable = "unsubscriptable-object,duplicate-code"
disable = "unsubscriptable-object,duplicate-code,too-many-public-methods"


[tool.coverage.run]
Expand Down
46 changes: 45 additions & 1 deletion tests/test_awesomeversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test awesomeversion."""
from __future__ import annotations

import json
import warnings

Expand All @@ -9,7 +11,10 @@
AwesomeVersionStrategy,
AwesomeVersionStrategyException,
)
from awesomeversion.exceptions import AwesomeVersionException
from awesomeversion.exceptions import (
AwesomeVersionCompareException,
AwesomeVersionException,
)
from awesomeversion.strategy import COMPARABLE_STRATEGIES
from awesomeversion.typing import VersionType

Expand Down Expand Up @@ -246,3 +251,42 @@ def test_diff() -> None:

with pytest.raises(AwesomeVersionException):
version.diff(None)


@pytest.mark.parametrize(
"lowest,version,highest,result",
[
("14.0", "14.0", "14.0", True),
("14.0", "12.0", "15.0", False),
("14", "12.1", "15", False),
("14", "14.3", "15", True),
("1", 13, "15", True),
("1", AwesomeVersion(2), "15", True),
],
)
def test_in_range(
lowest: VersionType,
version: VersionType,
highest: VersionType,
result: bool,
) -> None:
"""Test AwesomeVersion.in_range"""
assert AwesomeVersion(version).in_range(lowest, highest) == result


@pytest.mark.parametrize(
"lowest,version,highest,match",
[
(None, "14.0", "14.0", "Lowest version is not valid"),
("14.0", "14.0", None, "Highest version is not valid"),
],
)
def test_in_range_exception(
lowest: VersionType | None,
version: VersionType,
highest: VersionType | None,
match: str,
) -> None:
"""Test AwesomeVersion.in_range exceptions"""
with pytest.raises(AwesomeVersionCompareException, match=match):
AwesomeVersion(version).in_range(lowest, highest)