From 7af9a307dd035e9779987acc3222eeac0ccb15c7 Mon Sep 17 00:00:00 2001 From: Sebastien Ponce Date: Mon, 4 Dec 2023 14:47:27 +0100 Subject: [PATCH] Moved from make_data_with_FetchDataFromFile, to IOAlg --- Phys/FunTuple/tests/test_functions.py | 184 +++++++++++++++----------- 1 file changed, 107 insertions(+), 77 deletions(-) diff --git a/Phys/FunTuple/tests/test_functions.py b/Phys/FunTuple/tests/test_functions.py index 39a48374f..2d43e4acc 100644 --- a/Phys/FunTuple/tests/test_functions.py +++ b/Phys/FunTuple/tests/test_functions.py @@ -13,9 +13,10 @@ import sys import pytest from GaudiConf.LbExec import InputProcessTypes -from PyConf.application import make_data_with_FetchDataFromFile, default_raw_event +from PyConf.application import default_raw_event from PyConf.reading import upfront_decoder, reconstruction, get_pvs from PyConf.components import Algorithm +from PyConf.mock import MockDataHandle, mock_is_datahandle from PyConf import components import Functors as F @@ -34,14 +35,17 @@ def reset_global_store(algorithm_store={}): return old_algorithm_store -def data_handle(expected_type): +def data_handle(force_type): """ Make a dummy DataHandle instance to input to FunTuple_Particles or FunTuple_MCParticles. """ - return make_data_with_FetchDataFromFile( - "/Event/Spruce/MyCoolDecay/" + expected_type.replace(":", "_"), - force_type=expected_type, - ) + return MockDataHandle("/Not/Existing/Path", force_type) + + +@pytest.fixture(autouse=True) +def mock_datahandle(monkeypatch): + """Be sure we replace is_datahandle everywhere.""" + monkeypatch.setattr(components, "is_datahandle", mock_is_datahandle) # Some default arguments to FunTuple_Particles or FunTuple_MCParticles @@ -59,7 +63,9 @@ list_function_tuple_name = ( ) # make a separate list for FunTuple_Event as it expects no inputs -list_function_tuple_name_event = (("FunTuple_Event", "FunTupleEvent"),) +list_function_tuple_name_event = ( + ("FunTuple_Event", "FunTupleEvent", "SharedObjectsContainer"), +) this_module = sys.modules["FunTuple"] @@ -72,14 +78,16 @@ def getattr_wrapper(my_module, my_func_name, **kwargs): When running the options file, odin and other related objects are configured globally by DaVinci. """ - with default_raw_event.bind(raw_event_format=0.5): + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ): return getattr(my_module, my_func_name)(**kwargs) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_dummy(func_name, name_argument, expected_type): +def test_dummy(func_name, name_argument, type_argument): """ Check that a dummy tupling instance with no fields is not allowed and raises a RuntimeError. """ @@ -94,14 +102,14 @@ def test_dummy(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_simplest(func_name, name_argument, expected_type): +def test_simplest(func_name, name_argument, type_argument, monkeypatch): """ Check the simplest tupling instance where all fields are defined via the reserved name 'ALL'. """ @@ -115,14 +123,14 @@ def test_simplest(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_simple(func_name, name_argument, expected_type): +def test_simple(func_name, name_argument, type_argument): """ Check a very simple instantiation where only the mother particle is tupled with the Kinematics collection. @@ -134,7 +142,7 @@ def test_simple(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) # Trick to compare tuple.name and name_argument @@ -144,9 +152,9 @@ def test_simple(func_name, name_argument, expected_type): @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_simple_with_ALL_field(func_name, name_argument, expected_type): +def test_simple_with_ALL_field(func_name, name_argument, type_argument): """ Check with a typical and still simple tupling instance. """ @@ -162,18 +170,20 @@ def test_simple_with_ALL_field(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_with_event_variables(func_name, name_argument, expected_type): +def test_with_event_variables(func_name, name_argument, type_argument): """ Check that event variables passed as an optional argument are dealt with correctly. """ - with default_raw_event.bind(raw_event_format=0.5): + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ): event_variables = FC.EventInfo() tuple = getattr_wrapper( @@ -184,7 +194,7 @@ def test_with_event_variables(func_name, name_argument, expected_type): fields=fields, variables=variables, event_variables=event_variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) if name_argument == "FunTupleMCP": @@ -205,9 +215,9 @@ def test_with_event_variables(func_name, name_argument, expected_type): @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_with_loki_preamble(func_name, name_argument, expected_type): +def test_with_loki_preamble(func_name, name_argument, type_argument): """ Check of a next-to-simple instantiation where a LoKi preamble (string) is passed as an optional argument. @@ -221,7 +231,7 @@ def test_with_loki_preamble(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), loki_preamble=loki_preamble, ) @@ -231,9 +241,9 @@ def test_with_loki_preamble(func_name, name_argument, expected_type): @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_run_full_counter_mode(func_name, name_argument, expected_type): +def test_run_full_counter_mode(func_name, name_argument, type_argument): """ Check that the optional argument 'run_full_counter_mode' gets set correctly. """ @@ -244,7 +254,7 @@ def test_run_full_counter_mode(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), run_full_counter_mode=False, ) @@ -252,9 +262,9 @@ def test_run_full_counter_mode(func_name, name_argument, expected_type): @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_ALL_not_allowed_in_field_names(func_name, name_argument, expected_type): +def test_ALL_not_allowed_in_field_names(func_name, name_argument, type_argument): """ Check that using the reserved name 'ALL' as a key for 'fields' raises a ValueError. """ @@ -266,14 +276,14 @@ def test_ALL_not_allowed_in_field_names(func_name, name_argument, expected_type) tuple_name=name_argument, fields=dict(ALL="A -> B C"), # "ALL" is not allowed since special variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_more_than_one_caret_in_field(func_name, name_argument, expected_type): +def test_more_than_one_caret_in_field(func_name, name_argument, type_argument): """ Check that using more than one caret symbol in a field raises a ``SyntaxError``. """ @@ -285,14 +295,14 @@ def test_more_than_one_caret_in_field(func_name, name_argument, expected_type): tuple_name=name_argument, fields=dict(A="A -> B ^C ^D"), variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_fields_is_dict(func_name, name_argument, expected_type): +def test_fields_is_dict(func_name, name_argument, type_argument): """ A TypeError is raised if the 'fields' argument is not a dict. """ @@ -304,14 +314,14 @@ def test_fields_is_dict(func_name, name_argument, expected_type): tuple_name=name_argument, fields="A -> B C", # should be a dict variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_field_name_unused(func_name, name_argument, expected_type): +def test_field_name_unused(func_name, name_argument, type_argument): """ A field, 'C', is defined with no corresponding variables to tuple. The framework removes that unused field automatically. @@ -326,7 +336,7 @@ def test_field_name_unused(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) assert tuple.properties["decay_descriptors"] == ["A -> ^B C"] @@ -338,9 +348,9 @@ def test_field_name_unused(func_name, name_argument, expected_type): @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_field_name_unmatched_to_variable_keys(func_name, name_argument, expected_type): +def test_field_name_unmatched_to_variable_keys(func_name, name_argument, type_argument): """ All field names should match the particles to be tupled. In this case the field name 'NotInVariablesKey' does not correspond to any particle @@ -357,14 +367,14 @@ def test_field_name_unmatched_to_variable_keys(func_name, name_argument, expecte tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_is_dict(func_name, name_argument, expected_type): +def test_variables_is_dict(func_name, name_argument, type_argument): """ A TypeError is raised if the 'variables' argument is not a dict. """ @@ -376,14 +386,14 @@ def test_variables_is_dict(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=FC.Kinematics(), # should be a dict - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_key_ALL(func_name, name_argument, expected_type): +def test_variables_key_ALL(func_name, name_argument, type_argument): """ Check that the reserved field name 'ALL' is accepted as a key to 'variables'. """ @@ -396,14 +406,14 @@ def test_variables_key_ALL(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_all_type_FunctorCollection(func_name, name_argument, expected_type): +def test_variables_all_type_FunctorCollection(func_name, name_argument, type_argument): """ Check that the 'variables' (values) are all of type FunctorCollection. """ @@ -421,15 +431,15 @@ def test_variables_all_type_FunctorCollection(func_name, name_argument, expected tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) def test_variables_key_ALL_type_FunctorCollection( - func_name, name_argument, expected_type + func_name, name_argument, type_argument ): """ Check complementary to the above one for the reserved name 'ALL': @@ -445,14 +455,14 @@ def test_variables_key_ALL_type_FunctorCollection( tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_arrayindex(func_name, name_argument, expected_type): +def test_variables_arrayindex(func_name, name_argument, type_argument): """ Test the checking modules of FunTuple for what concerns array variables, see the module `common_util.py` and in particular the `conduct_checks_and_transform` @@ -461,9 +471,13 @@ def test_variables_arrayindex(func_name, name_argument, expected_type): """ reset_global_store() fields = dict(A="A -> B C", B="A -> ^B C", C="A -> B ^C") - with default_raw_event.bind(raw_event_format=0.5), upfront_decoder.bind( + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ), upfront_decoder.bind( source=InputProcessTypes("Spruce").sourceID() - ), reconstruction.bind(input_process="Spruce"): + ), reconstruction.bind( + input_process="Spruce" + ): v2_pvs = get_pvs() variables = { "B": FunctorCollection({"THOR_FourMom_P": F.FOURMOMENTUM}), @@ -481,14 +495,14 @@ def test_variables_arrayindex(func_name, name_argument, expected_type): tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_arrayindex_ValueError_1(func_name, name_argument, expected_type): +def test_variables_arrayindex_ValueError_1(func_name, name_argument, type_argument): """ Test the checking modules of FunTuple for what concerns array variables, see the module `common_util.py` and in particular the `conduct_checks_and_transform` @@ -497,9 +511,13 @@ def test_variables_arrayindex_ValueError_1(func_name, name_argument, expected_ty """ reset_global_store() fields = dict(A="A -> B C", B="A -> ^B C", C="A -> B ^C") - with default_raw_event.bind(raw_event_format=0.5), upfront_decoder.bind( + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ), upfront_decoder.bind( source=InputProcessTypes("Spruce").sourceID() - ), reconstruction.bind(input_process="Spruce"): + ), reconstruction.bind( + input_process="Spruce" + ): v2_pvs = get_pvs() variables = { "B": FunctorCollection({"THOR_FourMom_P": F.FOURMOMENTUM}), @@ -519,14 +537,14 @@ def test_variables_arrayindex_ValueError_1(func_name, name_argument, expected_ty tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) @pytest.mark.parametrize( - "func_name,name_argument,expected_type", list_function_tuple_name + "func_name,name_argument,type_argument", list_function_tuple_name ) -def test_variables_arrayindex_ValueError_2(func_name, name_argument, expected_type): +def test_variables_arrayindex_ValueError_2(func_name, name_argument, type_argument): """ Test the checking modules of FunTuple for what concerns array variables, see the module `common_util.py` and in particular the `conduct_checks_and_transform` @@ -535,9 +553,13 @@ def test_variables_arrayindex_ValueError_2(func_name, name_argument, expected_ty """ reset_global_store() fields = dict(A="A -> B C", B="A -> ^B C", C="A -> B ^C") - with default_raw_event.bind(raw_event_format=0.5), upfront_decoder.bind( + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ), upfront_decoder.bind( source=InputProcessTypes("Spruce").sourceID() - ), reconstruction.bind(input_process="Spruce"): + ), reconstruction.bind( + input_process="Spruce" + ): v2_pvs = get_pvs() variables = { "B": FunctorCollection({"THOR_FourMom_P": F.FOURMOMENTUM}), @@ -557,12 +579,14 @@ def test_variables_arrayindex_ValueError_2(func_name, name_argument, expected_ty tuple_name=name_argument, fields=fields, variables=variables, - inputs=data_handle(expected_type), + inputs=data_handle(type_argument), ) -@pytest.mark.parametrize("func_name,name_argument", list_function_tuple_name_event) -def test_ftevent_simplest(func_name, name_argument): +@pytest.mark.parametrize( + "func_name,name_argument,type_argument", list_function_tuple_name_event +) +def test_ftevent_simplest(func_name, name_argument, type_argument): """ Check the simplest tupling instance for FunTuple_Event. """ @@ -571,15 +595,20 @@ def test_ftevent_simplest(func_name, name_argument): func_name, name=name_argument + "_{hash}", tuple_name=name_argument, + inputs=data_handle(type_argument), ) -@pytest.mark.parametrize("func_name,name_argument", list_function_tuple_name_event) -def test_ftevent(func_name, name_argument): +@pytest.mark.parametrize( + "func_name,name_argument,type_argument", list_function_tuple_name_event +) +def test_ftevent(func_name, name_argument, type_argument): """ Check a tupling instance for FunTuple_Event with non-default arguments. """ - with default_raw_event.bind(raw_event_format=0.5): + with default_raw_event.bind( + raw_event_format=0.5, maker=lambda l, forced_type: data_handle("LHCb::RawEvent") + ): event_variables = FC.EventInfo() _ = getattr_wrapper( @@ -588,4 +617,5 @@ def test_ftevent(func_name, name_argument): name=name_argument + "_{hash}", tuple_name=name_argument, variables=event_variables, + inputs=data_handle(type_argument), ) -- GitLab