Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
ba0dee1
1
Parent(s):
a1c0c4d
test: unit tests for the try_download_dataset wrapper
Browse files- tests/test_obs_map.py +63 -0
tests/test_obs_map.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
from maps.obs_map import try_download_dataset
|
| 4 |
+
|
| 5 |
+
# tests for try_download_dataset
|
| 6 |
+
# - the main aim here is to mock the function load_dataset which makes external HTTP requests,
|
| 7 |
+
# and follow the successful and failing pathways.
|
| 8 |
+
# - tests templates generated with copilot, they test the text/messages too; the core
|
| 9 |
+
# is the return value, which should have similar form but change according to if an exception was raised or not
|
| 10 |
+
# since this function uses st and m_logger to keep track of the download status, we need to mock them too
|
| 11 |
+
|
| 12 |
+
@patch('maps.obs_map.load_dataset')
|
| 13 |
+
@patch('maps.obs_map.st')
|
| 14 |
+
@patch('maps.obs_map.m_logger')
|
| 15 |
+
def test_try_download_dataset_success(mock_logger, mock_st, mock_load_dataset):
|
| 16 |
+
# Mock the return value of load_dataset
|
| 17 |
+
mock_load_dataset.return_value = {'train': {'latitude': [1], 'longitude': [2], 'predicted_class': ['whale']}}
|
| 18 |
+
|
| 19 |
+
dataset_id = "test_dataset"
|
| 20 |
+
data_files = "test_file"
|
| 21 |
+
result = try_download_dataset(dataset_id, data_files)
|
| 22 |
+
|
| 23 |
+
# Assertions
|
| 24 |
+
mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
|
| 25 |
+
mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
|
| 26 |
+
assert result == {'train': {'latitude': [1], 'longitude': [2], 'predicted_class': ['whale']}}
|
| 27 |
+
mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
|
| 28 |
+
mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@patch('maps.obs_map.load_dataset', side_effect=ValueError("Download failed"))
|
| 32 |
+
@patch('maps.obs_map.st')
|
| 33 |
+
@patch('maps.obs_map.m_logger')
|
| 34 |
+
def test_try_download_dataset_failure_with_mockdata(mock_logger, mock_st, mock_load_dataset):
|
| 35 |
+
dataset_id = "test_dataset"
|
| 36 |
+
data_files = "test_file"
|
| 37 |
+
result = try_download_dataset(dataset_id, data_files, mockdata_on_failure=True)
|
| 38 |
+
|
| 39 |
+
# Assertions
|
| 40 |
+
mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
|
| 41 |
+
mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
|
| 42 |
+
mock_logger.error.assert_called_with("Error downloading dataset: Download failed. (after 0.00s) Using mock data to continue")
|
| 43 |
+
mock_st.error.assert_called_with("Error downloading dataset: Download failed. (after 0.00s) Using mock data to continue")
|
| 44 |
+
assert result == {'train': {'latitude': [0], 'longitude': [0], 'predicted_class': ['rough_toothed_dolphin']}}
|
| 45 |
+
mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
|
| 46 |
+
mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")
|
| 47 |
+
|
| 48 |
+
@patch('maps.obs_map.load_dataset', side_effect=ValueError("Download failed"))
|
| 49 |
+
@patch('maps.obs_map.st')
|
| 50 |
+
@patch('maps.obs_map.m_logger')
|
| 51 |
+
def test_try_download_dataset_failure_without_mockdata(mock_logger, mock_st, mock_load_dataset):
|
| 52 |
+
dataset_id = "test_dataset"
|
| 53 |
+
data_files = "test_file"
|
| 54 |
+
result = try_download_dataset(dataset_id, data_files, mockdata_on_failure=False)
|
| 55 |
+
|
| 56 |
+
# Assertions
|
| 57 |
+
mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
|
| 58 |
+
mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
|
| 59 |
+
mock_logger.error.assert_called_with("Error downloading dataset: Download failed. (after 0.00s) Using mock data to continue")
|
| 60 |
+
mock_st.error.assert_called_with("Error downloading dataset: Download failed. (after 0.00s) Using mock data to continue")
|
| 61 |
+
assert result == {}
|
| 62 |
+
mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
|
| 63 |
+
mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")
|