-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Script that runs a test, and optionally places the process into a sandbox. | ||
|
||
action() { | ||
local shell_is_zsh=$( [ -z "${ZSH_VERSION}" ] && echo "false" || echo "true" ) | ||
local this_file="$( ${shell_is_zsh} && echo "${(%):-%x}" || echo "${BASH_SOURCE[0]}" )" | ||
local this_dir="$( cd "$( dirname "${this_file}" )" && pwd )" | ||
local cf_dir="$( dirname "${this_dir}/../modules/columnflow" )" | ||
|
||
# get and check arguments | ||
local mod="$1" | ||
local sandbox="$2" | ||
|
||
if [ -z "${mod}" ]; then | ||
2>&1 echo "missing module to test (argument 1)" | ||
return "1" | ||
fi | ||
|
||
# run the test | ||
if [ -z "${sandbox}" ]; then | ||
echo "testing ${mod} ..." | ||
( | ||
cd "${cf_dir}" && \ | ||
python -m unittest "tests.${mod}" | ||
) | ||
else | ||
echo "testing ${mod} ..." | ||
( | ||
cd "${cf_dir}" && \ | ||
cf_sandbox "${sandbox}" "python -m unittest tests.${mod}" | ||
) | ||
fi | ||
} | ||
action "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Script that runs all unit tests. | ||
|
||
action() { | ||
local shell_is_zsh=$( [ -z "${ZSH_VERSION}" ] && echo "false" || echo "true" ) | ||
local this_file="$( ${shell_is_zsh} && echo "${(%):-%x}" || echo "${BASH_SOURCE[0]}" )" | ||
local this_dir="$( cd "$( dirname "${this_file}" )" && pwd )" | ||
local cf_dir="$( dirname "${this_dir}/../modules/columnflow" )" | ||
|
||
# dev flag for sandboxes | ||
local dev="$( [ "${CF_DEV}" = "1" ] && echo "_dev" || echo "" )" | ||
|
||
# start tests and keep track of immediate and global return values | ||
local gret="0" | ||
local ret | ||
|
||
# test_util | ||
bash "${this_dir}/run_test" test_util | ||
ret="$?" | ||
[ "${gret}" = "0" ] && gret="${ret}" | ||
|
||
return "${gret}" | ||
} | ||
action "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# coding: utf-8 | ||
|
||
""" | ||
unittests for hbw.util | ||
""" | ||
|
||
import unittest | ||
|
||
from columnflow.util import maybe_import | ||
|
||
from hbw.util import dict_diff, four_vec, call_once_on_config | ||
|
||
import order as od | ||
|
||
np = maybe_import("numpy") | ||
ak = maybe_import("awkward") | ||
|
||
|
||
class HbwUtilTest(unittest.TestCase): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.config_inst = od.Config(name="test_config", id=123456) | ||
|
||
def test_dict_diff(self): | ||
d1 = {"A": 1, "B": 2} | ||
d2 = {"A": 1, "B": 3, "C": 4} | ||
diff = dict_diff(d1, d2) | ||
|
||
self.assertEqual(diff, {("B", 2), ("B", 3), ("C", 4)}) | ||
|
||
def test_four_vec(self): | ||
self.assertEqual(four_vec("Jet"), {"Jet.pt", "Jet.eta", "Jet.phi", "Jet.mass"}) | ||
self.assertEqual(four_vec("Jet", "px", skip_defaults=True), {"Jet.px"}) | ||
self.assertEqual( | ||
four_vec(["Electron", "Muon"], ["dxy", "dz"]), | ||
{ | ||
"Electron.pt", "Electron.eta", "Electron.phi", "Electron.mass", "Electron.dxy", "Electron.dz", | ||
"Muon.pt", "Muon.eta", "Muon.phi", "Muon.mass", "Muon.dxy", "Muon.dz", | ||
}, | ||
) | ||
self.assertEqual(four_vec("MET"), {"MET.pt", "MET.phi"}) | ||
|
||
def test_call_once_on_config(self): | ||
@call_once_on_config() | ||
def some_config_function(config: od.Config) -> str: | ||
# do something with config | ||
config.add_variable(name="dummy_variable") | ||
|
||
return "test_string" | ||
|
||
# on first call, function is called -> returns "test_string" and adds identifier tag | ||
self.assertEqual(some_config_function(self.config_inst), "test_string") | ||
self.assertTrue(self.config_inst.has_tag("some_config_function_called")) | ||
|
||
# on second call, function should not be called -> returns None | ||
self.assertEqual(some_config_function(self.config_inst), None) |