-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KEP-2170: Add unit and E2E tests for model and dataset initializers #2323
Open
seanlaii
wants to merge
1
commit into
kubeflow:master
Choose a base branch
from
seanlaii:initializer-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from kubeflow.training import DATASET_PATH | ||
|
||
import pkg.initializer_v2.utils.utils as utils | ||
|
||
|
||
@pytest.fixture | ||
def huggingface_dataset_instance(): | ||
"""Fixture for HuggingFace Dataset instance""" | ||
from pkg.initializer_v2.dataset.huggingface import HuggingFace | ||
|
||
return HuggingFace() | ||
|
||
|
||
# Test cases for config loading | ||
@pytest.mark.parametrize( | ||
"test_name, test_config, expected", | ||
[ | ||
( | ||
"Full config with token", | ||
{"storage_uri": "hf://dataset/path", "access_token": "test_token"}, | ||
{"storage_uri": "hf://dataset/path", "access_token": "test_token"}, | ||
), | ||
( | ||
"Minimal config without token", | ||
{"storage_uri": "hf://dataset/path"}, | ||
{"storage_uri": "hf://dataset/path", "access_token": None}, | ||
), | ||
], | ||
) | ||
def test_load_config(test_name, test_config, expected, huggingface_dataset_instance): | ||
"""Test config loading with different configurations""" | ||
print(f"Running test: {test_name}") | ||
|
||
with patch.object(utils, "get_config_from_env", return_value=test_config): | ||
huggingface_dataset_instance.load_config() | ||
assert ( | ||
huggingface_dataset_instance.config.storage_uri == expected["storage_uri"] | ||
) | ||
assert ( | ||
huggingface_dataset_instance.config.access_token == expected["access_token"] | ||
) | ||
|
||
print("Test execution completed") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, test_case", | ||
[ | ||
( | ||
"Successful download with token", | ||
{ | ||
"config": { | ||
"storage_uri": "hf://username/dataset-name", | ||
"access_token": "test_token", | ||
}, | ||
"should_login": True, | ||
"expected_repo_id": "username/dataset-name", | ||
"mock_login_side_effect": None, | ||
"mock_download_side_effect": None, | ||
"expected_error": None, | ||
}, | ||
), | ||
( | ||
"Successful download without token", | ||
{ | ||
"config": {"storage_uri": "hf://org/dataset-v1", "access_token": None}, | ||
"should_login": False, | ||
"expected_repo_id": "org/dataset-v1", | ||
"mock_login_side_effect": None, | ||
"mock_download_side_effect": None, | ||
"expected_error": None, | ||
}, | ||
), | ||
( | ||
"Login failure", | ||
{ | ||
"config": { | ||
"storage_uri": "hf://username/dataset-name", | ||
"access_token": "test_token", | ||
}, | ||
"should_login": True, | ||
"expected_repo_id": "username/dataset-name", | ||
"mock_login_side_effect": Exception, | ||
"mock_download_side_effect": None, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
( | ||
"Download failure", | ||
{ | ||
"config": { | ||
"storage_uri": "hf://invalid/repo/name", | ||
"access_token": None, | ||
}, | ||
"should_login": False, | ||
"expected_repo_id": "invalid/repo/name", | ||
"mock_login_side_effect": None, | ||
"mock_download_side_effect": Exception, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
], | ||
) | ||
def test_download_dataset(test_name, test_case, huggingface_dataset_instance): | ||
"""Test dataset download with different configurations""" | ||
|
||
print(f"Running test: {test_name}") | ||
|
||
huggingface_dataset_instance.config = MagicMock(**test_case["config"]) | ||
|
||
with patch("huggingface_hub.login") as mock_login, patch( | ||
"huggingface_hub.snapshot_download" | ||
) as mock_download: | ||
|
||
# Configure mock behavior | ||
if test_case["mock_login_side_effect"]: | ||
mock_login.side_effect = test_case["mock_login_side_effect"] | ||
if test_case["mock_download_side_effect"]: | ||
mock_download.side_effect = test_case["mock_download_side_effect"] | ||
|
||
# Execute test | ||
if test_case["expected_error"]: | ||
with pytest.raises(test_case["expected_error"]): | ||
huggingface_dataset_instance.download_dataset() | ||
else: | ||
huggingface_dataset_instance.download_dataset() | ||
|
||
# Verify login behavior | ||
if test_case["should_login"]: | ||
mock_login.assert_called_once_with(test_case["config"]["access_token"]) | ||
else: | ||
mock_login.assert_not_called() | ||
|
||
# Verify download parameters | ||
mock_download.assert_called_once_with( | ||
repo_id=test_case["expected_repo_id"], | ||
local_dir=DATASET_PATH, | ||
repo_type="dataset", | ||
) | ||
print("Test execution completed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import os | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from pkg.initializer_v2.dataset.__main__ import main | ||
|
||
|
||
@pytest.fixture | ||
def mock_env_vars(): | ||
"""Fixture to set and clean up environment variables""" | ||
original_env = dict(os.environ) | ||
|
||
def _set_env_vars(**kwargs): | ||
for key, value in kwargs.items(): | ||
if value is None: | ||
os.environ.pop(key, None) | ||
else: | ||
os.environ[key] = str(value) | ||
return os.environ | ||
|
||
yield _set_env_vars | ||
|
||
# Cleanup | ||
os.environ.clear() | ||
os.environ.update(original_env) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, test_case", | ||
[ | ||
( | ||
"Successful download with HuggingFace provider", | ||
{ | ||
"storage_uri": "hf://dataset/path", | ||
"access_token": "test_token", | ||
"mock_config_error": False, | ||
"mock_download_error": False, | ||
"expected_error": None, | ||
}, | ||
), | ||
( | ||
"Missing storage URI environment variable", | ||
{ | ||
"storage_uri": None, | ||
"access_token": None, | ||
"mock_config_error": False, | ||
"mock_download_error": False, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
( | ||
"Invalid storage URI scheme", | ||
{ | ||
"storage_uri": "invalid://dataset/path", | ||
"access_token": None, | ||
"mock_config_error": False, | ||
"mock_download_error": False, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
( | ||
"Config loading failure", | ||
{ | ||
"storage_uri": "hf://dataset/path", | ||
"access_token": None, | ||
"mock_config_error": True, | ||
"mock_download_error": False, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
( | ||
"Dataset download failure", | ||
{ | ||
"storage_uri": "hf://dataset/path/error", | ||
"access_token": None, | ||
"mock_config_error": False, | ||
"mock_download_error": True, | ||
"expected_error": Exception, | ||
}, | ||
), | ||
], | ||
) | ||
def test_dataset_main(test_name, test_case, mock_env_vars): | ||
"""Test main script with different scenarios""" | ||
print(f"Running test: {test_name}") | ||
|
||
# Setup mock environment variables | ||
env_vars = { | ||
"STORAGE_URI": test_case["storage_uri"], | ||
"ACCESS_TOKEN": test_case["access_token"], | ||
} | ||
mock_env_vars(**env_vars) | ||
|
||
# Setup mock HuggingFace instance | ||
mock_hf_instance = MagicMock() | ||
if test_case["mock_config_error"]: | ||
mock_hf_instance.load_config.side_effect = Exception | ||
if test_case["mock_download_error"]: | ||
mock_hf_instance.download_dataset.side_effect = Exception | ||
|
||
with patch( | ||
"pkg.initializer_v2.dataset.__main__.HuggingFace", | ||
return_value=mock_hf_instance, | ||
) as mock_hf: | ||
|
||
# Execute test | ||
if test_case["expected_error"]: | ||
with pytest.raises(test_case["expected_error"]): | ||
main() | ||
else: | ||
main() | ||
|
||
# Verify HuggingFace instance methods were called | ||
mock_hf_instance.load_config.assert_called_once() | ||
mock_hf_instance.download_dataset.assert_called_once() | ||
|
||
# Verify HuggingFace class instantiation | ||
if test_case["storage_uri"] and test_case["storage_uri"].startswith("hf://"): | ||
mock_hf.assert_called_once() | ||
|
||
print("Test execution completed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please tell me why do we need this fixture? Maybe we could simply declare
huggingface_dataset_instance = HuggingFace()
in each UTs. WDYT👀 @andreyvelich @seanlaiiThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Sounds good to me. I just put it there for abstracting the initialization of HugginFace instance which is used in all the test functions. But since currently the initialization process only involves one line of code, I agree with your approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me @Electronic-Waste.