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

test: refactor some for loop tests into pytest parametrize #1192

Merged
merged 4 commits into from
Apr 22, 2024
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
316 changes: 158 additions & 158 deletions test/test_jujuversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,161 +20,161 @@
import ops


class TestJujuVersion(unittest.TestCase):

def test_parsing(self):
test_cases = [
("0.0.0", 0, 0, '', 0, 0),
("0.0.2", 0, 0, '', 2, 0),
("0.1.0", 0, 1, '', 0, 0),
("0.2.3", 0, 2, '', 3, 0),
("10.234.3456", 10, 234, '', 3456, 0),
("10.234.3456.1", 10, 234, '', 3456, 1),
("1.21-alpha12", 1, 21, 'alpha', 12, 0),
("1.21-alpha1.34", 1, 21, 'alpha', 1, 34),
("2.7", 2, 7, '', 0, 0)
]

for vs, major, minor, tag, patch, build in test_cases:
v = ops.JujuVersion(vs)
assert v.major == major
assert v.minor == minor
assert v.tag == tag
assert v.patch == patch
assert v.build == build

@unittest.mock.patch('os.environ', new={}) # type: ignore
def test_from_environ(self):
# JUJU_VERSION is not set
v = ops.JujuVersion.from_environ()
assert v == ops.JujuVersion('0.0.0')

os.environ['JUJU_VERSION'] = 'no'
with pytest.raises(RuntimeError, match='not a valid Juju version'):
ops.JujuVersion.from_environ()

os.environ['JUJU_VERSION'] = '2.8.0'
v = ops.JujuVersion.from_environ()
assert v == ops.JujuVersion('2.8.0')

def test_has_app_data(self):
assert ops.JujuVersion('2.8.0').has_app_data()
assert ops.JujuVersion('2.7.0').has_app_data()
assert not ops.JujuVersion('2.6.9').has_app_data()

def test_is_dispatch_aware(self):
assert ops.JujuVersion('2.8.0').is_dispatch_aware()
assert not ops.JujuVersion('2.7.9').is_dispatch_aware()

def test_has_controller_storage(self):
assert ops.JujuVersion('2.8.0').has_controller_storage()
assert not ops.JujuVersion('2.7.9').has_controller_storage()

def test_has_secrets(self):
assert ops.JujuVersion('3.0.3').has_secrets
assert ops.JujuVersion('3.1.0').has_secrets
assert not ops.JujuVersion('3.0.2').has_secrets
assert not ops.JujuVersion('2.9.30').has_secrets

def test_supports_open_port_on_k8s(self):
assert ops.JujuVersion('3.0.3').supports_open_port_on_k8s
assert ops.JujuVersion('3.3.0').supports_open_port_on_k8s
assert not ops.JujuVersion('3.0.2').supports_open_port_on_k8s
assert not ops.JujuVersion('2.9.30').supports_open_port_on_k8s

def test_supports_exec_service_context(self):
assert not ops.JujuVersion('2.9.30').supports_exec_service_context
assert ops.JujuVersion('4.0.0').supports_exec_service_context
assert not ops.JujuVersion('3.0.0').supports_exec_service_context
assert not ops.JujuVersion('3.1.5').supports_exec_service_context
assert ops.JujuVersion('3.1.6').supports_exec_service_context
assert not ops.JujuVersion('3.2.0').supports_exec_service_context
assert ops.JujuVersion('3.2.2').supports_exec_service_context
assert ops.JujuVersion('3.3.0').supports_exec_service_context
assert ops.JujuVersion('3.4.0').supports_exec_service_context

def test_parsing_errors(self):
invalid_versions = [
"xyz",
"foo.bar",
"foo.bar.baz",
"dead.beef.ca.fe",
"1234567890.2.1", # The major version is too long.
"0.2..1", # Two periods next to each other.
"1.21.alpha1", # Tag comes after period.
"1.21-alpha", # No patch number but a tag is present.
"1.21-alpha1beta", # Non-numeric string after the patch number.
"1.21-alpha-dev", # Tag duplication.
"1.21-alpha_dev3", # Underscore in a tag.
"1.21-alpha123dev3", # Non-numeric string after the patch number.
]
for v in invalid_versions:
with pytest.raises(RuntimeError):
ops.JujuVersion(v)

def test_equality(self):
test_cases = [
("1.0.0", "1.0.0", True),
("01.0.0", "1.0.0", True),
("10.0.0", "9.0.0", False),
("1.0.0", "1.0.1", False),
("1.0.1", "1.0.0", False),
("1.0.0", "1.1.0", False),
("1.1.0", "1.0.0", False),
("1.0.0", "2.0.0", False),
("1.2-alpha1", "1.2.0", False),
("1.2-alpha2", "1.2-alpha1", False),
("1.2-alpha2.1", "1.2-alpha2", False),
("1.2-alpha2.2", "1.2-alpha2.1", False),
("1.2-beta1", "1.2-alpha1", False),
("1.2-beta1", "1.2-alpha2.1", False),
("1.2-beta1", "1.2.0", False),
("1.2.1", "1.2.0", False),
("2.0.0", "1.0.0", False),
("2.0.0.0", "2.0.0", True),
("2.0.0.0", "2.0.0.0", True),
("2.0.0.1", "2.0.0.0", False),
("2.0.1.10", "2.0.0.0", False),
]

