Skip to content

Commit c1a914d

Browse files
test: Improve example_download tests
1 parent e1d0ffb commit c1a914d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,5 @@ cython_debug/
176176
._build
177177

178178
doc/source/api
179-
.cov
179+
.cov
180+
not_a_dir

src/ansys/tools/common/example_download.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ def download_file(
9999
destination_path = Path(destination) if destination is not None else None
100100

101101
# If destination is not a dir, create it
102-
if destination_path is not None and not destination_path.is_dir():
102+
if destination_path is not None and not destination_path.exists():
103103
destination_path.mkdir(parents=True, exist_ok=True)
104104

105-
# Check if it was able to create the dir
105+
# Check if it was able to create the dir, very rare case
106106
if destination_path is not None and not destination_path.is_dir():
107-
raise ValueError("Destination directory provided does not exist")
107+
raise ValueError("Destination directory provided does not exist") # pragma: no cover
108108

109109
url = self._get_filepath_on_default_server(filename, directory)
110110
local_path = self._retrieve_data(url, filename, dest=destination, force=force)
@@ -125,7 +125,8 @@ def _add_file(self, file_path: str):
125125
file_path : str
126126
Local path of the downloaded example file.
127127
"""
128-
self._downloads_list.append(file_path)
128+
if file_path not in self._downloads_list:
129+
self._downloads_list.append(file_path)
129130

130131
def _joinurl(self, base: str, directory: str) -> str:
131132
"""Join multiple paths to a base URL.
@@ -185,10 +186,11 @@ def _retrieve_data(self, url: str, filename: str, dest: str = None, force: bool
185186
str
186187
The local path where the file was saved.
187188
"""
189+
local_path = ""
188190
if dest is None:
189191
dest = tempfile.gettempdir() # Use system temp directory if no destination is provided
190192
local_path = Path(dest) / Path(filename).name
191-
if not force and Path.is_file(local_path):
193+
if not force and Path.is_file(Path(local_path)):
192194
return local_path
193195
try:
194196
local_path, _ = urllib.request.urlretrieve(url, filename=local_path)

tests/test_example_download.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def test_download():
3838

3939
assert Path.is_file(local_path)
4040

41+
# Check that file is cached
42+
local_path2 = download_manager.download_file(filename, directory)
43+
44+
assert local_path2 == local_path
45+
4146
download_manager.clear_download_cache()
4247

4348
assert not Path.is_file(local_path)
@@ -68,11 +73,16 @@ def test_get_filepath():
6873

6974
assert filepath == "https://github.com/ansys/example-data/raw/main/pymapdl/cfx_mapping/11_blades_mode_1_ND_0.csv"
7075

76+
filepath = download_manager._get_filepath_on_default_server(filename)
77+
78+
assert filepath == "https://github.com/ansys/example-data/raw/main/11_blades_mode_1_ND_0.csv"
79+
7180

7281
def test_destination_directory():
7382
"""Test getting the destination directory for a downloaded file."""
7483
filename = "11_blades_mode_1_ND_0.csv"
7584
directory = "pymapdl/cfx_mapping"
7685

77-
# Get the destination directory
86+
# Test directory gets created
7887
result = download_manager.download_file(filename, directory, destination="not_a_dir")
88+
assert result is not None

0 commit comments

Comments
 (0)