From 4baa23f10d81e0df958ef42050eef79d157261eb Mon Sep 17 00:00:00 2001 From: Graeme Watt Date: Thu, 4 Apr 2024 19:36:45 +0100 Subject: [PATCH] Improve test coverage to avoid Codecov failure --- hepdata_lib/root_utils.py | 6 +++--- tests/test_helpers.py | 6 ++++++ tests/test_rootfilereader.py | 25 ++++++++++++++++++++++--- tests/test_submission.py | 1 + tests/test_table.py | 4 ++++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hepdata_lib/root_utils.py b/hepdata_lib/root_utils.py index 48e0bec4..c6d0269a 100644 --- a/hepdata_lib/root_utils.py +++ b/hepdata_lib/root_utils.py @@ -36,13 +36,13 @@ def tfile(self, tfile): raise RuntimeError( "RootFileReader: Input file is not a ROOT file (name does not end in .root)!" ) - if check_file_existence(tfile): - self._tfile = r.TFile(tfile) # pylint: disable=no-member + check_file_existence(tfile) + self._tfile = r.TFile(tfile) # pylint: disable=no-member elif isinstance(tfile, r.TFile): # pylint: disable=no-member self._tfile = tfile else: raise ValueError( - "RootReader: Encountered unkonown type of variable passed as tfile argument: " + "RootReader: Encountered unknown type of variable passed as tfile argument: " + str(type(tfile))) if not self._tfile: diff --git a/tests/test_helpers.py b/tests/test_helpers.py index d3857f71..d04e99d1 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -8,6 +8,7 @@ from hepdata_lib.helpers import get_number_precision from hepdata_lib.helpers import get_value_precision_wrt_reference from hepdata_lib.helpers import round_value_and_uncertainty +from hepdata_lib.helpers import file_is_outdated class TestHelpers(TestCase): @@ -116,3 +117,8 @@ def test_round_value_and_uncertainty(self): round_value_and_uncertainty(cont_asymm_err, "val", "unc", 2) self.assertTrue(cont_asymm_err["val"] == cont_asymm_err["val_round"]) self.assertTrue(cont_asymm_err["unc"] == cont_asymm_err["unc_round"]) + + def test_file_is_outdated(self): + '''Test behavior of file_is_outdated function''' + with self.assertRaises(RuntimeError): + file_is_outdated(None, 'non_existing_file.png') diff --git a/tests/test_rootfilereader.py b/tests/test_rootfilereader.py index 1c6209ca..4cd0c52c 100644 --- a/tests/test_rootfilereader.py +++ b/tests/test_rootfilereader.py @@ -6,7 +6,8 @@ import ctypes import numpy as np import ROOT -from hepdata_lib.root_utils import RootFileReader +from hepdata_lib.root_utils import (RootFileReader, get_graph_points, + get_hist_1d_points, get_hist_2d_points) from .test_utilities import float_compare, tuple_compare, histogram_compare_1d, make_tmp_root_file @@ -18,11 +19,11 @@ def test_tfile_setter(self): Test the behavior of the RootFileReader member setters. """ - # Check with nonexistant file that ends in .root + # Check with nonexistent file that ends in .root with self.assertRaises(RuntimeError): _reader = RootFileReader("/path/to/nowhere/butEndsIn.root") - # Check with existant file that does not end in .root + # Check with existing file that does not end in .root path_to_file = "test.txt" self.addCleanup(os.remove, path_to_file) @@ -42,9 +43,11 @@ def test_tfile_setter(self): # Finally, try a good call path_to_file = make_tmp_root_file(close=True, testcase=self) + tfile = make_tmp_root_file(testcase=self) try: _reader = RootFileReader(path_to_file) + _reader = RootFileReader(tfile) # pylint: disable=W0702 except: self.fail("RootFileReader raised an unexpected exception.") @@ -817,3 +820,19 @@ def test_retrieve_object_canvas_tpad(self): # Clean up self.doCleanups() + + def test_get_graph_points(self): + '''Check that get_graph_points with input not a TGraph (or similar) gives an exception.''' + with self.assertRaises(TypeError): + get_graph_points(100) + + def test_get_hist_1d_and_2d_points(self): + '''Check that get_hist_2d_points with wrong kwargs gives an exception.''' + testname = "test2d_sym" + xmin = 20 + xmax = 80 + # pass non-existing axis limit/parameter + with self.assertRaises(TypeError): + get_hist_1d_points(testname, zlim=(xmin, xmax)) + with self.assertRaises(TypeError): + get_hist_2d_points(testname, zlim=(xmin, xmax)) diff --git a/tests/test_submission.py b/tests/test_submission.py index 39b063bc..d750e931 100644 --- a/tests/test_submission.py +++ b/tests/test_submission.py @@ -73,6 +73,7 @@ def test_create_files(self): testdir = tmp_directory_name() test_submission = Submission() + test_submission.add_record_id(1657397, "inspire") tab = Table("test") test_submission.add_table(tab) test_submission.create_files(testdir) diff --git a/tests/test_table.py b/tests/test_table.py index 026d5251..3fb95d41 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -121,6 +121,10 @@ def test_write_images(self): expected_file = os.path.join(testdir, "minimal.png") self.assertTrue(os.path.exists(expected_file)) + test_table.image_files = {"non_existing_file.pdf"} + with self.assertRaises(RuntimeError): + test_table.write_images(testdir) + # Try wrong type of input argument bad_arguments = [None, 5, {}, []] for argument in bad_arguments: