|
7 | 7 | import pytest |
8 | 8 | import requests |
9 | 9 |
|
10 | | -from openlifu.util.assets import get_kwave_paths, install_asset |
| 10 | +from openlifu.util.assets import ( |
| 11 | + download_and_install_kwave_assets, |
| 12 | + get_kwave_paths, |
| 13 | + install_asset, |
| 14 | + install_kwave_asset_from_file, |
| 15 | +) |
11 | 16 |
|
12 | 17 |
|
13 | 18 | def test_destination_already_exists(tmp_path, mocker): |
@@ -106,3 +111,51 @@ def test_get_kwave_paths(): |
106 | 111 | assert isinstance(p, Path) |
107 | 112 | assert isinstance(url, str) |
108 | 113 | assert len(url) > 0 |
| 114 | + |
| 115 | +def test_download_and_install_kwave_assets(tmp_path, mocker): |
| 116 | + """Verify kwave assets are downloaded by calling install_asset for each binary.""" |
| 117 | + fake_paths = [ |
| 118 | + (tmp_path / "bin1", "http://example.com/bin1"), |
| 119 | + (tmp_path / "bin2", "http://example.com/bin2"), |
| 120 | + ] |
| 121 | + mocker.patch("openlifu.util.assets.get_kwave_paths", return_value=fake_paths) |
| 122 | + mock_install_asset = mocker.patch("openlifu.util.assets.install_asset") |
| 123 | + |
| 124 | + download_and_install_kwave_assets() |
| 125 | + |
| 126 | + assert mock_install_asset.call_count == 2 |
| 127 | + # Check that install_asset was called for each item in our fake list |
| 128 | + mock_install_asset.assert_any_call(destination=fake_paths[0][0], url_to_asset=fake_paths[0][1]) |
| 129 | + mock_install_asset.assert_any_call(destination=fake_paths[1][0], url_to_asset=fake_paths[1][1]) |
| 130 | + |
| 131 | + |
| 132 | +def test_install_kwave_asset_from_file_succeeds(tmp_path, mocker): |
| 133 | + """Verify a kwave binary is installed from a file with a matching name.""" |
| 134 | + install_path1 = tmp_path / "kwave_binary_1" |
| 135 | + install_path2 = tmp_path / "another_binary" |
| 136 | + fake_paths = [(install_path1, "url1"), (install_path2, "url2")] |
| 137 | + mocker.patch("openlifu.util.assets.get_kwave_paths", return_value=fake_paths) |
| 138 | + mock_install_asset = mocker.patch("openlifu.util.assets.install_asset") |
| 139 | + |
| 140 | + # The source file can be in any directory, as long as its name matches. |
| 141 | + source_file = tmp_path / "some_dir" / "kwave_binary_1" |
| 142 | + |
| 143 | + result = install_kwave_asset_from_file(source_file) |
| 144 | + |
| 145 | + mock_install_asset.assert_called_once_with(destination=install_path1, path_to_asset=source_file) |
| 146 | + assert result == install_path1 |
| 147 | + |
| 148 | + |
| 149 | +def test_install_kwave_asset_from_file_fails_on_unrecognized_name(tmp_path, mocker): |
| 150 | + """Verify a ValueError is raised for a kwave binary with a non-matching name.""" |
| 151 | + fake_paths = [(tmp_path / "kwave_binary_1", "url1")] |
| 152 | + mocker.patch("openlifu.util.assets.get_kwave_paths", return_value=fake_paths) |
| 153 | + mock_install_asset = mocker.patch("openlifu.util.assets.install_asset") |
| 154 | + |
| 155 | + unrecognized_file = tmp_path / "unrecognized_file.exe" |
| 156 | + |
| 157 | + with pytest.raises(ValueError, match="was not recognized as one of the binaries"): |
| 158 | + install_kwave_asset_from_file(unrecognized_file) |
| 159 | + |
| 160 | + # Ensure we didn't accidentally try to install anything |
| 161 | + mock_install_asset.assert_not_called() |
0 commit comments