def test_save_load_midrun_no_movers(images_dir, uncertain): """ create model, save it after 1step, then load and check equality of original model and persisted model Remove all movers and ensure it still works as expected """ model = make_model(images_dir, uncertain) for mover in model.movers: del model.movers[mover.id] model.step() print 'saving scnario ..' scene = Scenario(saveloc_, model) scene.save() scene.model = None # make it none - load from persistence print 'loading scenario ..' model2 = scene.load() for sc in zip(model.spills.items(), model2.spills.items()): # need to change both atol since reading persisted data sc[0]._array_allclose_atol = 1e-5 sc[1]._array_allclose_atol = 1e-5 assert model.spills == model2.spills assert model == model2
def test_exception_no_model_to_load(images_dir): ''' raises exception since the saveloc_ from where to load the model is empty. There are no Model_*.txt files that can be loaded ''' s = Scenario(os.path.join(saveloc_)) with pytest.raises(ValueError): s.load()
def test_save_load_scenario(images_dir, uncertain): model = make_model(images_dir, uncertain) print 'saving scnario ..' scene = Scenario(saveloc_, model) scene.save() scene.model = None # make it none - load from persistence print 'loading scenario ..' model2 = scene.load() assert model == model2
def test_exception_multiple_models_to_load(images_dir): ''' create a model, save it. Then copy the Model_*.json file to a new Mode_*_new.json file in the same location. During Scenario(...).load(), this should raise an exception since there should only be 1 Model_*.json in saveloc_ ''' model = make_model(images_dir) scene = Scenario(saveloc_, model) scene.save() m_file = glob(os.path.join(saveloc_, 'Model_*.json'))[0] (fname, ext) = os.path.splitext(m_file) f_new = '{0}_new'.format(fname) m_new = f_new + ext shutil.copyfile(m_file, m_new) with pytest.raises(ValueError): scene.load()
def test_load_midrun_ne_rewound_model(images_dir, uncertain): """ Load the same model that was persisted previously after 1 step This time rewind the original model and test that the two are not equal. The data arrays in the spill container must not match """ # data arrays in model.spills no longer equal model = make_model(images_dir, uncertain) model.step() print 'saving scnario ..' scene = Scenario(saveloc_, model) scene.save() model.rewind() model2 = scene.load() assert model.spills != model2.spills assert model != model2
def test_save_load_midrun_scenario(images_dir, uncertain): """ create model, save it after 1step, then load and check equality of original model and persisted model """ model = make_model(images_dir, uncertain) model.step() print 'saving scnario ..' scene = Scenario(saveloc_, model) scene.save() scene.model = None # make it none - load from persistence print 'loading scenario ..' model2 = scene.load() for sc in zip(model.spills.items(), model2.spills.items()): sc[0]._array_allclose_atol = 1e-5 # need to change both atol sc[1]._array_allclose_atol = 1e-5 assert model.spills == model2.spills assert model == model2
def test_exception_no_model_to_save(): s = Scenario(os.path.join(saveloc_)) with pytest.raises(AttributeError): s.save()