Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add skip for missing dependency, fixture for local tests directory #933

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

import pytest


@pytest.fixture(scope="module")
def tests_directory() -> str:
return os.path.dirname(os.path.realpath(__file__))
28 changes: 15 additions & 13 deletions tests/test_lookup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,24 +371,26 @@ def test_jec_txt_effareas():
print(evaluator["photon_id_EA_Pho"])


def test_rochester():
def test_rochester(tests_directory):
rochester_data = lookup_tools.txt_converters.convert_rochester_file(
"tests/samples/RoccoR2018.txt.gz", loaduncs=True
f"{tests_directory}/samples/RoccoR2018.txt.gz", loaduncs=True
)
rochester = lookup_tools.rochester_lookup.rochester_lookup(rochester_data)

# to test 1-to-1 agreement with official Rochester requires loading C++ files
# instead, preload the correct scales in the sample directory
# the script tests/samples/rochester/build_rochester.py produces these
official_data_k = np.load("tests/samples/nano_dimuon_rochester.npy")
official_data_err = np.load("tests/samples/nano_dimuon_rochester_err.npy")
official_mc_k = np.load("tests/samples/nano_dy_rochester.npy")
official_mc_err = np.load("tests/samples/nano_dy_rochester_err.npy")
mc_rand = np.load("tests/samples/nano_dy_rochester_rand.npy")
official_data_k = np.load(f"{tests_directory}/samples/nano_dimuon_rochester.npy")
official_data_err = np.load(
f"{tests_directory}/samples/nano_dimuon_rochester_err.npy"
)
official_mc_k = np.load(f"{tests_directory}/samples/nano_dy_rochester.npy")
official_mc_err = np.load(f"{tests_directory}/samples/nano_dy_rochester_err.npy")
mc_rand = np.load(f"{tests_directory}/samples/nano_dy_rochester_rand.npy")

# test against nanoaod
events = NanoEventsFactory.from_root(
{os.path.abspath("tests/samples/nano_dimuon.root"): "Events"},
{os.path.abspath(f"{tests_directory}/samples/nano_dimuon.root"): "Events"},
).events()

data_k = rochester.kScaleDT(
Expand All @@ -404,7 +406,7 @@ def test_rochester():

# test against mc
events = NanoEventsFactory.from_root(
{os.path.abspath("tests/samples/nano_dy.root"): "Events"},
{os.path.abspath(f"{tests_directory}/samples/nano_dy.root"): "Events"},
).events()

hasgen = ~np.isnan(ak.fill_none(events.Muon.matched_gen.pt, np.nan))
Expand Down Expand Up @@ -470,13 +472,13 @@ def test_dense_lookup():
assert ak.to_list(lookup(a, a)) == [[1.0, 1.0], [1.0]]


def test_549():
def test_549(tests_directory):
import awkward as ak

from coffea.lookup_tools import extractor

ext = extractor()
f_in = "tests/samples/SFttbar_2016_ele_pt.root"
f_in = f"{tests_directory}/samples/SFttbar_2016_ele_pt.root"
ext.add_weight_sets(["ele_pt histo_eff_data %s" % f_in])
ext.finalize()
evaluator = ext.make_evaluator()
Expand All @@ -486,12 +488,12 @@ def test_549():
)


def test_554():
def test_554(tests_directory):
import uproot

from coffea.lookup_tools.root_converters import convert_histo_root_file

f_in = "tests/samples/PR554_SkipReadOnlyDirectory.root"
f_in = f"{tests_directory}/samples/PR554_SkipReadOnlyDirectory.root"
rf = uproot.open(f_in)

# check that input file contains uproot.ReadOnlyDirectory
Expand Down
28 changes: 13 additions & 15 deletions tests/test_nanoevents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

import awkward as ak
Expand Down Expand Up @@ -69,8 +68,8 @@ def crossref(events):


@pytest.mark.parametrize("suffix", suffixes)
def test_read_nanomc(suffix):
path = os.path.abspath(f"tests/samples/nano_dy.{suffix}")
def test_read_nanomc(tests_directory, suffix):
path = f"{tests_directory}/samples/nano_dy.{suffix}"
# parquet files were converted from even older nanoaod
nanoversion = NanoAODSchema
factory = getattr(NanoEventsFactory, f"from_{suffix}")(
Expand Down Expand Up @@ -130,10 +129,9 @@ def test_read_nanomc(suffix):


@pytest.mark.parametrize("suffix", suffixes)
def test_read_from_uri(suffix):
"Make sure we can properly open the file when a uri is used"

path = Path(os.path.abspath(f"tests/samples/nano_dy.{suffix}")).as_uri()
def test_read_from_uri(tests_directory, suffix):
"""Make sure we can properly open the file when a uri is used"""
path = Path(f"{tests_directory}/samples/nano_dy.{suffix}").as_uri()

nanoversion = NanoAODSchema
factory = getattr(NanoEventsFactory, f"from_{suffix}")(
Expand All @@ -147,8 +145,8 @@ def test_read_from_uri(suffix):


@pytest.mark.parametrize("suffix", suffixes)
def test_read_nanodata(suffix):
path = os.path.abspath(f"tests/samples/nano_dimuon.{suffix}")
def test_read_nanodata(tests_directory, suffix):
path = f"{tests_directory}/samples/nano_dimuon.{suffix}"
# parquet files were converted from even older nanoaod
nanoversion = NanoAODSchema
factory = getattr(NanoEventsFactory, f"from_{suffix}")(
Expand All @@ -162,17 +160,17 @@ def test_read_nanodata(suffix):
crossref(events[ak.num(events.Jet) > 2])


def test_missing_eventIds_error():
path = os.path.abspath("tests/samples/missing_luminosityBlock.root") + ":Events"
def test_missing_eventIds_error(tests_directory):
path = f"{tests_directory}/samples/missing_luminosityBlock.root:Events"
with pytest.raises(RuntimeError):
factory = NanoEventsFactory.from_root(
path, schemaclass=NanoAODSchema, delayed=False
)
factory.events()


def test_missing_eventIds_warning():
path = os.path.abspath("tests/samples/missing_luminosityBlock.root") + ":Events"
def test_missing_eventIds_warning(tests_directory):
path = f"{tests_directory}/samples/missing_luminosityBlock.root:Events"
with pytest.warns(
RuntimeWarning, match=r"Missing event_ids \: \[\'luminosityBlock\'\]"
):
Expand All @@ -183,8 +181,8 @@ def test_missing_eventIds_warning():
factory.events()


def test_missing_eventIds_warning_dask():
path = os.path.abspath("tests/samples/missing_luminosityBlock.root") + ":Events"
def test_missing_eventIds_warning_dask(tests_directory):
path = f"{tests_directory}/samples/missing_luminosityBlock.root:Events"
NanoAODSchema.error_missing_event_ids = False
with Client() as _:
events = NanoEventsFactory.from_root(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nanoevents_pfnano.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


@pytest.fixture(scope="module")
def events():
path = os.path.abspath("tests/samples/pfnano.root")
def events(tests_directory):
path = os.path.join(tests_directory, "samples/pfnano.root")
events = NanoEventsFactory.from_root(
{path: "Events"},
schemaclass=PFNanoAODSchema,
Expand Down
Loading