def test_can_restore_from(self): store = mock.Mock() model = MicroBenthosModel() with mock.patch('microbenthos.model.check_compatibility') as m: model.can_restore_from(store) m.assert_called_once_with(model.snapshot(), store)
def test_restore_from(self): model = MicroBenthosModel() entity = mock.MagicMock(DomainEntity) model.env['ent'] = entity microbe = mock.MagicMock(DomainEntity) model.microbes['microbe'] = microbe from h5py import Group store = mock.MagicMock(Group) store.__getitem__.return_value = mock.MagicMock(Group) with mock.patch.multiple( 'microbenthos.model', check_compatibility=mock.DEFAULT, truncate_model_data=mock.DEFAULT, restore_var=mock.DEFAULT, ) as mocks: check_compat = mocks['check_compatibility'] truncate = mocks['truncate_model_data'] restore_var = mocks['restore_var'] restore_var.return_value = clockval = PhysicalField(0.5, 's') truncate.return_value = 3 check_compat.side_effect = ValueError with pytest.raises(TypeError): model.restore_from(store, time_idx=-1) check_compat.assert_called_once() check_compat.reset_mock() check_compat.side_effect = None TIDX = -5 assert model.clock.value != clockval snapshot = model.snapshot() model.restore_from(store, time_idx=TIDX) check_compat.assert_called_once_with(snapshot, store) truncate.assert_called_once_with(store, time_idx=TIDX) entity.restore_from.assert_called_once_with( store['env']['ent'], -1) microbe.restore_from.assert_called_once_with( store['env']['micro'], -1) assert model.clock.value == clockval
def test_snapshot(self): model = MicroBenthosModel() e1 = mock.MagicMock(DomainEntity) e2 = mock.MagicMock(DomainEntity) model.env['e1'] = e1 model.microbes['e2'] = e2 state = model.snapshot() keys = set(('time', 'domain', 'env', 'microbes', 'equations')) assert keys == set(state) assert 'e1' in state['env'] assert 'e2' in state['microbes']