Skip to content

Commit

Permalink
support min/max only in VersionRange
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdual committed Aug 21, 2024
1 parent e701fac commit 1ec6eff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
16 changes: 6 additions & 10 deletions src/ua_generator/data/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import random
from typing import Union, List

from .. import exceptions


class Version:
major: int = None
Expand Down Expand Up @@ -116,21 +114,19 @@ class VersionRange:
max_version: Version = None

def __init__(self, min_version: Union[Version, int] = None, max_version: Union[Version, int] = None):
if min_version is None or max_version is None:
raise exceptions.InvalidArgumentError("min_version and max_version must be specified")

self.min_version = Version(major=min_version) if type(min_version) is int else min_version
self.max_version = Version(major=max_version) if type(max_version) is int else max_version

if self.max_version <= self.min_version:
raise exceptions.InvalidArgumentError("max_version must be greater than min_version")

def filter(self, versions: List[Version]) -> List[Version]:
tmp_versions: List[Version] = []

# TODO: Perhaps support for full version comparison, instead of just major versions
for version in versions:
# TODO: Perhaps support for full version comparison, instead of just major versions
if self.min_version.major <= version.major <= self.max_version.major:
if self.min_version is not None and self.max_version is not None and self.min_version.major <= version.major <= self.max_version.major:
tmp_versions.append(version)
elif self.min_version is not None and self.max_version is None and self.min_version.major <= version.major:
tmp_versions.append(version)
elif self.min_version is None and self.max_version is not None and version.major <= self.max_version.major:
tmp_versions.append(version)

return tmp_versions
13 changes: 0 additions & 13 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import src.ua_generator as ua_generator
from src.ua_generator import exceptions
from src.ua_generator.data.version import VersionRange


class TestExceptions(unittest.TestCase):
Expand Down Expand Up @@ -57,18 +56,6 @@ def raised_call():

self.assertRaises(exceptions.InvalidArgumentError, raised_call)

def test_version_range_error(self):
def raised_call():
VersionRange(min_version=2, max_version=None)

self.assertRaises(exceptions.InvalidArgumentError, raised_call)

def test_version_range_error_2(self):
def raised_call():
VersionRange(min_version=1, max_version=1)

self.assertRaises(exceptions.InvalidArgumentError, raised_call)


if __name__ == '__main__':
unittest.main()
28 changes: 28 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ def test_version_range_5(self):
self.assertIsNotNone(ua.generator.platform_version)
self.assertTrue(macos_min <= ua.generator.platform_version.major <= macos_max)

def test_version_range_min_only(self):
# MUST be valid version range
chrome_min = 124

for i in range(0, 100):
options = Options(
version_ranges={
'chrome': VersionRange(min_version=chrome_min),
})
ua = ua_generator.generate(browser='chrome', options=options)
self.assertTrue(ua.browser == 'chrome')
self.assertIsNotNone(ua.generator.browser_version)
self.assertTrue(chrome_min <= ua.generator.browser_version.major)

def test_version_range_max_only(self):
# MUST be valid version range
chrome_max = 125

for i in range(0, 100):
options = Options(
version_ranges={
'chrome': VersionRange(max_version=chrome_max),
})
ua = ua_generator.generate(browser='chrome', options=options)
self.assertTrue(ua.browser == 'chrome')
self.assertIsNotNone(ua.generator.browser_version)
self.assertTrue(ua.generator.browser_version.major <= chrome_max)

def test_version_range_invalid(self):
# MUST be INVALID version range
edge_min = 1
Expand Down

0 comments on commit 1ec6eff

Please sign in to comment.