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 asserts to pytest style #1191

Merged
merged 13 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ ignore = [
"S605", # Starting a process with a shell: seems safe, but may be changed in the future; consider rewriting without `shell`
"S607", # Starting a process with a partial executable path
]
"test/test_private.py" = [
"RUF001", # String contains ambiguous `µ` (MICRO SIGN). Did you mean `μ` (GREEK SMALL LETTER MU)?
]

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
230 changes: 114 additions & 116 deletions test/test_charm.py

Large diffs are not rendered by default.

379 changes: 185 additions & 194 deletions test/test_framework.py

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ def test_fake_script_works(self):
# subprocess.getoutput goes via the shell, so it needs to be
# something both sh and CMD understand
output = subprocess.getoutput('foo a "b c " && bar "d e" f')
self.assertEqual(output, 'foo runs\nbar runs')
self.assertEqual(fake_script_calls(self), [
assert output == 'foo runs\nbar runs'
assert fake_script_calls(self) == [
['foo', 'a', 'b c '],
['bar', 'd e', 'f'],
])
]

def test_fake_script_clear(self):
fake_script(self, 'foo', 'echo foo runs')

output = subprocess.getoutput('foo a "b c"')
self.assertEqual(output, 'foo runs')
assert output == 'foo runs'

self.assertEqual(fake_script_calls(self, clear=True), [['foo', 'a', 'b c']])
assert fake_script_calls(self, clear=True) == [['foo', 'a', 'b c']]

fake_script(self, 'bar', 'echo bar runs')

output = subprocess.getoutput('bar "d e" f')
self.assertEqual(output, 'bar runs')
assert output == 'bar runs'

self.assertEqual(fake_script_calls(self, clear=True), [['bar', 'd e', 'f']])
assert fake_script_calls(self, clear=True) == [['bar', 'd e', 'f']]

self.assertEqual(fake_script_calls(self, clear=True), [])
assert fake_script_calls(self, clear=True) == []


class BaseTestCase(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion test/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def check(self, name: str):
else:
environ['PYTHONPATH'] = os.getcwd()
proc = subprocess.run([sys.executable, testfile], env=environ)
self.assertEqual(proc.returncode, 0)
assert proc.returncode == 0
88 changes: 45 additions & 43 deletions test/test_jujuversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import os
import unittest.mock # in this file, importing just 'patch' would be confusing

import pytest

import ops


Expand All @@ -35,61 +37,61 @@ def test_parsing(self):

for vs, major, minor, tag, patch, build in test_cases:
v = ops.JujuVersion(vs)
self.assertEqual(v.major, major)
self.assertEqual(v.minor, minor)
self.assertEqual(v.tag, tag)
self.assertEqual(v.patch, patch)
self.assertEqual(v.build, build)
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()
self.assertEqual(v, ops.JujuVersion('0.0.0'))
assert v == ops.JujuVersion('0.0.0')

os.environ['JUJU_VERSION'] = 'no'
with self.assertRaisesRegex(RuntimeError, 'not a valid Juju version'):
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()
self.assertEqual(v, ops.JujuVersion('2.8.0'))
assert v == ops.JujuVersion('2.8.0')

def test_has_app_data(self):
self.assertTrue(ops.JujuVersion('2.8.0').has_app_data())
self.assertTrue(ops.JujuVersion('2.7.0').has_app_data())
self.assertFalse(ops.JujuVersion('2.6.9').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(self):
self.assertTrue(ops.JujuVersion('2.8.0').is_dispatch_aware())
self.assertFalse(ops.JujuVersion('2.7.9').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(self):
self.assertTrue(ops.JujuVersion('2.8.0').has_controller_storage())
self.assertFalse(ops.JujuVersion('2.7.9').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(self):
self.assertTrue(ops.JujuVersion('3.0.3').has_secrets)
self.assertTrue(ops.JujuVersion('3.1.0').has_secrets)
self.assertFalse(ops.JujuVersion('3.0.2').has_secrets)
self.assertFalse(ops.JujuVersion('2.9.30').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(self):
self.assertTrue(ops.JujuVersion('3.0.3').supports_open_port_on_k8s)
self.assertTrue(ops.JujuVersion('3.3.0').supports_open_port_on_k8s)
self.assertFalse(ops.JujuVersion('3.0.2').supports_open_port_on_k8s)
self.assertFalse(ops.JujuVersion('2.9.30').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(self):
self.assertFalse(ops.JujuVersion('2.9.30').supports_exec_service_context)
self.assertTrue(ops.JujuVersion('4.0.0').supports_exec_service_context)
self.assertFalse(ops.JujuVersion('3.0.0').supports_exec_service_context)
self.assertFalse(ops.JujuVersion('3.1.5').supports_exec_service_context)
self.assertTrue(ops.JujuVersion('3.1.6').supports_exec_service_context)
self.assertFalse(ops.JujuVersion('3.2.0').supports_exec_service_context)
self.assertTrue(ops.JujuVersion('3.2.2').supports_exec_service_context)
self.assertTrue(ops.JujuVersion('3.3.0').supports_exec_service_context)
self.assertTrue(ops.JujuVersion('3.4.0').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

def test_parsing_errors(self):
invalid_versions = [
Expand All @@ -107,7 +109,7 @@ def test_parsing_errors(self):
"1.21-alpha123dev3", # Non-numeric string after the patch number.
]
for v in invalid_versions:
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
ops.JujuVersion(v)

def test_equality(self):
Expand Down Expand Up @@ -136,8 +138,8 @@ def test_equality(self):
]

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

def test_comparison(self):
test_cases = [
Expand Down Expand Up @@ -167,12 +169,12 @@ def test_comparison(self):

for a, b, expected_strict, expected_weak in test_cases:
with self.subTest(a=a, b=b):
self.assertEqual(ops.JujuVersion(a) < ops.JujuVersion(b), expected_strict)
self.assertEqual(ops.JujuVersion(a) <= ops.JujuVersion(b), expected_weak)
self.assertEqual(ops.JujuVersion(b) > ops.JujuVersion(a), expected_strict)
self.assertEqual(ops.JujuVersion(b) >= ops.JujuVersion(a), expected_weak)
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.
self.assertEqual(ops.JujuVersion(a) < b, expected_strict)
self.assertEqual(ops.JujuVersion(a) <= b, expected_weak)
self.assertEqual(b > ops.JujuVersion(a), expected_strict)
self.assertEqual(b >= ops.JujuVersion(a), expected_weak)
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