Skip to content

Commit

Permalink
Added tests and fixed incorrect exception type
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeByDrescher committed Oct 15, 2024
1 parent 086c05b commit 7d0e85c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
4 changes: 3 additions & 1 deletion biosimulators_utils/sedml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ def apply_changes_to_xml_model(model, model_etree, sed_doc=None, working_dir=Non
attribute.set("size", change.new_value)
elif type_suffix == "parameter" and attribute.get("value") is not None:
attribute.set("value", change.new_value)
elif (type_suffix == "reaction" and second_type_suffix == "parameter"
elif (type_suffix == "reaction" and
(second_type_suffix == "parameter" # SBML L1, L2
or second_type_suffix == "localParameter") # SBML L3+
and attribute.get("value") is not None): # Reaction parameter change
attribute.set("value", change.new_value)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,27 @@
<parameter constant="false" id="parameter_1" name="Fe2GutQUant" value="1E-7" units="second">
</parameter>
</listOfParameters>
<listOfReactions>
<reaction metaid="COPASI127" id="lumen" name="01. StomachLumen -&gt; IntestineLumen" reversible="false" fast="false">
<listOfReactants>
<speciesReference species="Trim" stoichiometry="1" constant="true"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="Clb" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> k1 </ci>
<ci> Trim </ci>
</apply>
</math>
<listOfParameters>
<parameter id="k1" name="k1" value="350"/>
</listOfParameters>
</kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>
44 changes: 44 additions & 0 deletions tests/fixtures/sbml-list-of-species-lvl-3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1" level="3" version="1">
<model id="Ciliberto2003_Morphogenesis" name="Ciliberto2003_Morphogenesis_Checkpoint">
<listOfCompartments>
<compartment id="compartment" size="1"/>
</listOfCompartments>
<listOfSpecies>
<species compartment="compartment" id="Trim" initialConcentration="0.1" name="CDC28_Clb2_Sic1_Complex" sboTerm="SBO:0000296">
</species>
<species compartment="compartment" id="Clb" initialConcentration="0.1" name="Clb2" sboTerm="SBO:0000245">
</species>
<species compartment="compartment" id="Sic" initialConcentration="0.1" name="Sic1" sboTerm="SBO:0000245">
</species>
<species compartment="compartment" id="SpeciesToReplace" initialConcentration="0.0035491784" name="Sic1" sboTerm="SBO:0000245">
</species>
</listOfSpecies>
<listOfParameters>
<parameter constant="false" id="parameter_1" name="Fe2GutQUant" value="1E-7" units="second">
</parameter>
</listOfParameters>
<listOfReactions>
<reaction metaid="COPASI127" id="lumen" name="01. StomachLumen -&gt; IntestineLumen" reversible="false" fast="false">
<listOfReactants>
<speciesReference species="Trim" stoichiometry="1" constant="true"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="Clb" stoichiometry="1" constant="true"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci> k1 </ci>
<ci> Trim </ci>
</apply>
</math>
<listOfLocalParameters>
<localParameter id="k1" name="k1" value="350"/>
</listOfLocalParameters>
</kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>
2 changes: 1 addition & 1 deletion tests/model_lang/sbml/test_sbml_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ValidationTestCase(unittest.TestCase):
FIXTURE_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'fixtures')

def test(self):
errors, warnings, _ = validate_model(os.path.join(self.FIXTURE_DIR, 'sbml-list-of-species.xml'))
errors, warnings, _ = validate_model(os.path.join(self.FIXTURE_DIR, 'sbml-list-of-species-lvl-2.xml'))
self.assertEqual(errors, [])
self.assertEqual(warnings, [])

Expand Down
20 changes: 18 additions & 2 deletions tests/sedml/test_sedml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def test_get_variables_for_task(self):


class ApplyModelChangesTestCase(unittest.TestCase):
FIXTURE_FILENAME = os.path.join(os.path.dirname(__file__), '../fixtures/sbml-list-of-species.xml')
FIXTURE_FILENAME = os.path.join(os.path.dirname(__file__), '../fixtures/sbml-list-of-species-lvl-2.xml')
FIXTURE_FILENAME_2 = os.path.join(os.path.dirname(__file__), '../fixtures/sbml-list-of-species-lvl-3.xml')

def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
Expand Down Expand Up @@ -668,7 +669,7 @@ def test_errors(self):
target="/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:spesies[@id='Trim']/initialConcentration",
target_namespaces={'sbml': 'http://www.sbml.org/sbml/level2/version4'},
new_value='1.9')
self._attempt_change(change, self.FIXTURE_FILENAME, NotImplementedError)
self._attempt_change(change, self.FIXTURE_FILENAME, ValueError)

change = data_model.ModelAttributeChange(
target="/sbml:sbml/sbml:model/sbml:listOfSpecies/sbml:spesies[@id='Trim']",
Expand Down Expand Up @@ -746,6 +747,21 @@ def test_errors(self):
new_value='1.9')
self._attempt_change(change, self.FIXTURE_FILENAME)

change = data_model.ModelAttributeChange(
target="/sbml:sbml/sbml:model/sbml:listOfReactions/sbml:reaction[@id='lumen']/sbml:kineticLaw"
"/sbml:listOfParameters/sbml:parameter[@id='k1']",
target_namespaces={'sbml': 'http://www.sbml.org/sbml/level2/version4'},
new_value='1.9')
self._attempt_change(change, self.FIXTURE_FILENAME)

change = data_model.ModelAttributeChange(
target="/sbml:sbml/sbml:model/sbml:listOfReactions/sbml:reaction[@id='lumen']/sbml:kineticLaw"
"/sbml:listOfLocalParameters/sbml:localParameter[@id='k1']/@value",
target_namespaces={'sbml': 'http://www.sbml.org/sbml/level3/version1'},
new_value='1.9')
self._attempt_change(change, self.FIXTURE_FILENAME_2)


def _attempt_change(self, change: data_model.ModelChange, sbml_path: str,
expected_exception: "type[BaseException] | tuple[type[BaseException], ...]" = None):
et = etree.parse(sbml_path)
Expand Down
4 changes: 2 additions & 2 deletions tests/sedml/test_sedml_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ def _validate_task(self, task, variables):
return (errors, warnings)

def test_validate_model_source_with_abs_path(self):
filename = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'sbml-list-of-species.xml'))
filename = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'sbml-list-of-species-lvl-2.xml'))
errors, warnings = validation.validate_model_source(data_model.Model(
source=filename, language=data_model.ModelLanguage.SBML), [], None, validate_models_with_languages=False)
self.assertEqual(errors, [])
Expand All @@ -851,7 +851,7 @@ def test_validate_model_source_with_url(self):

def test_validate_model_with_language(self):
# SBML
filename = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'sbml-list-of-species.xml')
filename = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'sbml-list-of-species-lvl-2.xml')
validation.validate_model_with_language(filename, data_model.ModelLanguage.SBML)

errors, warnings, _ = validation.validate_model_with_language(filename, '--not-supported--')
Expand Down

0 comments on commit 7d0e85c

Please sign in to comment.