Skip to content

Commit

Permalink
add some first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mafrahm committed Sep 13, 2023
1 parent 38afd76 commit b7a7e43
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/run_all
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,19 @@ action() {
fi

return "$ret_global"

# tests
cecho 35 "check tests ..."
bash "${this_dir}/run_tests"
ret="$?"
if [ "${ret}" != "0" ]; then
2>&1 cecho 31 "run_tests failed with exit code ${ret}"
[ "${mode}" = "force" ] || return "${ret}"
ret_global="1"
else
cecho 32 "done"
fi

echo
}
action "$@"
35 changes: 35 additions & 0 deletions tests/run_test
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 "$@"
25 changes: 25 additions & 0 deletions tests/run_tests
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 "$@"
58 changes: 58 additions & 0 deletions tests/test_util.py
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)

0 comments on commit b7a7e43

Please sign in to comment.