From 9292e38cd8831d2b5725bbe8e4108fcda72caafc Mon Sep 17 00:00:00 2001 From: Sander Roet Date: Fri, 22 Mar 2024 17:04:30 +0100 Subject: [PATCH] add test for completely missing data --- src/pytom_tm/io.py | 4 +++- tests/Data/header_only.mrc | Bin 0 -> 1024 bytes tests/test_io.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/Data/header_only.mrc diff --git a/src/pytom_tm/io.py b/src/pytom_tm/io.py index 0699cdd6..bfe4667b 100644 --- a/src/pytom_tm/io.py +++ b/src/pytom_tm/io.py @@ -146,7 +146,9 @@ def wrap_mrcfile_readers(func, *args, **kwargs): "However, loading with 'permissive=True' did generate data, make sure this is correct!") else: logging.debug(f"Could not reasonably recover") - raise err + raise ValueError( + f"{args[0]} header or data is too corrupt to recover, please fix the header or data" + ) from err yield mrc # this should only be called after the context exists mrc.close() diff --git a/tests/Data/header_only.mrc b/tests/Data/header_only.mrc new file mode 100644 index 0000000000000000000000000000000000000000..0cd033e28ffaa494711f7b2f8cf27d6788c2ff6d GIT binary patch literal 1024 zcmY#jU|>*yVkRJs3>1JI7zQ$(kT8%50gONjq>mZOXJBZsU-fW@Eu4$Yh?wh!%q5@Q bV2@9)iz5R=azVEpJ~g9qqaiRF0t^fQgfs~0 literal 0 HcmV?d00001 diff --git a/tests/test_io.py b/tests/test_io.py index 1d82aeee..50d6341a 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -3,6 +3,9 @@ import pathlib FAILING_MRC = pathlib.Path(__file__).parent.joinpath(pathlib.Path('Data/human_ribo_mask_32_8_5.mrc')) +# The below file was made with head -c 1024 human_ribo_mask_32_8_5.mrc > header_only.mrc +CORRUPT_MRC = pathlib.Path(__file__).parent.joinpath(pathlib.Path('header_only.mrc')) + class TestBrokenMRC(unittest.TestCase): def test_read_mrc_minor_broken(self): @@ -14,6 +17,13 @@ def test_read_mrc_minor_broken(self): self.assertIn(FAILING_MRC, cm.output[0]) self.assertIn("make sure this is correct", cm.output[0]) + def test_read_mrc_too_broken(self): + # Test if this mrc raises an error as expected + with self.assertRaises(ValueError) as err: + mrc = read_mrc(FAILING_MRC) + self.assertIn(FAILING_MRC, str(err.exception) + self.assertIn("too corrupt", cm.output[0]) + def test_read_mrc_meta_data(self): # Test if this mrc can be read and if the approriate logs are printed with self.assertLogs(level='WARNING') as cm: