Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
eboileau committed Feb 18, 2024
1 parent d38ac72 commit 98a0f1d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 44 deletions.
1 change: 0 additions & 1 deletion server/tests/unit/services/test_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
29 changes: 23 additions & 6 deletions server/tests/unit/services/test_data_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
111 changes: 77 additions & 34 deletions server/tests/unit/services/test_header_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"),
],
Expand All @@ -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(
Expand All @@ -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):
Expand Down
11 changes: 8 additions & 3 deletions server/tests/unit/services/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 98a0f1d

Please sign in to comment.