Skip to content

Commit

Permalink
docstrings for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dstandish committed Dec 1, 2022
1 parent 2346846 commit 2d84e5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
9 changes: 5 additions & 4 deletions airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,23 +649,24 @@ class _optionally_suppress(AbstractContextManager):
"""
Returns context manager that will swallow and log exceptions.
By default swallows descendents of Exception, but you can provide other classes through ``*args``.
By default swallows descendents of Exception, but you can provide other classes through
the vararg ``exceptions``.
Suppression behavior can be disabled with reraise=True.
:meta private:
"""

def __init__(self, *args, reraise=False):
def __init__(self, *exceptions, reraise=False):
self.reraise = reraise
self._exception = args or (Exception,)
self._exceptions = exceptions or (Exception,)

def __enter__(self):
return self

def __exit__(self, exctype, excinst, exctb):
error = exctype is not None
matching_error = error and issubclass(exctype, self._exception)
matching_error = error and issubclass(exctype, self._exceptions)
if error and not matching_error:
return False
elif matching_error and self.reraise:
Expand Down
74 changes: 50 additions & 24 deletions tests/providers/cncf/kubernetes/operators/test_kubernetes_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,36 +1022,62 @@ def test_task_id_as_name_dag_id_is_ignored(self):
assert re.match(r"a-very-reasonable-task-name-[a-z0-9-]+", pod.metadata.name) is not None


def test__suppress(caplog):
with _optionally_suppress(ValueError):
raise ValueError("failure")
assert "ValueError: failure" in caplog.text


def test__suppress_no_args(caplog):
with _optionally_suppress():
raise RuntimeError("failure")
assert "RuntimeError: failure" in caplog.text

class TestSuppress:
def test__suppress(self, caplog):
with _optionally_suppress(ValueError):
raise ValueError("failure")
assert "ValueError: failure" in caplog.text

def test__suppress_no_args_reraise(caplog):
with pytest.raises(RuntimeError):
with _optionally_suppress(reraise=True):
def test__suppress_no_args(self, caplog):
"""By default, suppresses Exception, so should suppress and log RuntimeError"""
with _optionally_suppress():
raise RuntimeError("failure")
assert caplog.text == ""
assert "RuntimeError: failure" in caplog.text

def test__suppress_no_args_reraise(self, caplog):
"""
By default, suppresses Exception, but with reraise=True,
should raise RuntimeError and not log.
"""
with pytest.raises(RuntimeError):
with _optionally_suppress(reraise=True):
raise RuntimeError("failure")
assert caplog.text == ""

def test__suppress_wrong_error(caplog):
with pytest.raises(RuntimeError):
with _optionally_suppress(ValueError):
raise RuntimeError("failure")
assert caplog.text == ""
def test__suppress_wrong_error(self, caplog):
"""
Here, we specify only catch ValueError. But we raise RuntimeError.
So it should raise and not log.
"""
with pytest.raises(RuntimeError):
with _optionally_suppress(ValueError):
raise RuntimeError("failure")
assert caplog.text == ""

def test__suppress_wrong_error_multiple(self, caplog):
"""
Here, we specify only catch RuntimeError/IndexError.
But we raise RuntimeError. So it should raise and not log.
"""
with pytest.raises(RuntimeError):
with _optionally_suppress(ValueError, IndexError):
raise RuntimeError("failure")
assert caplog.text == ""

def test__suppress_no_error(caplog):
with _optionally_suppress():
print("hi")
assert caplog.text == ""
def test__suppress_right_error_multiple(self, caplog):
"""
Here, we specify catch RuntimeError/IndexError.
And we raise RuntimeError. So it should suppress and log.
"""
with _optionally_suppress(ValueError, IndexError):
raise IndexError("failure")
assert "IndexError: failure" in caplog.text

def test__suppress_no_error(self, caplog):
"""When no error in context, should do nothing."""
with _optionally_suppress():
print("hi")
assert caplog.text == ""


@pytest.mark.parametrize(
Expand Down

0 comments on commit 2d84e5a

Please sign in to comment.