def test_func_ref(function_factory: FunctionFactoryBase): const_cls = function_factory.get('ConstFunc') f = const_cls(function_factory=function_factory, duration=4, a=.7, name='f') f2 = const_cls(function_factory=function_factory, duration=5, a=.9, name='f') function_factory.add_func(f) ref1 = function_factory.get_func_ref(name='f') ref2 = function_factory.get_func_ref(func=f2) assert ref1.func is f assert ref2.func is f2 assert f.has_ref assert f2.has_ref assert f in function_factory._ref_funcs assert f2 in function_factory._ref_funcs function_factory.return_func_ref(ref1) assert ref2.func is f2 assert not f.has_ref assert f2.has_ref assert f not in function_factory._ref_funcs assert f2 in function_factory._ref_funcs function_factory.return_func_ref(ref2) assert not f.has_ref assert not f2.has_ref assert f not in function_factory._ref_funcs assert f2 not in function_factory._ref_funcs
def test_return_not_added_func_ref(function_factory: FunctionFactoryBase): from ceed.function import CeedFuncRef const_cls = function_factory.get('ConstFunc') f = const_cls(function_factory=function_factory, duration=4, a=.7, name='f') ref = CeedFuncRef(function_factory=function_factory, func=f) with pytest.raises(ValueError): function_factory.return_func_ref(ref)
def test_clear_funcs_with_ref(function_factory: FunctionFactoryBase): const_cls = function_factory.get('ConstFunc') f = const_cls(function_factory=function_factory, duration=4, a=.7, name='f') f2 = const_cls(function_factory=function_factory, duration=5, a=.9, name='f2') function_factory.add_func(f) function_factory.add_func(f2) assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert function_factory.funcs_inst['f2'] is f2 assert f2 in function_factory.funcs_user ref = function_factory.get_func_ref(name='f') function_factory.clear_added_funcs() # f should not have been removed, but f2 was removed assert ref.func is f assert f.has_ref assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert 'f2' not in function_factory.funcs_inst assert f2 not in function_factory.funcs_user function_factory.clear_added_funcs(force=True) assert ref.func is f assert f.has_ref assert 'f' not in function_factory.funcs_inst assert f not in function_factory.funcs_user function_factory.return_func_ref(ref) assert not f.has_ref
def test_remove_func_with_ref(function_factory: FunctionFactoryBase): const_cls = function_factory.get('ConstFunc') f = const_cls(function_factory=function_factory, duration=4, a=.7, name='f') f2 = const_cls(function_factory=function_factory, duration=5, a=.9, name='f2') f3 = const_cls(function_factory=function_factory, duration=5, a=.9, name='f3') function_factory.add_func(f) function_factory.add_func(f2) function_factory.add_func(f3) assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert function_factory.funcs_inst['f2'] is f2 assert f2 in function_factory.funcs_user assert function_factory.funcs_inst['f3'] is f3 assert f3 in function_factory.funcs_user ref = function_factory.get_func_ref(name='f') ref3 = function_factory.get_func_ref(name='f3') assert not function_factory.remove_func(f) assert ref.func is f assert f.has_ref assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert function_factory.funcs_inst['f2'] is f2 assert f2 in function_factory.funcs_user assert function_factory.funcs_inst['f3'] is f3 assert f3 in function_factory.funcs_user assert function_factory.remove_func(f2) assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert 'f2' not in function_factory.funcs_inst assert f2 not in function_factory.funcs_user assert function_factory.funcs_inst['f3'] is f3 assert f3 in function_factory.funcs_user assert not function_factory.remove_func(f3) assert ref3.func is f3 assert f3.has_ref assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert function_factory.funcs_inst['f3'] is f3 assert f3 in function_factory.funcs_user assert function_factory.remove_func(f3, force=True) assert ref3.func is f3 assert f3.has_ref assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user assert 'f3' not in function_factory.funcs_inst assert f3 not in function_factory.funcs_user assert not function_factory.remove_func(f) assert ref.func is f assert f.has_ref assert function_factory.funcs_inst['f'] is f assert f in function_factory.funcs_user function_factory.return_func_ref(ref) assert not f.has_ref assert function_factory.remove_func(f) assert 'f' not in function_factory.funcs_inst assert f not in function_factory.funcs_user function_factory.return_func_ref(ref3) assert not f3.has_ref