Skip to content

Commit 1676167

Browse files
committed
Increase code coverage
1 parent 48635dd commit 1676167

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

sdv/datasets/demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ def _list_objects(prefix, bucket, client):
8787
dataset_name = _get_dataset_name_from_prefix(prefix)
8888
if dataset_name:
8989
raise DemoResourceNotFoundError(
90-
f'Could not download dataset {dataset_name} from bucket {bucket}. '
90+
f"Could not download dataset '{dataset_name}' from bucket '{bucket}'. "
9191
'Make sure the bucket name is correct. If the bucket is private '
9292
'make sure to provide your credentials.'
9393
)
9494
else:
9595
raise DemoResourceNotFoundError(
96-
f'Could not list datasets in modality {modality} from bucket {bucket}. '
96+
f"Could not list datasets in modality '{modality}' from bucket '{bucket}'. "
9797
'Make sure the bucket name is correct. If the bucket is private '
9898
'make sure to provide your credentials.'
9999
)

tests/unit/datasets/test_demo.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
_get_metadata,
1919
_get_text_file_content,
2020
_iter_metainfo_yaml_entries,
21+
_list_objects,
2122
download_demo,
2223
get_available_demos,
2324
get_readme,
@@ -1323,3 +1324,59 @@ def test_download_demo_credentials_raises_error():
13231324
'sdv-datasets-public',
13241325
{'username': '[email protected]', 'license_key': 'FakeKey123'},
13251326
)
1327+
1328+
1329+
def test__list_objects_returns_all_contents():
1330+
"""Test that `_list_objects` returns all object summaries across paginator pages."""
1331+
# Setup
1332+
mock_client = Mock()
1333+
paginator = mock_client.get_paginator.return_value
1334+
paginator.paginate.return_value = [
1335+
{'Contents': [{'Key': 'path/file1.txt'}]},
1336+
{'Contents': [{'Key': 'path/file2.txt'}]},
1337+
]
1338+
1339+
# Run
1340+
result = _list_objects(prefix='single_table/', bucket='mybucket', client=mock_client)
1341+
1342+
# Assert
1343+
assert result == [
1344+
{'Key': 'path/file1.txt'},
1345+
{'Key': 'path/file2.txt'},
1346+
]
1347+
mock_client.get_paginator.assert_called_once_with('list_objects_v2')
1348+
paginator.paginate.assert_called_once_with(Bucket='mybucket', Prefix='single_table/')
1349+
1350+
1351+
def test__list_objects_raises_when_no_contents_and_dataset_found():
1352+
"""Test that `_list_objects` raise a dataset-specific error when dataset name is known."""
1353+
# Setup
1354+
mock_client = Mock()
1355+
paginator = mock_client.get_paginator.return_value
1356+
paginator.paginate.return_value = [{'Contents': []}] # no objects found
1357+
1358+
# Run / Assert
1359+
error_msg = (
1360+
"Could not download dataset 'mydataset' from bucket 'bucket'. "
1361+
'Make sure the bucket name is correct. If the bucket is private '
1362+
'make sure to provide your credentials.'
1363+
)
1364+
with pytest.raises(DemoResourceNotFoundError, match=error_msg):
1365+
_list_objects(prefix='single_table/mydataset/', bucket='bucket', client=mock_client)
1366+
1367+
1368+
def test_list_objects_raises_when_no_contents_and_no_dataset():
1369+
"""Test that `_list_objects` raise a modality-specific error when dataset name is unknown."""
1370+
# Setup
1371+
mock_client = Mock()
1372+
paginator = mock_client.get_paginator.return_value
1373+
paginator.paginate.return_value = [{'Contents': []}]
1374+
1375+
# Run / Assert
1376+
error_msg = (
1377+
"Could not list datasets in modality 'single_table' from bucket 'bucket'. "
1378+
'Make sure the bucket name is correct. If the bucket is private '
1379+
'make sure to provide your credentials.'
1380+
)
1381+
with pytest.raises(DemoResourceNotFoundError, match=error_msg):
1382+
_list_objects(prefix='single_table/', bucket='bucket', client=mock_client)

0 commit comments

Comments
 (0)