"""Test pickling of the various classes in the package."""importimportlibimportpickleimportnumpyasnpimportpytestfromeulerpi.core.modelsimportBaseModelfromtests.test_examplesimportExamples,get_example_name
[docs]@pytest.mark.parametrize("example",Examples(),ids=get_example_name)deftest_pickling_model(example):try:module_location,className,_=exampleexceptValueError:module_location,className=exampledataFileName=None# Import class dynamically to avoid error on imports at the top which cant be tracked back to a specific testmodule=importlib.import_module(module_location)ModelClass=getattr(module,className)model:BaseModel=ModelClass()# Test picklingdumped=pickle.dumps(model)loaded=pickle.loads(dumped)forkeyinmodel.__dict__.keys():ifkey=="amici_model"orkey=="amici_solver":# Can't compare the models directly, because I dont know how to compare the SwigPyObject objects# Iterate over dict and compare each value, besides the SwigPyObject amici_model and amici_solvercontinueifisinstance(model.__dict__[key],np.ndarray):# catch numpy arrays, they dont implement __eq__ like normal python objectsassertnp.array_equal(model.__dict__[key],loaded.__dict__[key])else:assertmodel.__dict__[key]==loaded.__dict__[key]