Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
#218, #269, #317 do not create empty datasources, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof (Chris) Bernat committed Aug 22, 2017
1 parent 53f4e56 commit 15788ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cate/ds/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions test/ds/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 15788ab

Please sign in to comment.