Skip to content

Commit

Permalink
add a fixture for tests directory so tests can be run from anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Nov 24, 2023
1 parent 1db5c73 commit 91ee27b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 53 deletions.
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__))
32 changes: 21 additions & 11 deletions tests/test_local_executors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os.path as osp
import os
import sys

import pytest
Expand All @@ -20,16 +20,20 @@
"executor", [processor.IterativeExecutor] # , processor.FuturesExecutor
)
def test_dataframe_analysis(
executor, schema, chunksize, maxchunks, skipbadfiles, filetype
executor, schema, chunksize, maxchunks, skipbadfiles, filetype, tests_directory
):
from coffea.processor.test_items import NanoTestProcessor

if schema is not None and filetype == "parquet":
pytest.xfail("parquet nanoevents not supported yet")

filelist = {
"ZJets": {"files": [osp.abspath(f"tests/samples/nano_dy.{filetype}")]},
"Data": {"files": [osp.abspath(f"tests/samples/nano_dimuon.{filetype}")]},
"ZJets": {
"files": [os.path.join(tests_directory, f"samples/nano_dy.{filetype}")]
},
"Data": {
"files": [os.path.join(tests_directory, f"samples/nano_dimuon.{filetype}")]
},
}

executor = executor()
Expand Down Expand Up @@ -65,7 +69,9 @@ def test_dataframe_analysis(
@pytest.mark.parametrize(
"executor", [processor.IterativeExecutor] # , processor.FuturesExecutor
)
def test_nanoevents_analysis(executor, compression, maxchunks, skipbadfiles, filetype):
def test_nanoevents_analysis(
executor, compression, maxchunks, skipbadfiles, filetype, tests_directory
):
from coffea.processor.test_items import NanoEventsProcessor

if filetype == "parquet":
Expand All @@ -74,27 +80,31 @@ def test_nanoevents_analysis(executor, compression, maxchunks, skipbadfiles, fil
filelist = {
"DummyBadMissingFile": {
"treename": "Events",
"files": [osp.abspath(f"tests/samples/non_existent.{filetype}")],
"files": [
os.path.join(tests_directory, f"samples/non_existent.{filetype}")
],
},
"ZJetsBadMissingTree": {
"treename": "NotEvents",
"files": [
osp.abspath(f"tests/samples/nano_dy.{filetype}"),
osp.abspath(f"tests/samples/nano_dy_SpecialTree.{filetype}"),
os.path.join(tests_directory, f"samples/nano_dy.{filetype}"),
os.path.join(
tests_directory, f"samples/nano_dy_SpecialTree.{filetype}"
),
],
},
"ZJetsBadMissingTreeAllFiles": {
"treename": "NotEvents",
"files": [osp.abspath(f"tests/samples/nano_dy.{filetype}")],
"files": [os.path.join(tests_directory, f"samples/nano_dy.{filetype}")],
},
"ZJets": {
"treename": "Events",
"files": [osp.abspath(f"tests/samples/nano_dy.{filetype}")],
"files": [os.path.join(tests_directory, f"samples/nano_dy.{filetype}")],
"metadata": {"checkusermeta": True, "someusermeta": "hello"},
},
"Data": {
"treename": "Events",
"files": [osp.abspath(f"tests/samples/nano_dimuon.{filetype}")],
"files": [os.path.join(tests_directory, f"samples/nano_dimuon.{filetype}")],
"metadata": {"checkusermeta": True, "someusermeta2": "world"},
},
}
Expand Down
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 @@ -68,8 +67,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 @@ -129,10 +128,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 @@ -146,8 +144,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 @@ -161,17 +159,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 @@ -182,8 +180,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
events = NanoEventsFactory.from_root(
path,
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
12 changes: 5 additions & 7 deletions tests/test_parsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ def do_parsl_job(filelist, flatten=False, compression=0, config=None):


# @pytest.mark.skipif(sys.platform.startswith('darwin'), reason='parsl htex not working on osx again')
def test_parsl_htex_executor():
def test_parsl_htex_executor(tests_directory):
pytest.importorskip("parsl", minversion="0.7.2")
import os
import os.path as osp

import parsl
from parsl.channels import LocalChannel
Expand Down Expand Up @@ -66,8 +64,8 @@ def test_parsl_htex_executor():
parsl.load(parsl_config)

filelist = {
"ZJets": [osp.join(os.getcwd(), "tests/samples/nano_dy.root")],
"Data": [osp.join(os.getcwd(), "tests/samples/nano_dimuon.root")],
"ZJets": [f"{tests_directory}/samples/nano_dy.root"],
"Data": [f"{tests_directory}/samples/nano_dimuon.root"],
}

do_parsl_job(filelist)
Expand All @@ -76,11 +74,11 @@ def test_parsl_htex_executor():
filelist = {
"ZJets": {
"treename": "Events",
"files": [osp.join(os.getcwd(), "tests/samples/nano_dy.root")],
"files": [f"{tests_directory}/samples/nano_dy.root"],
},
"Data": {
"treename": "Events",
"files": [osp.join(os.getcwd(), "tests/samples/nano_dimuon.root")],
"files": [f"{tests_directory}/samples/nano_dimuon.root"],
},
}

Expand Down
10 changes: 5 additions & 5 deletions tests/test_spark.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest


Expand All @@ -11,10 +13,8 @@ def test_spark_imports():


@pytest.mark.skip(reason="pyspark executor work currently in progress")
def test_spark_executor():
def test_spark_executor(tests_directory):
pyspark = pytest.importorskip("pyspark", minversion="3.3.0")
import os
import os.path as osp

import pyspark.sql
from pyarrow.util import guid
Expand All @@ -39,12 +39,12 @@ def test_spark_executor():

filelist = {
"ZJets": {
"files": ["file:" + osp.join(os.getcwd(), "tests/samples/nano_dy.root")],
"files": ["file:" + os.path.join(tests_directory, "samples/nano_dy.root")],
"treename": "Events",
},
"Data": {
"files": [
"file:" + osp.join(os.getcwd(), "tests/samples/nano_dimuon.root")
"file:" + os.path.join(tests_directory, "samples/nano_dimuon.root")
],
"treename": "Events",
},
Expand Down

0 comments on commit 91ee27b

Please sign in to comment.