for a, b, expected in test_cases:
assert (ops.JujuVersion(a) == ops.JujuVersion(b)) == expected
assert (ops.JujuVersion(a) == b) == expected

def test_comparison(self):
test_cases = [
("1.0.0", "1.0.0", False, True),
("01.0.0", "1.0.0", False, True),
("10.0.0", "9.0.0", False, False),
("1.0.0", "1.0.1", True, True),
("1.0.1", "1.0.0", False, False),
("1.0.0", "1.1.0", True, True),
("1.1.0", "1.0.0", False, False),
("1.0.0", "2.0.0", True, True),
("1.2-alpha1", "1.2.0", True, True),
("1.2-alpha2", "1.2-alpha1", False, False),
("1.2-alpha2.1", "1.2-alpha2", False, False),
("1.2-alpha2.2", "1.2-alpha2.1", False, False),
("1.2-beta1", "1.2-alpha1", False, False),
("1.2-beta1", "1.2-alpha2.1", False, False),
("1.2-beta1", "1.2.0", True, True),
("1.2.1", "1.2.0", False, False),
("2.0.0", "1.0.0", False, False),
("2.0.0.0", "2.0.0", False, True),
("2.0.0.0", "2.0.0.0", False, True),
("2.0.0.1", "2.0.0.0", False, False),
("2.0.1.10", "2.0.0.0", False, False),
("2.10.0", "2.8.0", False, False),
]

for a, b, expected_strict, expected_weak in test_cases:
with self.subTest(a=a, b=b):
assert (ops.JujuVersion(a) < ops.JujuVersion(b)) == expected_strict
assert (ops.JujuVersion(a) <= ops.JujuVersion(b)) == expected_weak
assert (ops.JujuVersion(b) > ops.JujuVersion(a)) == expected_strict
assert (ops.JujuVersion(b) >= ops.JujuVersion(a)) == expected_weak
# Implicit conversion.
assert (ops.JujuVersion(a) < b) == expected_strict
assert (ops.JujuVersion(a) <= b) == expected_weak
assert (b > ops.JujuVersion(a)) == expected_strict
assert (b >= ops.JujuVersion(a)) == expected_weak
@pytest.mark.parametrize("vs,major,minor,tag,patch,build", [
("0.0.0", 0, 0, '', 0, 0),
("0.0.2", 0, 0, '', 2, 0),
("0.1.0", 0, 1, '', 0, 0),
("0.2.3", 0, 2, '', 3, 0),
("10.234.3456", 10, 234, '', 3456, 0),
("10.234.3456.1", 10, 234, '', 3456, 1),
("1.21-alpha12", 1, 21, 'alpha', 12, 0),
("1.21-alpha1.34", 1, 21, 'alpha', 1, 34),
("2.7", 2, 7, '', 0, 0)
])
def test_parsing(vs: str, major: int, minor: int, tag: str, patch: int, build: int):
v = ops.JujuVersion(vs)
assert v.major == major
assert v.minor == minor
assert v.tag == tag
assert v.patch == patch
assert v.build == build


@unittest.mock.patch('os.environ', new={}) # type: ignore
def test_from_environ():
# JUJU_VERSION is not set
v = ops.JujuVersion.from_environ()
assert v == ops.JujuVersion('0.0.0')

os.environ['JUJU_VERSION'] = 'no'
with pytest.raises(RuntimeError, match='not a valid Juju version'):
ops.JujuVersion.from_environ()

os.environ['JUJU_VERSION'] = '2.8.0'
v = ops.JujuVersion.from_environ()
assert v == ops.JujuVersion('2.8.0')


def test_has_app_data():
assert ops.JujuVersion('2.8.0').has_app_data()
assert ops.JujuVersion('2.7.0').has_app_data()
assert not ops.JujuVersion('2.6.9').has_app_data()


def test_is_dispatch_aware():
assert ops.JujuVersion('2.8.0').is_dispatch_aware()
assert not ops.JujuVersion('2.7.9').is_dispatch_aware()


def test_has_controller_storage():
assert ops.JujuVersion('2.8.0').has_controller_storage()
assert not ops.JujuVersion('2.7.9').has_controller_storage()


def test_has_secrets():
assert ops.JujuVersion('3.0.3').has_secrets
assert ops.JujuVersion('3.1.0').has_secrets
assert not ops.JujuVersion('3.0.2').has_secrets
assert not ops.JujuVersion('2.9.30').has_secrets


