Repository: matchms/matchms
Pull request #161 (2020-12-04 08:17:45)

Add (some) missing tests
Adding some missing unit tests...
Idea was not to achieve 100% completeness but to (quickly) fill some gaps ;)

Thereby also found:
- minor bug in ``add_precursor_mz``: fixed
- there are still several ``filtering`` functions which do not have their own unit test --> no time to work on it now --> #162 


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21acd3ec..99078925 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Minor bug in add_precursor_mz [#161](https://github.com/matchms/matchms/pull/161)
+
 ## [0.6.2] - 2020-12-03
 
 ### Changed
diff --git a/matchms/filtering/add_precursor_mz.py b/matchms/filtering/add_precursor_mz.py
index f3930c7e..9e402764 100644
--- a/matchms/filtering/add_precursor_mz.py
+++ b/matchms/filtering/add_precursor_mz.py
@@ -15,8 +15,8 @@ def add_precursor_mz(spectrum_in: SpectrumType) -> SpectrumType:
     if isinstance(spectrum.get("precursor_mz", None), str):
         spectrum.set("precursor_mz", float(spectrum.get("precursor_mz").strip()))
     elif spectrum.get("precursor_mz", None) is None:
-        pepmass = spectrum.get("pepmass")
-        if isinstance(pepmass[0], float):
+        pepmass = spectrum.get("pepmass", None)
+        if pepmass is not None and isinstance(pepmass[0], float):
             spectrum.set("precursor_mz", pepmass[0])
         else:
             print("No precursor_mz found in metadata.")


Test patch:
diff --git a/tests/test_add_parent_mass.py b/tests/test_add_parent_mass.py
index e777fb9d..4e1f73cb 100644
--- a/tests/test_add_parent_mass.py
+++ b/tests/test_add_parent_mass.py
@@ -31,3 +31,10 @@ def test_add_parent_mass_no_pepmass():
     spectrum = add_parent_mass(spectrum_in)
 
     assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = add_parent_mass(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_add_precursor_mz.py b/tests/test_add_precursor_mz.py
new file mode 100644
index 00000000..920bd282
--- /dev/null
+++ b/tests/test_add_precursor_mz.py
@@ -0,0 +1,66 @@
+import numpy
+from matchms import Spectrum
+from matchms.filtering import add_precursor_mz
+
+
+def test_add_precursor_mz():
+    """Test if precursor_mz is correctly derived. Here nothing should change."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {"precursor_mz": 444.0}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_precursor_mz(spectrum_in)
+
+    assert spectrum.get("precursor_mz") == 444.0, "Expected different precursor_mz."
+
+
+def test_add_precursor_mz_no_masses():
+    """Test if no precursor_mz is handled correctly. Here nothing should change."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_precursor_mz(spectrum_in)
+
+    assert spectrum.get("precursor_mz") is None, "Outcome should be None."
+
+
+def test_add_precursor_mz_only_pepmass_present():
+    """Test if precursor_mz is correctly derived if only pepmass is present."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {"pepmass": (444.0, 10)}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_precursor_mz(spectrum_in)
+
+    assert spectrum.get("precursor_mz") == 444.0, "Expected different precursor_mz."
+
+
+def test_add_precursor_mz_no_precursor_mz():
+    """Test if precursor_mz is correctly derived if "precursor_mz" is str."""
+    mz = numpy.array([], dtype='float')
+    intensities = numpy.array([], dtype='float')
+    metadata = {"precursor_mz": "444.0"}
+    spectrum_in = Spectrum(mz=mz,
+                           intensities=intensities,
+                           metadata=metadata)
+
+    spectrum = add_precursor_mz(spectrum_in)
+
+    assert spectrum.get("precursor_mz") == 444.0, "Expected different precursor_mz."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = add_precursor_mz(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_derive_adduct_from_name.py b/tests/test_derive_adduct_from_name.py
index 6bbf38f2..4a2085cd 100644
--- a/tests/test_derive_adduct_from_name.py
+++ b/tests/test_derive_adduct_from_name.py
@@ -46,3 +46,10 @@ def test_derive_adduct_from_name_no_compound_name_empty_name():
 
     assert spectrum.get("adduct", None) is None, "Expected None for adduct."
     assert spectrum.get("compound_name", None) is None, "Expected None for name."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = derive_adduct_from_name(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_derive_formula_from_name.py b/tests/test_derive_formula_from_name.py
index 0e36e469..e9342274 100644
--- a/tests/test_derive_formula_from_name.py
+++ b/tests/test_derive_formula_from_name.py
@@ -46,3 +46,10 @@ def test_derive_formula_from_name_no_name_given():
 
     assert spectrum.get("formula", None) is None, "Expected None for adduct."
     assert spectrum.get("compound_name", None) is None, "Expected None for name."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = derive_formula_from_name(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_derive_inchi_from_inchikey.py b/tests/test_derive_inchi_from_inchikey.py
index b605b106..4382ba47 100644
--- a/tests/test_derive_inchi_from_inchikey.py
+++ b/tests/test_derive_inchi_from_inchikey.py
@@ -14,3 +14,10 @@ def test_derive_inchikey_from_inchi():
 
     spectrum = derive_inchikey_from_inchi(spectrum_in)
     assert spectrum.get("inchikey")[:14] == 'XDTMQSROBMDMFD', "Expected different inchikey"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = derive_inchikey_from_inchi(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_derive_inchi_from_smiles.py b/tests/test_derive_inchi_from_smiles.py
index 7fd21cb0..2bb6c0fd 100644
--- a/tests/test_derive_inchi_from_smiles.py
+++ b/tests/test_derive_inchi_from_smiles.py
@@ -15,3 +15,10 @@ def test_derive_inchi_from_smiles():
     spectrum = derive_inchi_from_smiles(spectrum_in)
     inchi = spectrum.get("inchi").replace('"', '')
     assert inchi == 'InChI=1S/C6H12/c1-2-4-6-5-3-1/h1-6H2', "Expected different InChI"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = derive_inchi_from_smiles(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_derive_smiles_from_inchi.py b/tests/test_derive_smiles_from_inchi.py
index f4f81d87..199a51e6 100644
--- a/tests/test_derive_smiles_from_inchi.py
+++ b/tests/test_derive_smiles_from_inchi.py
@@ -15,3 +15,10 @@ def test_derive_smiles_from_inchi():
 
     spectrum = derive_smiles_from_inchi(spectrum_in)
     assert spectrum.get("smiles") == "C1CCCCC1", "Expected different smiles"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = derive_smiles_from_inchi(spectrum_in)
+
+    assert spectrum is None, "Expected differnt handling of None spectrum."
diff --git a/tests/test_harmonize_undefined_inchi.py b/tests/test_harmonize_undefined_inchi.py
index fa2069da..6c5cd777 100644
--- a/tests/test_harmonize_undefined_inchi.py
+++ b/tests/test_harmonize_undefined_inchi.py
@@ -61,3 +61,10 @@ def test_harmonize_undefined_inchi_alias_nan_undefined_is_na():
 
     spectrum = harmonize_undefined_inchi(spectrum_in, aliases=["nodata", "NaN", "Nan", "nan"], undefined="n/a")
     assert spectrum.get("inchi") == "n/a"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = harmonize_undefined_inchi(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_harmonize_undefined_inchikey.py b/tests/test_harmonize_undefined_inchikey.py
index 8b6ad15e..fac8a245 100644
--- a/tests/test_harmonize_undefined_inchikey.py
+++ b/tests/test_harmonize_undefined_inchikey.py
@@ -71,3 +71,10 @@ def test_harmonize_undefined_inchikey_alias_nan_undefined_is_na():
 
     spectrum = harmonize_undefined_inchikey(spectrum_in, aliases=["nodata", "NaN", "Nan", "nan"], undefined="n/a")
     assert spectrum.get("inchikey") == "n/a"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = harmonize_undefined_inchikey(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_harmonize_undefined_smiles.py b/tests/test_harmonize_undefined_smiles.py
index 7856b957..c890775c 100644
--- a/tests/test_harmonize_undefined_smiles.py
+++ b/tests/test_harmonize_undefined_smiles.py
@@ -71,3 +71,10 @@ def test_harmonize_undefined_smiles_alias_nan_undefined_is_na():
 
     spectrum = harmonize_undefined_smiles(spectrum_in, aliases=["nodata", "NaN", "Nan", "nan"], undefined="n/a")
     assert spectrum.get("smiles") == "n/a"
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = harmonize_undefined_smiles(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_reduce_to_number_of_peaks.py b/tests/test_reduce_to_number_of_peaks.py
index 2cf00ed0..593992d3 100644
--- a/tests/test_reduce_to_number_of_peaks.py
+++ b/tests/test_reduce_to_number_of_peaks.py
@@ -89,3 +89,10 @@ def test_reduce_to_number_of_peaks_desired_5_check_sorting():
 
     assert spectrum.peaks.intensities.tolist() == [5., 4., 3., 100., 2.], "Expected different intensities."
     assert spectrum.peaks.mz.tolist() == [10., 30., 40., 50., 60.], "Expected different peaks to remain."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = reduce_to_number_of_peaks(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_repair_inchi_inchikey_smiles.py b/tests/test_repair_inchi_inchikey_smiles.py
index ada49b01..99b95980 100644
--- a/tests/test_repair_inchi_inchikey_smiles.py
+++ b/tests/test_repair_inchi_inchikey_smiles.py
@@ -132,3 +132,10 @@ def test_repair_inchi_inchikey_smiles_various_inchi_entered_as_smiles():
         assert spectrum.get("inchi") == "InChI=" + inchi.replace("InChI=", "").replace('"', "")
         assert spectrum.get("inchikey") == ""
         assert spectrum.get("smiles") == ""
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = repair_inchi_inchikey_smiles(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."
diff --git a/tests/test_require_minimum_number_of_peaks.py b/tests/test_require_minimum_number_of_peaks.py
index 5914db5a..517194b7 100644
--- a/tests/test_require_minimum_number_of_peaks.py
+++ b/tests/test_require_minimum_number_of_peaks.py
@@ -101,3 +101,10 @@ def test_require_minimum_number_of_peaks_required_5_or_10():
 
     assert spectrum is None, "Did not expect the spectrum to qualify because the number of peaks (4) is less " \
                              "than the required number (10)."
+
+
+def test_empty_spectrum():
+    spectrum_in = None
+    spectrum = require_minimum_number_of_peaks(spectrum_in)
+
+    assert spectrum is None, "Expected different handling of None spectrum."