Skip to content

Commit

Permalink
Merge pull request #582 from opencobra/fix-model-fixture
Browse files Browse the repository at this point in the history
refactor: use a model context for every test case
  • Loading branch information
Midnighter committed Jan 22, 2019
2 parents 7191633 + 89d2662 commit 03451d5
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 166 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ History

Next Release
------------
* Use a model context for every test case.
* Fix bug which involved find_transport_reactions to ignore compartments.
* Internal change to use model context rather than copy.
* Internal changes to JSON structure.
Expand Down
7 changes: 1 addition & 6 deletions memote/suite/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,8 @@ def pytest_report_teststatus(self, report):
case["duration"] = report.duration
case["result"] = report.outcome

@pytest.fixture(scope="session")
def read_only_model(self):
"""Provide the model for the complete test session."""
return self._model

@pytest.fixture(scope="function")
def model(self):
"""Provide a pristine model for a test unit."""
"""Provide each test case with a pristine model."""
with self._model as model:
yield model
80 changes: 40 additions & 40 deletions memote/suite/tests/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@annotate(title="Presence of Metabolite Annotation", format_type="count")
def test_metabolite_annotation_presence(read_only_model):
def test_metabolite_annotation_presence(model):
"""
Expect all metabolites to have a non-empty annotation attribute.
Expand All @@ -40,16 +40,16 @@ def test_metabolite_annotation_presence(read_only_model):
"""
ann = test_metabolite_annotation_presence.annotation
ann["data"] = get_ids(annotation.find_components_without_annotation(
read_only_model, "metabolites"))
ann["metric"] = len(ann["data"]) / len(read_only_model.metabolites)
model, "metabolites"))
ann["metric"] = len(ann["data"]) / len(model.metabolites)
ann["message"] = wrapper.fill(
"""A total of {} metabolites ({:.2%}) lack any form of annotation:
{}""".format(len(ann["data"]), ann["metric"], truncate(ann["data"])))
assert len(ann["data"]) == 0, ann["message"]


@annotate(title="Presence of Reaction Annotation", format_type="count")
def test_reaction_annotation_presence(read_only_model):
def test_reaction_annotation_presence(model):
"""
Expect all reactions to have a non-empty annotation attribute.
Expand All @@ -61,16 +61,16 @@ def test_reaction_annotation_presence(read_only_model):
"""
ann = test_reaction_annotation_presence.annotation
ann["data"] = get_ids(annotation.find_components_without_annotation(
read_only_model, "reactions"))
ann["metric"] = len(ann["data"]) / len(read_only_model.reactions)
model, "reactions"))
ann["metric"] = len(ann["data"]) / len(model.reactions)
ann["message"] = wrapper.fill(
"""A total of {} reactions ({:.2%}) lack any form of annotation:
{}""".format(len(ann["data"]), ann["metric"], truncate(ann["data"])))
assert len(ann["data"]) == 0, ann["message"]


