Skip to content

Commit

Permalink
Programmatically create combined context manager in tests.
Browse files Browse the repository at this point in the history
It takes care of requiring a test exception or not, and using the class
patching context manager.
  • Loading branch information
ivilata committed Dec 11, 2023
1 parent ac4a5be commit e5250db
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions b2h5py/tests/test_dataset_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,18 @@ def tearDown(self):
b2h5py.patch_dataset_class()
super().tearDown()

@property
def error_cmgr(self):
return (self.assertRaises(CMTestError) if self.shall_raise
else contextlib.nullcontext())
def patching_cmgr(self):
"""Checks for error if `self.shall_raise`, patches dataset class"""
test_case = self

class CMTestContextManager(contextlib.ExitStack):
def __enter__(self):
if test_case.shall_raise:
self.enter_context(test_case.assertRaises(CMTestError))
self.enter_context(b2h5py.patching_dataset_class())
return super().__enter__()

return CMTestContextManager()

def maybe_raise(self):
if self.shall_raise:
Expand All @@ -118,14 +126,14 @@ def maybe_raise(self):
def test_default(self):
"""Dataset class is patched then unpatched"""
self.assertFalse(b2h5py.is_dataset_class_patched())
with self.error_cmgr:
with b2h5py.patching_dataset_class():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
with self.patching_cmgr():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
self.assertFalse(b2h5py.is_dataset_class_patched())

def test_exception(self):
"""Exceptions are propagated"""
# This test always raises, do not use `self.patching_cmgr()`.
with self.assertRaises(CMTestError):
with b2h5py.patching_dataset_class():
raise CMTestError
Expand All @@ -134,24 +142,21 @@ def test_already_patched(self):
"""Not unpatching if already patched before entry"""
b2h5py.patch_dataset_class()
self.assertTrue(b2h5py.is_dataset_class_patched())
with self.error_cmgr:
with b2h5py.patching_dataset_class():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
with self.patching_cmgr():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
self.assertTrue(b2h5py.is_dataset_class_patched())

def test_nested(self):
"""Nesting patching context managers"""
self.assertFalse(b2h5py.is_dataset_class_patched())
with self.error_cmgr:
with b2h5py.patching_dataset_class():
self.assertTrue(b2h5py.is_dataset_class_patched())
with self.error_cmgr:
with b2h5py.patching_dataset_class():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
with self.patching_cmgr():
self.assertTrue(b2h5py.is_dataset_class_patched())
with self.patching_cmgr():
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
self.assertTrue(b2h5py.is_dataset_class_patched())
self.maybe_raise()
self.assertFalse(b2h5py.is_dataset_class_patched())


Expand Down

0 comments on commit e5250db

Please sign in to comment.