Skip to content

Commit

Permalink
Improve test coverage to avoid Codecov failure
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeWatt committed Apr 4, 2024
1 parent 3a0f1ea commit 4baa23f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions hepdata_lib/root_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
25 changes: 22 additions & 3 deletions tests/test_rootfilereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

Expand All @@ -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.")
Expand Down Expand Up @@ -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))
1 change: 1 addition & 0 deletions tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4baa23f

Please sign in to comment.