Skip to content

Commit

Permalink
Added and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Mar 3, 2022
1 parent f0f3e69 commit 1965b70
Show file tree
Hide file tree
Showing 10 changed files with 2,549 additions and 36 deletions.
45 changes: 24 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import json
import yaml

from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument
from pyenzyme.enzymeml.core.measurement import Measurement
Expand All @@ -17,16 +19,12 @@ def enzmldoc():

@pytest.fixture
def reaction():
return EnzymeReaction.fromJSON(
open("./tests/fixtures/reaction_object.json").read()
)
return EnzymeReaction.fromJSON(open("./tests/fixtures/reaction_object.json").read())


@pytest.fixture
def measurement():
return Measurement.fromJSON(
open("./tests/fixtures/measurement_object.json").read()
)
return Measurement.fromJSON(open("./tests/fixtures/measurement_object.json").read())


@pytest.fixture
Expand All @@ -38,26 +36,31 @@ def measurement_data():

@pytest.fixture
def replicate():
return Replicate.fromJSON(
open("./tests/fixtures/replicate_object.json").read()
)
return Replicate.fromJSON(open("./tests/fixtures/replicate_object.json").read())


@pytest.fixture
def correct_model():
parameters = [
KineticParameter(name="x", value=10.0, unit="mmole / l")
]
return KineticModel(
name="SomeModel", equation="s0 * x", parameters=parameters
)
parameters = [KineticParameter(name="x", value=10.0, unit="mmole / l")]
return KineticModel(name="SomeModel", equation="s0 * x", parameters=parameters)


@pytest.fixture
def faulty_model():
parameters = [
KineticParameter(name="x", value=10.0, unit="mmole / l")
]
return KineticModel(
name="SomeModel", equation="s10 * x", parameters=parameters
)
parameters = [KineticParameter(name="x", value=10.0, unit="mmole / l")]
return KineticModel(name="SomeModel", equation="s10 * x", parameters=parameters)


@pytest.fixture
def always_pass():
return yaml.safe_load(open("./tests/fixtures/validator_always_passes.yaml"))


@pytest.fixture
def should_fail():
return yaml.safe_load(open("./tests/fixtures/validator_should_fail.yaml"))


@pytest.fixture
def template_example():
return json.loads(open("tests/fixtures/template_result.json").read())
36 changes: 21 additions & 15 deletions tests/enzymeml/core/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@


class TestMeasurement:

def test_content(self):
"""Tests consistency of content"""

measurement = Measurement(
name="SomeMeasurement", temperature=100.0, temperature_unit="C", ph=7.0,
global_time=[1, 2, 3, 4], global_time_unit="s", id="m0", uri="URI", creator_id="a0"
name="SomeMeasurement",
temperature=100.0,
temperature_unit="C",
ph=7.0,
global_time=[1, 2, 3, 4],
global_time_unit="s",
id="m0",
uri="URI",
creator_id="a0",
)

assert measurement.name == "SomeMeasurement"
Expand All @@ -29,9 +35,7 @@ def test_content(self):
def test_defaults(self):
"""Test default values"""

measurement = Measurement(
name="SomeMeasurement"
)
measurement = Measurement(name="SomeMeasurement")

assert not measurement.temperature
assert not measurement.temperature_unit
Expand Down Expand Up @@ -62,12 +66,12 @@ def test_data_export(self, measurement):

# Construct expected DataFrame
expected_protein = {
'p0': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
'time': {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
"p0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
}
expected_reactant = {
's0': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
'time': {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
"s0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
}

assert data["reactants"]["data"].to_dict() == expected_reactant
Expand Down Expand Up @@ -99,8 +103,8 @@ def test_data_export_specific_species(self, measurement):
# Test case with list as argument
data = measurement.exportData(species_ids=["s0"])
expected_reactant = {
's0': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
'time': {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0}
"s0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
}

assert data["reactants"]["initConc"] == {"s0": (10.0, "mmole / l")}
Expand All @@ -113,8 +117,8 @@ def test_data_export_specific_species(self, measurement):
data = measurement.exportData(species_ids="s0")
init_conc = data["reactants"]["initConc"]
expected_reactant = {
's0': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
'time': {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0}
"s0": {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
"time": {0: 1.0, 1: 2.0, 2: 3.0, 3: 4.0},
}

assert data["reactants"]["initConc"] == {"s0": (10.0, "mmole / l")}
Expand Down Expand Up @@ -143,7 +147,9 @@ def test_add_replicates(self, measurement, replicate, enzmldoc):
measurement.addReplicates([replicate, replicate2], enzmldoc)

assert measurement.species_dict["reactants"]["s0"].replicates == [
replicate, replicate2]
replicate,
replicate2,
]

def test_add_data(self, measurement, replicate):
"""Tests the addition of data"""
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions tests/enzymeml/tools/test_templatereader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import json

from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument


class TestTemplateReader:
def test_template_conversion(self, template_example):
"""Tests the conversion of the EnzymeML Spreadsheet template"""

# Convert the template fixture
enzmldoc = EnzymeMLDocument.fromTemplate(
"./tests/fixtures/EnzymeML_Template_Example.xlsm"
)

assert json.loads(enzmldoc.json()) == template_example
57 changes: 57 additions & 0 deletions tests/enzymeml/tools/test_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import yaml
import pandas as pd

from pyenzyme.enzymeml.tools.validator import EnzymeMLValidator
from pyenzyme.enzymeml.core.enzymemldocument import EnzymeMLDocument


class TestValidation:
def test_positive_validation(self, enzmldoc, always_pass):
"""Tests positive validation with a non-restrictive validation YAML"""

# Set up validator
validator = EnzymeMLValidator(scheme=always_pass)

# Perform validation
report, check = validator.validate(enzmldoc)

assert check is True
assert not report, "Report is not empty"

def test_negative_validation(self, enzmldoc, should_fail):
"""Tests negative validation with a restrictive validation YAML"""

# Set up validator
validator = EnzymeMLValidator(scheme=should_fail)

# Perform validation and assertit failed
report, check = validator.validate(enzmldoc)

assert check is False
assert report, "Report is empty"

def test_creation_to_conversion(self):
"""Tests the complete path from EnzymeMLDocument class over Spreadhseet back to dict structure"""

# Generate sheet
sheet_path = "./tests/fixtures/"
sheet_loc = os.path.join(sheet_path, "EnzymeML_Validation_Template.xlsx")
EnzymeMLValidator.generateValidationSpreadsheet(path=sheet_path)

assert pd.read_excel(sheet_loc).to_dict()

# Manually create collection and compare it
# to the one that will be drawn from the sheet
collection = EnzymeMLValidator(scheme={})._get_cls_annotations(
EnzymeMLDocument, level="document"
)[0]
expected = yaml.safe_load(
EnzymeMLValidator._dump_validation_template_yaml(collection)
)
converted = yaml.safe_load(EnzymeMLValidator.convertSheetToYAML(sheet_loc))

assert converted == expected, "Conversion from spreadsheet is inconsistent"

# Remove everything
os.remove(sheet_loc)
Binary file added tests/fixtures/EnzymeML_Template_Example.xlsm
Binary file not shown.

0 comments on commit 1965b70

Please sign in to comment.