From 15788ab355618d29f4c8358840dcd4e7a517047c Mon Sep 17 00:00:00 2001 From: "Krzysztof (Chris) Bernat" Date: Tue, 22 Aug 2017 17:07:23 +0100 Subject: [PATCH] #218, #269, #317 do not create empty datasources, added tests --- cate/ds/local.py | 5 +++-- test/ds/test_local.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cate/ds/local.py b/cate/ds/local.py index 31ca38d4f..1b348652e 100644 --- a/cate/ds/local.py +++ b/cate/ds/local.py @@ -354,6 +354,7 @@ def make_local(self, self._make_local(local_ds, time_range, region, var_names, monitor) if local_ds.is_empty: local_store.remove_data_source(local_ds) + return None return local_ds def update_local(self, @@ -488,7 +489,7 @@ def info_string(self): return 'Files: %s' % (' '.join(self._files)) @property - def is_empty(self) -> []: + def is_empty(self) -> bool: """ Check if DataSource is empty @@ -589,7 +590,7 @@ def remove_data_source(self, data_source: Union[str, DataSource], remove_files: file_name = os.path.join(self._store_dir, data_source.id + '.json') os.remove(file_name) if remove_files: - shutil.rmtree(os.path.join(self._store_dir, data_source.id)) + shutil.rmtree(os.path.join(self._store_dir, data_source.id), ignore_errors=True) self._data_sources.remove(data_source) def create_data_source(self, data_source_id: str, region: PolygonLike.TYPE = None, diff --git a/test/ds/test_local.py b/test/ds/test_local.py index a51a5de45..0bb2b104c 100644 --- a/test/ds/test_local.py +++ b/test/ds/test_local.py @@ -281,3 +281,32 @@ def test_make_local(self): self.assertEqual(new_ds_w_region.spatial_coverage(), PolygonLike.convert("10,10,20,20")) data_set = new_ds_w_region.open_dataset() self.assertSetEqual(set(data_set.variables), {'sm', 'lat', 'lon', 'time'}) + + no_data = data_source.make_local('no_data', None, + (datetime.datetime(2020, 11, 14, 0, 0), + datetime.datetime(2020, 11, 15, 23, 59))) + self.assertIsNone(no_data) + + def test_remove_data_source_by_id(self): + + data_sources = self._local_data_store.query('local_w_temporal') + data_sources_len_before_remove = len(data_sources) + self.assertGreater(data_sources_len_before_remove, 0) + + with unittest.mock.patch.object(os, 'remove', return_value=None): + self._local_data_store.remove_data_source('local_w_temporal') + data_sources = self._local_data_store.query('local_w_temporal') + data_sources_len_after_remove = len(data_sources) + self.assertGreater(data_sources_len_before_remove, data_sources_len_after_remove) + + def test_remove_data_source(self): + + data_sources = self._local_data_store.query('local_w_temporal') + data_sources_len_before_remove = len(data_sources) + self.assertGreater(data_sources_len_before_remove, 0) + + with unittest.mock.patch.object(os, 'remove', return_value=None): + self._local_data_store.remove_data_source(data_sources[0]) + data_sources = self._local_data_store.query('local_w_temporal') + data_sources_len_after_remove = len(data_sources) + self.assertGreater(data_sources_len_before_remove, data_sources_len_after_remove)