test_xr_accessor.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:xarray-simlab 作者: benbovy 项目源码 文件源码
def test_set_snapshot_vars(self, model):
        ds = xr.Dataset()
        ds['clock'] = ('clock', [0, 2, 4, 6, 8],
                       {self._clock_key: 1, self._master_clock_key: 1})
        ds['snap_clock'] = ('snap_clock', [0, 4, 8], {self._clock_key: 1})
        ds['not_a_clock'] = ('not_a_clock', [0, 1])

        with pytest.raises(KeyError) as excinfo:
            ds.xsimlab._set_snapshot_vars(model, None, invalid_process='var')
        assert "no process named" in str(excinfo.value)

        with pytest.raises(KeyError) as excinfo:
            ds.xsimlab._set_snapshot_vars(model, None, quantity='invalid_var')
        assert "has no variable" in str(excinfo.value)

        ds.xsimlab._set_snapshot_vars(model, None, grid='x')
        assert ds.attrs[self._snapshot_vars_key] == 'grid__x'

        ds.xsimlab._set_snapshot_vars(model, 'clock',
                                      some_process='some_effect',
                                      quantity='quantity')
        expected = {'some_process__some_effect', 'quantity__quantity'}
        actual = set(ds['clock'].attrs[self._snapshot_vars_key].split(','))
        assert actual == expected

        ds.xsimlab._set_snapshot_vars(model, 'snap_clock',
                                      other_process=('other_effect', 'x2'))
        expected = {'other_process__other_effect', 'other_process__x2'}
        actual = set(ds['snap_clock'].attrs[self._snapshot_vars_key].split(','))
        assert actual == expected

        with pytest.raises(ValueError) as excinfo:
            ds.xsimlab._set_snapshot_vars(model, 'not_a_clock',
                                          quantity='quantity')
        assert "not a valid clock" in str(excinfo.value)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号