File size: 3,577 Bytes
4f84795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abf9c5d
 
4f84795
 
3920cb1
4f84795
 
 
 
3920cb1
 
4f84795
 
3920cb1
abf9c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest
from unittest.mock import patch, MagicMock
from maps.obs_map import try_download_dataset

# tests for try_download_dataset
# - the main aim here is to mock the function load_dataset which makes external HTTP requests, 
#   and follow the successful and failing pathways. 
# - tests templates generated with copilot, they test the text/messages too; the core
#   is the return value, which should have similar form but change according to if an exception was raised or not
# since this function uses st and m_logger to keep track of the download status, we need to mock them too

@patch('maps.obs_map.load_dataset')
@patch('maps.obs_map.st')
@patch('maps.obs_map.m_logger')
def test_try_download_dataset_success(mock_logger, mock_st, mock_load_dataset):
    # Mock the return value of load_dataset
    mock_load_dataset.return_value = {'train': {'latitude': [1], 'longitude': [2], 'predicted_class': ['whale']}}

    dataset_id = "test_dataset"
    data_files = "test_file"
    result = try_download_dataset(dataset_id, data_files)

    # Assertions
    mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
    mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
    assert result == {'train': {'latitude': [1], 'longitude': [2], 'predicted_class': ['whale']}}
    mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
    mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")


@patch('maps.obs_map.load_dataset', side_effect=ValueError("Download failed"))
@patch('maps.obs_map.st')
@patch('maps.obs_map.m_logger')
def test_try_download_dataset_failure_known(mock_logger, mock_st, mock_load_dataset):
    # testing the case where we've found (can reproduce by removing network connection)
    dataset_id = "test_dataset"
    data_files = "test_file"
    result = try_download_dataset(dataset_id, data_files)

    # Assertions
    mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
    mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
    mock_logger.error.assert_called_with("Error downloading dataset: Download failed.  (after 0.00s).")
    mock_st.error.assert_called_with("Error downloading dataset: Download failed.  (after 0.00s).")
    assert result == {}
    mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
    mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")

@patch('maps.obs_map.load_dataset', side_effect=Exception("Download engine corrupt"))
@patch('maps.obs_map.st')
@patch('maps.obs_map.m_logger')
def test_try_download_dataset_failure_unknown(mock_logger, mock_st, mock_load_dataset):
    # the cases we haven't found, but should still be handled (maybe network error, etc)
    dataset_id = "test_dataset"
    data_files = "test_file"
    result = try_download_dataset(dataset_id, data_files)

    # Assertions
    mock_logger.info.assert_any_call(f"Starting to download dataset {dataset_id} from Hugging Face")
    mock_load_dataset.assert_called_once_with(dataset_id, data_files=data_files)
    mock_logger.error.assert_called_with("!!Unknown Error!! downloading dataset: Download engine corrupt.  (after 0.00s).")
    mock_st.error.assert_called_with("!!Unknown Error!! downloading dataset: Download engine corrupt.  (after 0.00s).")
    assert result == {}
    mock_logger.info.assert_called_with("Downloaded dataset: (after 0.00s). ")
    mock_st.write.assert_called_with("Downloaded dataset: (after 0.00s). ")