def test_supports_open_port_on_k8s():
assert ops.JujuVersion('3.0.3').supports_open_port_on_k8s
assert ops.JujuVersion('3.3.0').supports_open_port_on_k8s
assert not ops.JujuVersion('3.0.2').supports_open_port_on_k8s
assert not ops.JujuVersion('2.9.30').supports_open_port_on_k8s


def test_supports_exec_service_context():
assert not ops.JujuVersion('2.9.30').supports_exec_service_context
assert ops.JujuVersion('4.0.0').supports_exec_service_context
assert not ops.JujuVersion('3.0.0').supports_exec_service_context
assert not ops.JujuVersion('3.1.5').supports_exec_service_context
assert ops.JujuVersion('3.1.6').supports_exec_service_context
assert not ops.JujuVersion('3.2.0').supports_exec_service_context
assert ops.JujuVersion('3.2.2').supports_exec_service_context
assert ops.JujuVersion('3.3.0').supports_exec_service_context
assert ops.JujuVersion('3.4.0').supports_exec_service_context


@pytest.mark.parametrize("invalid_version", [
"xyz",
"foo.bar",
"foo.bar.baz",
"dead.beef.ca.fe",
"1234567890.2.1", # The major version is too long.
"0.2..1", # Two periods next to each other.
"1.21.alpha1", # Tag comes after period.
"1.21-alpha", # No patch number but a tag is present.
"1.21-alpha1beta", # Non-numeric string after the patch number.
"1.21-alpha-dev", # Tag duplication.
"1.21-alpha_dev3", # Underscore in a tag.
"1.21-alpha123dev3", # Non-numeric string after the patch number.
])
def test_parsing_errors(invalid_version: str):
with pytest.raises(RuntimeError):
ops.JujuVersion(invalid_version)


@pytest.mark.parametrize("a,b,expected", [
("1.0.0", "1.0.0", True),
("01.0.0", "1.0.0", True),
("10.0.0", "9.0.0", False),
("1.0.0", "1.0.1", False),
("1.0.1", "1.0.0", False),
("1.0.0", "1.1.0", False),
("1.1.0", "1.0.0", False),
("1.0.0", "2.0.0", False),
("1.2-alpha1", "1.2.0", False),
("1.2-alpha2", "1.2-alpha1", False),
("1.2-alpha2.1", "1.2-alpha2", False),
("1.2-alpha2.2", "1.2-alpha2.1", False),
("1.2-beta1", "1.2-alpha1", False),
("1.2-beta1", "1.2-alpha2.1", False),
("1.2-beta1", "1.2.0", False),
("1.2.1", "1.2.0", False),
("2.0.0", "1.0.0", False),
("2.0.0.0", "2.0.0", True),
("2.0.0.0", "2.0.0.0", True),
("2.0.0.1", "2.0.0.0", False),
("2.0.1.10", "2.0.0.0", False),
])
def test_equality(a: str, b: str, expected: bool):
assert (ops.JujuVersion(a) == ops.JujuVersion(b)) == expected
assert (ops.JujuVersion(a) == b) == expected


@pytest.mark.parametrize("a,b,expected_strict,expected_weak", [
("1.0.0", "1.0.0", False, True),
("01.0.0", "1.0.0", False, True),
("10.0.0", "9.0.0", False, False),
("1.0.0", "1.0.1", True, True),
("1.0.1", "1.0.0", False, False),
("1.0.0", "1.1.0", True, True),
("1.1.0", "1.0.0", False, False),
("1.0.0", "2.0.0", True, True),
("1.2-alpha1", "1.2.0", True, True),
("1.2-alpha2", "1.2-alpha1", False, False),
("1.2-alpha2.1", "1.2-alpha2", False, False),
("1.2-alpha2.2", "1.2-alpha2.1", False, False),
("1.2-beta1", "1.2-alpha1", False, False),
("1.2-beta1", "1.2-alpha2.1", False, False),
("1.2-beta1", "1.2.0", True, True),
("1.2.1", "1.2.0", False, False),
("2.0.0", "1.0.0", False, False),
("2.0.0.0", "2.0.0", False, True),
("2.0.0.0", "2.0.0.0", False, True),
("2.0.0.1", "2.0.0.0", False, False),
("2.0.1.10", "2.0.0.0", False, False),
("2.10.0", "2.8.0", False, False),
])
def test_comparison(a: str, b: str, expected_strict: bool, expected_weak: bool):
assert (ops.JujuVersion(a) < ops.JujuVersion(b)) == expected_strict
assert (ops.JujuVersion(a) <= ops.JujuVersion(b)) == expected_weak
assert (ops.JujuVersion(b) > ops.JujuVersion(a)) == expected_strict
assert (ops.JujuVersion(b) >= ops.JujuVersion(a)) == expected_weak
# Implicit conversion.
assert (ops.JujuVersion(a) < b) == expected_strict
assert (ops.JujuVersion(a) <= b) == expected_weak
assert (b > ops.JujuVersion(a)) == expected_strict
assert (b >= ops.JujuVersion(a)) == expected_weak
Loading
Loading