From 98a0f1daf95053c385631920d54b42e2f4f80f47 Mon Sep 17 00:00:00 2001 From: Etienne Boileau Date: Sun, 18 Feb 2024 21:59:30 +0100 Subject: [PATCH] all tests pass --- server/tests/unit/services/test_assembly.py | 1 - .../tests/unit/services/test_data_importer.py | 29 ++++- .../unit/services/test_header_importer.py | 111 ++++++++++++------ server/tests/unit/services/test_project.py | 11 +- 4 files changed, 108 insertions(+), 44 deletions(-) diff --git a/server/tests/unit/services/test_assembly.py b/server/tests/unit/services/test_assembly.py index 667d15ad..55696dbe 100644 --- a/server/tests/unit/services/test_assembly.py +++ b/server/tests/unit/services/test_assembly.py @@ -10,7 +10,6 @@ def test_init_from_id_wrong_version(Session, setup, data_path): with Session() as session, session.begin(): session.add_all(setup) - # excinfo.value contains msg with pytest.raises(AssemblyVersionError) as exc: service = AssemblyService.from_id(Session(), assembly_id=3) assert ( diff --git a/server/tests/unit/services/test_data_importer.py b/server/tests/unit/services/test_data_importer.py index af361c3c..00f631cf 100644 --- a/server/tests/unit/services/test_data_importer.py +++ b/server/tests/unit/services/test_data_importer.py @@ -84,10 +84,19 @@ def _get_data_with_header(fmt): @pytest.mark.parametrize( - "fmt", - [("minimum"), ("chrom"), ("strand"), ("maximum"), ("association")], + "fmt,msg", + [ + ("minimum", "Value start: -1 out of range."), + ( + "chrom", + "Unrecognized chrom: A. Ignore this warningfor scaffolds and contigs, otherwise this could be due to misformatting!", + ), + ("strand", "Unrecognized strand: ."), + ("maximum", "Value score: 200 out of range."), + ("association", "Unrecognized name: m5C."), + ], ) -def test_importer_parse_record_fail(fmt, Session, EUF_specs): +def test_importer_parse_record_fail(fmt, msg, Session, EUF_specs): format, version, specs = EUF_specs expected_record = _get_record(fmt) importer = EUFDataImporter( @@ -98,8 +107,10 @@ def test_importer_parse_record_fail(fmt, Session, EUF_specs): seqids=["1"], specs_ver=version, ) - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError) as exc: importer.parse_record(expected_record) + assert str(exc.value) == msg + assert exc.type == ValueError def test_importer_parse_record(Session, EUF_specs): @@ -237,8 +248,12 @@ def parse_record(record): return record importer = TestBaseImporter() - with pytest.raises(Exception) as excinfo: + with pytest.raises(Exception) as exc: importer._validate_columns() + assert ( + str(exc.value) + == "Column name chromStart doesn't match any of the ORM mapped attribute names for Data. This can be caused by a file header with wrong column names, or a change in model declaration. Aborting transaction!" + ) def test_base_importer_comment_fail(Session): @@ -257,5 +272,7 @@ def __init__(self): def parse_record(record): return record - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError) as exc: importer = TestBaseImporter() + assert str(exc.value) == "Maximum length of 1 expected, got 2 for comment." + assert exc.type == ValueError diff --git a/server/tests/unit/services/test_header_importer.py b/server/tests/unit/services/test_header_importer.py index 492b2ccb..3e95749b 100644 --- a/server/tests/unit/services/test_header_importer.py +++ b/server/tests/unit/services/test_header_importer.py @@ -91,13 +91,39 @@ def _get_header(EUF_specs, fmt=None): return StringIO(string) +@pytest.mark.parametrize( + "fmt,error,msg", + [ + ( + "version", + SpecsError, + "Unknown or outdated version bedRModv0.0.", + ), # wrong version + ("EOF", EOFError, "filen"), # empty + ], +) +def test_importer_read_version_fail(fmt, error, msg, Session, EUF_specs): + format, version, specs = EUF_specs + handle = _get_header(EUF_specs, fmt) + importer = EUFHeaderImporter( + Session(), + filen="filen", + handle=handle, + smid="ABCDEFGH", + eufid="123456789ABC", + title="Title", + ) + with pytest.raises(error) as exc: + importer._read_version() + assert str(exc.value) == msg + assert exc.type == error + + @pytest.mark.parametrize( "fmt", [ (None), # expected version ("string"), # wrong format but right version - ("version"), # wrong version - ("EOF"), # empty ], ) def test_importer_read_version(fmt, Session, EUF_specs): @@ -111,26 +137,42 @@ def test_importer_read_version(fmt, Session, EUF_specs): eufid="123456789ABC", title="Title", ) - if fmt == "version": - with pytest.raises(SpecsError) as excinfo: - importer._read_version() - elif fmt == "EOF": - with pytest.raises(EOFError) as excinfo: - importer._read_version() - else: - importer._read_version() - assert version == importer._specs_ver - assert len(specs["headers"]) == importer._num_cols - # ignore fileformat - assert specs["required"][1:] == importer._required + importer._read_version() + assert version == importer._specs_ver + assert len(specs["headers"]) == importer._num_cols + # ignore fileformat + assert specs["required"][1:] == importer._required + + +@pytest.mark.parametrize( + "fmt,msg", + [ + ("misformatted", "#organism=."), + ("missing", "#annotation_version=."), + ], +) +def test_importer_parse_lines_fail(fmt, msg, Session, EUF_specs): + handle = _get_header(EUF_specs, fmt) + importer = EUFHeaderImporter( + Session(), + filen="filen", + handle=handle, + smid="ABCDEFGH", + eufid="123456789ABC", + title="Title", + ) + importer._lino = 1 + importer._read_version() + with pytest.raises(SpecsError) as exc: + importer._parse_lines() + assert str(exc.value) == f"Missing or misformatted header: {msg}" + assert exc.type == SpecsError @pytest.mark.parametrize( "fmt", [ ("full"), - ("misformatted"), - ("missing"), ("longer"), ("disordered"), ], @@ -147,23 +189,19 @@ def test_importer_parse_lines(fmt, Session, EUF_specs): ) importer._lino = 1 importer._read_version() - if fmt in ["full", "longer", "disordered"]: - importer._parse_lines() - assert importer._lino == importer._num_cols - assert importer.taxid == 9606 - assert importer.assembly == "GRCh38" - assert importer._header["id"] == "123456789ABC" - assert importer._header["project_id"] == "ABCDEFGH" - assert importer._header["title"] == "Title" - assert importer._header["modification_type"] == "RNA" - assert importer._header["sequencing_platform"] == "Sequencing platform" - assert importer._header["basecalling"] is None - assert importer._header["bioinformatics_workflow"] == "Workflow" - assert importer._header["experiment"] == "Description of experiment." - assert importer._header["external_source"] is None - else: - with pytest.raises(SpecsError) as excinfo: - importer._parse_lines() + importer._parse_lines() + assert importer._lino == importer._num_cols + assert importer.taxid == 9606 + assert importer.assembly == "GRCh38" + assert importer._header["id"] == "123456789ABC" + assert importer._header["project_id"] == "ABCDEFGH" + assert importer._header["title"] == "Title" + assert importer._header["modification_type"] == "RNA" + assert importer._header["sequencing_platform"] == "Sequencing platform" + assert importer._header["basecalling"] is None + assert importer._header["bioinformatics_workflow"] == "Workflow" + assert importer._header["experiment"] == "Description of experiment." + assert importer._header["external_source"] is None @pytest.mark.parametrize( @@ -188,8 +226,13 @@ def test_importer_validate_columns(fmt, Session, EUF_specs): if fmt == "columns_extra": importer._validate_columns() else: - with pytest.raises(SpecsError) as excinfo: + with pytest.raises(SpecsError) as exc: importer._validate_columns() + assert ( + str(exc.value) + == "Column count (header) doesn't match required count at row 2 for bedRModv1.7." + ) + assert exc.type == SpecsError def test_importer(Session, EUF_specs): diff --git a/server/tests/unit/services/test_project.py b/server/tests/unit/services/test_project.py index 1085a0c0..8b0b2a90 100644 --- a/server/tests/unit/services/test_project.py +++ b/server/tests/unit/services/test_project.py @@ -139,8 +139,10 @@ def test_project_validate_keys_error( metadata_fmt=metadata_fmt, missing_key=missing_key, ) - with pytest.raises(KeyError): + with pytest.raises(KeyError) as exc: ProjectService(Session(), project)._validate_keys() + assert str(exc.value) == f"'Keys not found: {missing_key}.'" + assert exc.type == KeyError def test_project_add_selection(Session, setup, project_template, data_path): @@ -216,9 +218,12 @@ def test_project_validate_existing_entry( missing_key=missing_key, ) - # excinfo.value contains msg - with pytest.raises(DuplicateProjectError) as excinfo: + with pytest.raises(DuplicateProjectError) as exc: ProjectService(Session(), project)._validate_entry() + assert ( + str(exc.value) + == f"At least one similar record exists with SMID = {smid} and title = Title. Aborting transaction!" + ) def test_project_validate_entry(Session, project_template, data_path):