From 5491da18dda8c6f55bc2bc5d95f21b86908cd382 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 9 Jan 2024 09:50:18 +0300 Subject: Raise exception on reading non-existing file The validation checks ensure that whatever files are listed in the control file exist in the zip file bundle. It is still possible, however, that the code tries to read a file that does not exist in the file and is not listed in the control file. In those cases, raise the appropriate exception. --- r_qtl/r_qtl2.py | 35 +++++++++++++++------------- tests/r_qtl/test_files/nonexistent.zip | Bin 0 -> 380 bytes tests/r_qtl/test_r_qtl2_nonexistent_file.py | 29 +++++++++++++++++++++++ 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 tests/r_qtl/test_files/nonexistent.zip create mode 100644 tests/r_qtl/test_r_qtl2_nonexistent_file.py diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index a8958a0..d3a3805 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -189,23 +189,26 @@ def file_data(zfile: ZipFile, [str, tuple[str, ...], tuple[str, ...]], tuple[dict, ...]] = __default_process_value_transposed__) -> Iterator[dict]: """Load data from files in R/qtl2 zip bundle.""" - if isinstance(cdata[member_key], list): - for row in (line for lines in - (file_data( - zfile, member_key, {**cdata, member_key: innerfile}, - process_value, process_transposed_value) - for innerfile in cdata[member_key]) - for line in lines): - yield row - return - if not cdata.get(f"{member_key}_transposed", False): - for row in with_non_transposed(zfile, member_key, cdata, process_value): - yield row - return + try: + if isinstance(cdata[member_key], list): + for row in (line for lines in + (file_data( + zfile, member_key, {**cdata, member_key: innerfile}, + process_value, process_transposed_value) + for innerfile in cdata[member_key]) + for line in lines): + yield row + return + if not cdata.get(f"{member_key}_transposed", False): + for row in with_non_transposed(zfile, member_key, cdata, process_value): + yield row + return - for row in with_transposed( - zfile, member_key, cdata, process_transposed_value): - yield row + for row in with_transposed( + zfile, member_key, cdata, process_transposed_value): + yield row + except KeyError as exc: + raise InvalidFormat(*exc.args) from exc def cross_information(zfile: ZipFile, cdata: dict) -> Iterator[dict]: """Load cross information where present.""" diff --git a/tests/r_qtl/test_files/nonexistent.zip b/tests/r_qtl/test_files/nonexistent.zip new file mode 100644 index 0000000..4a31bf1 Binary files /dev/null and b/tests/r_qtl/test_files/nonexistent.zip differ diff --git a/tests/r_qtl/test_r_qtl2_nonexistent_file.py b/tests/r_qtl/test_r_qtl2_nonexistent_file.py new file mode 100644 index 0000000..c783a76 --- /dev/null +++ b/tests/r_qtl/test_r_qtl2_nonexistent_file.py @@ -0,0 +1,29 @@ +"""Test accessing nonexistent member.""" +from pathlib import Path + +import pytest +from zipfile import ZipFile + +from r_qtl import r_qtl2 as rqtl2 + +@pytest.mark.unit_test +@pytest.mark.parametrize( + "filepath,memberkey", + (("tests/r_qtl/test_files/nonexistent.zip", "geno"), + ("tests/r_qtl/test_files/nonexistent.zip", "founder_geno"), + ("tests/r_qtl/test_files/nonexistent.zip", "pheno"), + ("tests/r_qtl/test_files/nonexistent.zip", "covar"), + ("tests/r_qtl/test_files/nonexistent.zip", "phenocovar"), + ("tests/r_qtl/test_files/nonexistent.zip", "gmap"), + ("tests/r_qtl/test_files/nonexistent.zip", "pmap"))) +def test_loading_nonexistent_file(filepath, memberkey): + """ + GIVEN: A zipfile with a valid control file, but some files mentioned in the + control file do not exist in the zipfile + WHEN: access is made to such missing files + THEN: raise an exception + """ + with (ZipFile(Path(filepath).absolute(), "r") as zfile, + pytest.raises(rqtl2.InvalidFormat)): + cdata = rqtl2.control_data(zfile) + tuple(rqtl2.file_data(zfile, memberkey, cdata)) -- cgit v1.2.3