Skip to content

Commit

Permalink
Normalized paths in test_commons.py to make tests platform-independent
Browse files Browse the repository at this point in the history
  • Loading branch information
kremnik committed Sep 29, 2024
1 parent 5c242e2 commit 4a81fc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion deepface/commons/weight_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def download_weights_if_necessary(
"""
home = folder_utils.get_deepface_home()

target_file = os.path.join(home, ".deepface/weights", file_name)
target_file = os.path.normpath(os.path.join(home, ".deepface/weights", file_name))

if os.path.isfile(target_file):
logger.debug(f"{file_name} is already available at {target_file}")
Expand Down
33 changes: 18 additions & 15 deletions tests/test_commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def test_download_weights_for_available_file(
mock_get_deepface_home,
):
mock_isfile.return_value = True
mock_get_deepface_home.return_value = "/mock/home"
mock_get_deepface_home.return_value = os.path.normpath("/mock/home")

file_name = "model_weights.h5"
source_url = "http://example.com/model_weights.zip"

result = weight_utils.download_weights_if_necessary(file_name, source_url)

assert result == os.path.join("/mock/home", ".deepface/weights", file_name)
assert os.path.normpath(result) == os.path.normpath(os.path.join("/mock/home", ".deepface/weights", file_name))

mock_gdown.assert_not_called()
mock_zipfile.assert_not_called()
Expand All @@ -96,7 +96,7 @@ def test_download_weights_if_necessary_gdown_failure(
mock_get_deepface_home,
):
# Setting up the mock return values
mock_get_deepface_home.return_value = "/mock/home"
mock_get_deepface_home.return_value = os.path.normpath("/mock/home")
mock_isfile.return_value = False # Simulate file not being present

file_name = "model_weights.h5"
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_download_weights_if_necessary_no_compression(
mock_get_deepface_home,
):
# Setting up the mock return values
mock_get_deepface_home.return_value = "/mock/home"
mock_get_deepface_home.return_value = os.path.normpath("/mock/home")
mock_isfile.return_value = False # Simulate file not being present

file_name = "model_weights.h5"
Expand All @@ -134,13 +134,16 @@ def test_download_weights_if_necessary_no_compression(
# Call the function
result = weight_utils.download_weights_if_necessary(file_name, source_url)

# Normalize the expected path
expected_path = os.path.normpath("/mock/home/.deepface/weights/model_weights.h5")

# Assert that gdown.download was called with the correct parameters
mock_gdown.assert_called_once_with(
source_url, "/mock/home/.deepface/weights/model_weights.h5", quiet=False
source_url, expected_path, quiet=False
)

# Assert that the return value is correct
assert result == "/mock/home/.deepface/weights/model_weights.h5"
assert result == expected_path

# Assert that zipfile.ZipFile and bz2.BZ2File were not called
mock_zipfile.assert_not_called()
Expand All @@ -159,7 +162,7 @@ def test_download_weights_if_necessary_zip(
mock_get_deepface_home,
):
# Setting up the mock return values
mock_get_deepface_home.return_value = "/mock/home"
mock_get_deepface_home.return_value = os.path.normpath("/mock/home")
mock_isfile.return_value = False # Simulate file not being present

file_name = "model_weights.h5"
Expand All @@ -171,21 +174,21 @@ def test_download_weights_if_necessary_zip(

# Assert that gdown.download was called with the correct parameters
mock_gdown.assert_called_once_with(
source_url, "/mock/home/.deepface/weights/model_weights.h5.zip", quiet=False
source_url, os.path.normpath("/mock/home/.deepface/weights/model_weights.h5.zip"), quiet=False
)

# Simulate the unzipping behavior
mock_zipfile.return_value.__enter__.return_value.extractall = mock.Mock()

# Call the function again to simulate unzipping
with mock_zipfile.return_value as zip_ref:
zip_ref.extractall("/mock/home/.deepface/weights")
zip_ref.extractall(os.path.normpath("/mock/home/.deepface/weights"))

# Assert that the zip file was unzipped correctly
zip_ref.extractall.assert_called_once_with("/mock/home/.deepface/weights")
zip_ref.extractall.assert_called_once_with(os.path.normpath("/mock/home/.deepface/weights"))

# Assert that the return value is correct
assert result == "/mock/home/.deepface/weights/model_weights.h5"
assert result == os.path.normpath("/mock/home/.deepface/weights/model_weights.h5")

logger.info("✅ test download weights for zip is done")

Expand All @@ -201,7 +204,7 @@ def test_download_weights_if_necessary_bz2(
):

# Setting up the mock return values
mock_get_deepface_home.return_value = "/mock/home"
mock_get_deepface_home.return_value = os.path.normpath("/mock/home")
mock_isfile.return_value = False # Simulate file not being present

file_name = "model_weights.h5"
Expand All @@ -219,16 +222,16 @@ def test_download_weights_if_necessary_bz2(

# Assert that gdown.download was called with the correct parameters
mock_gdown.assert_called_once_with(
source_url, "/mock/home/.deepface/weights/model_weights.h5.bz2", quiet=False
source_url, os.path.normpath("/mock/home/.deepface/weights/model_weights.h5.bz2"), quiet=False
)

# Ensure open() is called once for writing the decompressed data
mock_open.assert_called_once_with("/mock/home/.deepface/weights/model_weights.h5", "wb")
mock_open.assert_called_once_with(os.path.normpath("/mock/home/.deepface/weights/model_weights.h5"), "wb")

# TODO: find a way to check write is called

# Assert that the return value is correct
assert result == "/mock/home/.deepface/weights/model_weights.h5"
assert result == os.path.normpath("/mock/home/.deepface/weights/model_weights.h5")

logger.info("✅ test download weights for bz2 is done")

Expand Down

0 comments on commit 4a81fc2

Please sign in to comment.