@annotate(title="Presence of Gene Annotation", format_type="count")
def test_gene_product_annotation_presence(read_only_model):
def test_gene_product_annotation_presence(model):
"""
Expect all genes to have a non-empty annotation attribute.
Expand All @@ -83,8 +83,8 @@ def test_gene_product_annotation_presence(read_only_model):
"""
ann = test_gene_product_annotation_presence.annotation
ann["data"] = get_ids(annotation.find_components_without_annotation(
read_only_model, "genes"))
ann["metric"] = len(ann["data"]) / len(read_only_model.genes)
model, "genes"))
ann["metric"] = len(ann["data"]) / len(model.genes)
ann["message"] = wrapper.fill(
"""A total of {} genes ({:.2%}) lack any form of
annotation: {}""".format(
Expand All @@ -95,7 +95,7 @@ def test_gene_product_annotation_presence(read_only_model):
@pytest.mark.parametrize("db", list(annotation.METABOLITE_ANNOTATIONS))
@annotate(title="Metabolite Annotations Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_metabolite_annotation_overview(read_only_model, db):
def test_metabolite_annotation_overview(model, db):
"""
Expect all metabolites to have annotations from common databases.
Expand All @@ -122,8 +122,8 @@ def test_metabolite_annotation_overview(read_only_model, db):
ann = test_metabolite_annotation_overview.annotation
ann["data"][db] = get_ids(
annotation.generate_component_annotation_overview(
read_only_model.metabolites, db))
ann["metric"][db] = len(ann["data"][db]) / len(read_only_model.metabolites)
model.metabolites, db))
ann["metric"][db] = len(ann["data"][db]) / len(model.metabolites)
ann["message"][db] = wrapper.fill(
"""The following {} metabolites ({:.2%}) lack annotation for {}:
{}""".format(len(ann["data"][db]), ann["metric"][db], db,
Expand All @@ -134,7 +134,7 @@ def test_metabolite_annotation_overview(read_only_model, db):
@pytest.mark.parametrize("db", list(annotation.REACTION_ANNOTATIONS))
@annotate(title="Reaction Annotations Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_reaction_annotation_overview(read_only_model, db):
def test_reaction_annotation_overview(model, db):
"""
Expect all reactions to have annotations from common databases.
Expand All @@ -161,8 +161,8 @@ def test_reaction_annotation_overview(read_only_model, db):
ann = test_reaction_annotation_overview.annotation
ann["data"][db] = get_ids(
annotation.generate_component_annotation_overview(
read_only_model.reactions, db))
ann["metric"][db] = len(ann["data"][db]) / len(read_only_model.reactions)
model.reactions, db))
ann["metric"][db] = len(ann["data"][db]) / len(model.reactions)
ann["message"][db] = wrapper.fill(
"""The following {} reactions ({:.2%}) lack annotation for {}:
{}""".format(len(ann["data"][db]), ann["metric"][db], db,
Expand All @@ -173,7 +173,7 @@ def test_reaction_annotation_overview(read_only_model, db):
@pytest.mark.parametrize("db", list(annotation.GENE_PRODUCT_ANNOTATIONS))
@annotate(title="Gene Annotations Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_gene_product_annotation_overview(read_only_model, db):
def test_gene_product_annotation_overview(model, db):
"""
Expect all genes to have annotations from common databases.
Expand All @@ -200,8 +200,8 @@ def test_gene_product_annotation_overview(read_only_model, db):
ann = test_gene_product_annotation_overview.annotation
ann["data"][db] = get_ids(
annotation.generate_component_annotation_overview(
read_only_model.genes, db))
ann["metric"][db] = len(ann["data"][db]) / len(read_only_model.genes)
model.genes, db))
ann["metric"][db] = len(ann["data"][db]) / len(model.genes)
ann["message"][db] = wrapper.fill(
"""The following {} genes ({:.2%}) lack annotation for {}:
{}""".format(len(ann["data"][db]), ann["metric"][db], db,
Expand All @@ -212,7 +212,7 @@ def test_gene_product_annotation_overview(read_only_model, db):
@pytest.mark.parametrize("db", list(annotation.METABOLITE_ANNOTATIONS))
@annotate(title="Metabolite Annotation Conformity Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_metabolite_annotation_wrong_ids(read_only_model, db):
def test_metabolite_annotation_wrong_ids(model, db):
"""
Expect all annotations of metabolites to be in the correct format.
Expand All @@ -229,17 +229,17 @@ def test_metabolite_annotation_wrong_ids(read_only_model, db):
"""
ann = test_metabolite_annotation_wrong_ids.annotation
ann["data"][db] = total = get_ids(
set(read_only_model.metabolites).difference(
set(model.metabolites).difference(
annotation.generate_component_annotation_overview(
read_only_model.metabolites, db)))
model.metabolites, db)))
ann["metric"][db] = 1.0
ann["message"][db] = wrapper.fill(
"""There are no metabolite annotations for the {} database.
""".format(db))
assert len(total) > 0, ann["message"][db]
ann["data"][db] = get_ids(
annotation.generate_component_annotation_miriam_match(
read_only_model.metabolites, "metabolites", db))
model.metabolites, "metabolites", db))
ann["metric"][db] = len(ann["data"][db]) / len(total)
ann["message"][db] = wrapper.fill(
"""A total of {} metabolite annotations ({:.2%}) do not match the
Expand All @@ -253,7 +253,7 @@ def test_metabolite_annotation_wrong_ids(read_only_model, db):
@pytest.mark.parametrize("db", annotation.REACTION_ANNOTATIONS)
@annotate(title="Reaction Annotation Conformity Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_reaction_annotation_wrong_ids(read_only_model, db):
def test_reaction_annotation_wrong_ids(model, db):
"""
Expect all annotations of reactions to be in the correct format.
Expand All @@ -270,18 +270,18 @@ def test_reaction_annotation_wrong_ids(read_only_model, db):
"""
ann = test_reaction_annotation_wrong_ids.annotation
ann["data"][db] = total = get_ids(
set(read_only_model.reactions).difference(
set(model.reactions).difference(
annotation.generate_component_annotation_overview(
read_only_model.reactions, db)))
model.reactions, db)))
ann["metric"][db] = 1.0
ann["message"][db] = wrapper.fill(
"""There are no reaction annotations for the {} database.
""".format(db))
assert len(total) > 0, ann["message"][db]
ann["data"][db] = get_ids(
annotation.generate_component_annotation_miriam_match(
read_only_model.reactions, "reactions", db))
ann["metric"][db] = len(ann["data"][db]) / len(read_only_model.reactions)
model.reactions, "reactions", db))
ann["metric"][db] = len(ann["data"][db]) / len(model.reactions)
ann["message"][db] = wrapper.fill(
"""A total of {} reaction annotations ({:.2%}) do not match the
regular expression patterns defined on identifiers.org for the {}
Expand All @@ -294,7 +294,7 @@ def test_reaction_annotation_wrong_ids(read_only_model, db):
@pytest.mark.parametrize("db", annotation.GENE_PRODUCT_ANNOTATIONS)
@annotate(title="Gene Annotation Conformity Per Database",
format_type="percent", message=dict(), data=dict(), metric=dict())
def test_gene_product_annotation_wrong_ids(read_only_model, db):
def test_gene_product_annotation_wrong_ids(model, db):
"""
Expect all annotations of genes/gene-products to be in the correct format.
Expand All @@ -311,18 +311,18 @@ def test_gene_product_annotation_wrong_ids(read_only_model, db):
"""
ann = test_gene_product_annotation_wrong_ids.annotation
ann["data"][db] = total = get_ids(
set(read_only_model.genes).difference(
set(model.genes).difference(
annotation.generate_component_annotation_overview(
read_only_model.genes, db)))
model.genes, db)))
ann["metric"][db] = 1.0
ann["message"][db] = wrapper.fill(
"""There are no gene annotations for the {} database.
""".format(db))
assert len(total) > 0, ann["message"][db]
ann["data"][db] = get_ids(
annotation.generate_component_annotation_miriam_match(
read_only_model.genes, "genes", db))
ann["metric"][db] = len(ann["data"][db]) / len(read_only_model.genes)
model.genes, "genes", db))
ann["metric"][db] = len(ann["data"][db]) / len(model.genes)
ann["message"][db] = wrapper.fill(
"""A total of {} gene annotations ({:.2%}) do not match the
regular expression patterns defined on identifiers.org for the {}
Expand All @@ -333,7 +333,7 @@ def test_gene_product_annotation_wrong_ids(read_only_model, db):


@annotate(title="Uniform Metabolite Identifier Namespace", format_type="count")
def test_metabolite_id_namespace_consistency(read_only_model):
def test_metabolite_id_namespace_consistency(model):
"""
Expect metabolite identifiers to be from the same namespace.
Expand All @@ -349,14 +349,14 @@ def test_metabolite_id_namespace_consistency(read_only_model):
"""
ann = test_metabolite_id_namespace_consistency.annotation
overview = annotation.generate_component_id_namespace_overview(
read_only_model, "metabolites")
model, "metabolites")
distribution = overview.sum()
cols = list(distribution.index)
largest = distribution[cols].idxmax()
# Assume that all identifiers match the largest namespace.
ann["data"] = list(set(get_ids(read_only_model.metabolites)).difference(
ann["data"] = list(set(get_ids(model.metabolites)).difference(
overview[overview[largest]].index.tolist()))
ann["metric"] = len(ann["data"]) / len(read_only_model.metabolites)
ann["metric"] = len(ann["data"]) / len(model.metabolites)
ann["message"] = wrapper.fill(
"""{} metabolite identifiers ({:.2%}) deviate from the largest found
namespace ({}): {}""".format(
Expand All @@ -365,7 +365,7 @@ def test_metabolite_id_namespace_consistency(read_only_model):


@annotate(title="Uniform Reaction Identifier Namespace", format_type="count")
def test_reaction_id_namespace_consistency(read_only_model):
def test_reaction_id_namespace_consistency(model):
"""
Expect reaction identifiers to be from the same namespace.
Expand All @@ -381,14 +381,14 @@ def test_reaction_id_namespace_consistency(read_only_model):
"""
ann = test_reaction_id_namespace_consistency.annotation
overview = annotation.generate_component_id_namespace_overview(
read_only_model, "reactions")
model, "reactions")
distribution = overview.sum()
cols = list(distribution.index)
largest = distribution[cols].idxmax()
# Assume that all identifiers match the largest namespace.
ann["data"] = list(set(get_ids(read_only_model.reactions)).difference(
ann["data"] = list(set(get_ids(model.reactions)).difference(
overview[overview[largest]].index.tolist()))
ann["metric"] = len(ann["data"]) / len(read_only_model.reactions)
ann["metric"] = len(ann["data"]) / len(model.reactions)
ann["message"] = wrapper.fill(
"""{} reaction identifiers ({:.2%}) deviate from the largest found
namespace ({}): {}""".format(
Expand Down

0 comments on commit 03451d5

Please sign in to comment.