diff --git a/src/allotropy/parsers/beckman_pharmspec/beckman_pharmspec_reader.py b/src/allotropy/parsers/beckman_pharmspec/beckman_pharmspec_reader.py index 8d2a99d42..05fd75fd2 100644 --- a/src/allotropy/parsers/beckman_pharmspec/beckman_pharmspec_reader.py +++ b/src/allotropy/parsers/beckman_pharmspec/beckman_pharmspec_reader.py @@ -1,5 +1,6 @@ import pandas as pd +from allotropy.exceptions import AllotropeConversionError from allotropy.named_file_contents import NamedFileContents from allotropy.parsers.utils.pandas import parse_header_row, read_excel, SeriesData @@ -17,8 +18,19 @@ def __init__(self, named_file_contents: NamedFileContents) -> None: by finding the index first row which contains the word 'Particle' and ending right before the index of the first row containing 'Approver'. """ - start = df[df[1].str.contains("Particle", na=False)].index.values[0] - end = df[df[0].str.contains("Approver_", na=False)].index.values[0] - 1 + # Check for rows containing "Particle" in column 1 + particle_rows = df[df[1].str.contains("Particle", na=False)].index.values + if len(particle_rows) == 0: + msg = "Unable to find required 'Particle' marker in column 1 of the data file." + raise AllotropeConversionError(msg) + start = particle_rows[0] + + # Check for rows containing "Approver_" in column 0 + approver_rows = df[df[0].str.contains("Approver_", na=False)].index.values + if len(approver_rows) == 0: + msg = "Unable to find required 'Approver_' marker in column 0 of the data file." + raise AllotropeConversionError(msg) + end = approver_rows[0] - 1 # The header data is everything up to the start of the data. # It is stored in two columns spread over the first 6 columns. diff --git a/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_approver.xlsx b/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_approver.xlsx new file mode 100644 index 000000000..b68e27427 Binary files /dev/null and b/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_approver.xlsx differ diff --git a/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_particle.xlsx b/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_particle.xlsx new file mode 100644 index 000000000..d45ee2e03 Binary files /dev/null and b/tests/parsers/beckman_pharmspec/testdata/errors/hiac_missing_particle.xlsx differ diff --git a/tests/parsers/beckman_pharmspec/to_allotrope_test.py b/tests/parsers/beckman_pharmspec/to_allotrope_test.py index 8b81a7d06..1bfdf314d 100644 --- a/tests/parsers/beckman_pharmspec/to_allotrope_test.py +++ b/tests/parsers/beckman_pharmspec/to_allotrope_test.py @@ -1,6 +1,36 @@ +from pathlib import Path + +import pytest + +from allotropy.exceptions import AllotropeConversionError from allotropy.parser_factory import Vendor +from allotropy.testing.utils import from_file from tests.to_allotrope_test import ParserTest +TESTDATA = Path(Path(__file__).parent, "testdata") + class TestParser(ParserTest): VENDOR = Vendor.BECKMAN_PHARMSPEC + + +def test_to_allotrope_missing_particle_marker() -> None: + with pytest.raises( + AllotropeConversionError, + match="Unable to find required 'Particle' marker in column 1 of the data file.", + ): + from_file( + f"{TESTDATA}/errors/hiac_missing_particle.xlsx", + TestParser.VENDOR, + ) + + +def test_to_allotrope_missing_approver_marker() -> None: + with pytest.raises( + AllotropeConversionError, + match="Unable to find required 'Approver_' marker in column 0 of the data file.", + ): + from_file( + f"{TESTDATA}/errors/hiac_missing_approver.xlsx", + TestParser.VENDOR, + )