Commit 45f54ca0 authored by Davide Fazzini's avatar Davide Fazzini
Browse files

making the tests more robust

parent 95f62429
Pipeline #3543711 failed with stages
in 31 seconds
......@@ -12,6 +12,9 @@
import subprocess, os
import pytest
from pathlib import Path
DIR = Path(__file__).parent.resolve()
from unittest.mock import Mock, patch
input_mock = Mock()
......@@ -38,7 +41,7 @@ def test_override_job_option():
No need to actually run the job, hence the dry run.
"""
cmd = 'davinci --export test_override_job_option.opts --dry-run run-mc --override_data_options '\
'--process "Turbo" --inputfiledb Upgrade_Bd2KstarMuMu -'
' --process "Turbo" --inputfiledb Upgrade_Bd2KstarMuMu -'
result = subprocess.run(cmd, shell=True)
# Just be maniac - the command should work ;-)
......@@ -47,7 +50,6 @@ def test_override_job_option():
# Inspect the produced .opts file and check for the option value overridden
with open("test_override_job_option.opts") as f:
assert any('.process = "Turbo";' in line for line in f.readlines())
os.remove("test_override_job_option.opts")
......@@ -102,11 +104,10 @@ def test_create_options_templates():
assert result.returncode == 0
import yaml
with open(
os.path.expandvars("test_inputdb_template.yaml")) as inputdb_file:
with open("test_inputdb_template.yaml") as inputdb_file:
assert yaml.safe_load(inputdb_file)
with open(os.path.expandvars("test_jobopt_template.yaml")) as jobopt_file:
with open("test_jobopt_template.yaml") as jobopt_file:
assert yaml.safe_load(jobopt_file)
os.remove('test_inputdb_template.yaml')
......@@ -140,7 +141,7 @@ def test_create_template_interactively_changing_defaults(pytestconfig):
def test_prompt_values():
with patch('builtins.input', input_mock):
create_jobopt_template("test_jobopt_template.yaml", True)
create_jobopt_template(DIR / "test_jobopt_template.yaml", True)
capmanager = pytestconfig.pluginmanager.getplugin('capturemanager')
capmanager.suspend_global_capture(in_=True)
......@@ -148,12 +149,12 @@ def test_create_template_interactively_changing_defaults(pytestconfig):
capmanager.resume_global_capture()
config = []
with open(os.path.expandvars("test_jobopt_template.yaml")) as jobopt_file:
with open(DIR / "test_jobopt_template.yaml") as jobopt_file:
config = yaml.safe_load(jobopt_file)
assert isinstance(config, dict)
assert config["annsvc_config"] == "test_annsvc.tck.json"
os.remove('test_jobopt_template.yaml')
os.remove(DIR / 'test_jobopt_template.yaml')
input_mock.reset_mock(return_value=True, side_effect=True)
......@@ -180,12 +181,12 @@ def test_create_template_interactively_with_defaults(pytestconfig):
def test_prompt_values():
with patch('builtins.input', input_mock):
create_jobopt_template("test_jobopt_template.yaml", True)
create_jobopt_template(DIR / "test_jobopt_template.yaml", True)
capmanager = pytestconfig.pluginmanager.getplugin('capturemanager')
capmanager.suspend_global_capture(in_=True)
test_prompt_values()
capmanager.resume_global_capture()
assert not os.path.exists("test_jobopt_template.yaml")
assert not os.path.exists(DIR / "test_jobopt_template.yaml")
input_mock.reset_mock(return_value=True, side_effect=True)
......@@ -50,10 +50,8 @@ def test_ApplicationOptions_consistency_with_PyConf():
from PyConf.application import ApplicationOptions
from DaVinci import options
from DaVinci.options_default import __optsDict__ as dv_defaults
lhcb_options = ApplicationOptions.getDefaultProperties()
dv_options = options.getDefaultProperties()
for name, _ in lhcb_options.items():
# Exclude Moore-specific options since they will be moved into a
# dedicated Moore-specific ApplicationOptions class.
......
......@@ -31,8 +31,8 @@ def test_import_main_options():
options.main_options = filename
options.user_algorithms = ""
setup_algorithms(options)
os.remove(filename)
# Restore default value avoid failure in the next tests
options.main_options = ""
......@@ -59,7 +59,6 @@ def main():
options.user_algorithms = f"{filename}:main"
test_algs, _ = setup_algorithms(options)
assert "testAlg" in test_algs
os.remove(f"{filename}.py")
......@@ -164,7 +163,6 @@ def test_define_write_fsr():
options.output_level = 3
options.merge_genfsr = True
options.simulation = True
test_algs = define_fsr_writer(options)
assert any("GenFSRMerge" == x.name for x in test_algs)
......@@ -176,7 +174,6 @@ def test_add_hlt2_filter():
options.process = "Hlt2"
options.stream = "default"
test_filter = add_filter("test_filter", "HLT_PASS('Hlt2TESTLineDecision')")
assert "HDRFilter" in test_filter.fullname
......@@ -188,7 +185,6 @@ def test_add_spruce_filter():
options.stream = "default"
test_filter = add_filter("test_filter",
"HLT_PASS('SpruceTESTLineDecision')")
assert "HDRFilter" in test_filter.fullname
......@@ -198,7 +194,6 @@ def test_add_void_filter():
if 'HLT_PASS' string is not found in the filter code."
"""
test_filter = add_filter("test_filter", "VOIDTEST_Filter")
assert "VoidFilter" in test_filter.fullname
......@@ -238,9 +233,7 @@ def test_apply_filters_and_unpack():
options.evt_pre_filters = {"test_filter": "EVT_PREFILTER"}
options.enable_unpack = True
alg_dict = {"test_alg": [VoidConsumer()]}
test_alg_dict = apply_filters_and_unpacking(options, alg_dict, False)
list_of_main_expected_algs = [
"LoKi__VoidFilter", "LHCb__UnpackRawEvent", "HltPackedDataDecoder",
"UnpackMCParticle", "UnpackTrack"
......@@ -257,10 +250,8 @@ def test_configured_funtuple():
"""
from FunTuple import FunctorCollection
from FunTuple.functorcollections import Kinematics
branches = {'B0': "[B0 -> D_s- pi+]CC"}
variables = {'B0': FunctorCollection(Kinematics)}
config = {
"TestTuple": {
"location": "/Event/Spruce/SpruceTestLine/Particles",
......
......@@ -16,15 +16,20 @@ from DaVinci.configOptions import (list_data_options, set_option_value,
set_input_file_options, set_job_options,
check_options, is_option_settable)
from DaVinci import options
from pathlib import Path
DIR = Path(__file__).parent.resolve()
fileDB_file = DIR / "../../options/DaVinciDB-Example.yaml"
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst"
fileDB = "TestFileDB"
jobOpt_file = DIR / "../../options/jobOptions-Example"
def test_list_data_options_key_in_TestFileDB():
"""
Check if the list of data options in TestFileDB matches the one expected by DaVinci.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu"
fileDB_file = "TestFileDB"
assert list_data_options(fileDB_key, fileDB_file) == [
key = "Upgrade_Bd2KstarMuMu"
assert list_data_options(key, fileDB) == [
'Author', 'Format', 'DataType', 'Date', 'Simulation', 'CondDB', 'DDDB'
]
......@@ -33,9 +38,6 @@ def test_list_data_options_key_in_userDB():
"""
Check if the list of data options in a TestFileDB-like file prepared as example is updated.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
assert list_data_options(fileDB_key, fileDB_file) == [
'data_type', 'input_type', 'simulation', 'conddb_tag', 'dddb_tag'
]
......@@ -46,9 +48,8 @@ def test_list_data_options_unknown_key_in_TestFileDB():
Check if DaVinci returns an error if the key used for the TestFileDB is unknown.
"""
with pytest.raises(KeyError):
fileDB_key = "UnknownKey"
fileDB_file = "TestFileDB"
_ = list_data_options(fileDB_key, fileDB_file)
key = "UnknownKey"
_ = list_data_options(key, fileDB)
def test_handle_options():
......@@ -85,10 +86,8 @@ def test_set_input_file_from_TestFileDB():
"""
Check if DaVinci is able to set correctly input data options given a key and the TestFileDB.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu"
fileDB_file = "TestFileDB"
set_input_file_options(options, fileDB_key, fileDB_file)
key = "Upgrade_Bd2KstarMuMu"
set_input_file_options(options, key, fileDB)
assert not options.input_files == []
......@@ -97,9 +96,6 @@ def test_set_input_file_from_userDB():
Check if DaVinci is able to set correctly input data options
given a key and TestFileDB-like file database defined by the user.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
set_input_file_options(options, fileDB_key, fileDB_file)
assert not options.input_files == []
......@@ -109,10 +105,8 @@ def test_select_input_file_by_index():
Check if DaVinci selects only a specific file from the ones listed in the database
if the file index is provided after the database key.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst:3"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
set_input_file_options(options, fileDB_key, fileDB_file)
key = f"{fileDB_key}:3"
set_input_file_options(options, key, fileDB_file)
assert len(options.input_files) == 1
......@@ -121,11 +115,9 @@ def test_wrong_input_file_index():
Check that DaVinci raises an error if the file index is higher than the number of
input files available in the TestFileDB-like database.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst:100"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
key = f"{fileDB_key}:100"
with pytest.raises(ValueError):
set_input_file_options(options, fileDB_key, fileDB_file)
set_input_file_options(options, key, fileDB_file)
def test_job_options_from_py_file():
......@@ -133,9 +125,7 @@ def test_job_options_from_py_file():
Check if DaVinci sets correctly an option value when reading a .py job option file.
Check also the .py example is well formatted and readable by DaVinci.
"""
job_opt_name = "../../../Phys/DaVinci/options/jobOptions-Example.py"
set_job_options(options, job_opt_name, "", "", False)
set_job_options(options, f"{jobOpt_file}.py", "", "", False)
assert options.evt_max == 100
......@@ -144,9 +134,7 @@ def test_job_options_from_yaml_file():
Check if DaVinci sets correctly an option value when reading a .yaml job option file.
Check also the .yaml example is well formatted and readable by DaVinci.
"""
job_opt_name = "../../../Phys/DaVinci/options/jobOptions-Example.yaml"
set_job_options(options, job_opt_name, "", "", False)
set_job_options(options, f"{jobOpt_file}.yaml", "", "", False)
assert options.evt_max == 100
......@@ -159,10 +147,8 @@ def test_job_options_from_unknown_file():
with open(job_opt_name, "w") as f:
f.write("evt_max: 100")
f.close()
with pytest.raises(ValueError):
set_job_options(options, job_opt_name, "", "", False)
os.remove(job_opt_name)
......@@ -174,7 +160,6 @@ def test_lumi_fails_on_MC():
options.lumi = True
with pytest.raises(DVRuntimeError):
check_options(options)
# Restore default value avoid failure in the next tests
options.lumi = False
......@@ -187,7 +172,6 @@ def test_merge_genfsr_fails_on_data():
options.merge_genfsr = True
with pytest.raises(DVRuntimeError):
check_options(options)
# Restore default value avoid failure in the next tests
options.simulation = True
options.merge_genfsr = False
......@@ -210,7 +194,6 @@ def test_mcJob_fails_without_conddb():
options.conddb_tag = ""
with pytest.raises(DVOptionError):
check_options(options)
# Restore default value avoid failure in the next tests
options.dddb_tag = ""
......@@ -220,11 +203,8 @@ def test_set_data_option_without_override():
Check that DaVinci raises an error if the user tries to change the value of an input data option
without explicitely enabling the '--override' flag.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
override_data_options = False
data_options = list_data_options(fileDB_key, fileDB_file)
with pytest.raises(DVRuntimeError):
is_option_settable(options, "data_type", data_options,
override_data_options)
......@@ -234,10 +214,7 @@ def test_set_data_option_with_override():
"""
Check that DaVinci updates the value of an input data option if the '--override' flag is enabled.
"""
fileDB_key = "Upgrade_Bd2KstarMuMu_ldst"
fileDB_file = "../../../Phys/DaVinci/options/DaVinciDB-Example.yaml"
override_data_options = True
data_options = list_data_options(fileDB_key, fileDB_file)
assert is_option_settable(options, "data_type", data_options,
override_data_options)
......@@ -14,13 +14,15 @@ import pytest
from DaVinci.Configuration import run_davinci_app
from DaVinci.config import prepare_davinci_nodes
from pathlib import Path
DIR = Path(__file__).parent.resolve()
def test_run_app_dummy():
"""
Check if DaVinci returns a ComponentConfig object even when passing only the input data.
"""
from PyConf.application import ComponentConfig
configurables = run_davinci_app(
fileDB_key="Upgrade_Bd2KstarMuMu", fileDB_file="TestFileDB")
assert isinstance(configurables, ComponentConfig)
......@@ -33,9 +35,8 @@ def test_run_simple_job():
config = run_davinci_app(
fileDB_key="Upgrade_Bd2KstarMuMu",
fileDB_file="TestFileDB",
jobOpt_file="../../../Phys/DaVinci/options/jobOptions-Example.yaml",
jobOpt_file=DIR / "../../options/jobOptions-Example.yaml",
simple_job=True)
assert "HLTControlFlowMgr/HLTControlFlowMgr" in config
......@@ -50,7 +51,6 @@ def test_set_hltAnnSvc():
"--annsvc_config",
"root://eoslhcb.cern.ch//eos/lhcb/wg/dpa/wp3/tests/spruce_all_lines_realtime.tck.json"
])
assert "HltANNSvc/HltANNSvc" in config
......@@ -65,7 +65,6 @@ def test_lumi_counters_in_data():
"--simulation", "False", "--write_fsr", "True", "--lumi", "True"
],
override_data_options=True)
nodes = config["HLTControlFlowMgr/HLTControlFlowMgr"].CompositeCFNodes
# Node of the HLTControlFlowMgr are tuple containing : (<node_name>, <logic>, list_of_algs, <force_order>)
# E.g. : ('DaVinci', 'LAZY_AND', ['FileSummaryRecords', 'UserAnalysis'], True),
......@@ -77,7 +76,6 @@ def test_prepare_davinci_nodes():
Check that DaVinci raises an error when trying to prepare a node with a set of algorithms different from a list.
"""
from PyConf.Algorithms import Gaudi__Examples__VoidConsumer as VoidConsumer
list_of_algs = {"alg": VoidConsumer()}
with pytest.raises(ValueError):
prepare_davinci_nodes(list_of_algs)
......@@ -18,7 +18,6 @@ def test_get_allowed_values_for_known_option():
"""
opt_name = "data_type"
opt_value = "2018"
with pytest.raises(DVOptionError):
option_checker(opt_name, opt_value)
......@@ -29,7 +28,6 @@ def test_get_allowed_values_for_unknown_option():
"""
opt_name = "data"
opt_value = "2018"
with pytest.raises(DVKeyError):
option_checker(opt_name, opt_value)
......@@ -40,5 +38,4 @@ def test_get_allowed_values_for_unrestricted_option():
"""
opt_name = "evt_max"
opt_value = "200"
option_checker(opt_name, opt_value)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment