Buckets:

download
raw
7.38 kB
import os
import tempfile
import unittest
from unittest.mock import MagicMock, patch
from typer.testing import CliRunner
from codecarbon import __app_name__, __version__
from codecarbon.cli.main import codecarbon
# MOCK API CLIENT
@patch("codecarbon.core.api_client.ApiClient")
class TestApp(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
self.mock_api_client = MagicMock()
self.mock_api_client.get_list_organizations.return_value = [
{"id": "1", "name": "test org Code Carbon"}
]
self.mock_api_client.list_teams_from_organization.return_value = [
{"id": "1", "name": "test team Code Carbon"}
]
self.mock_api_client.list_projects_from_team.return_value = [
{"id": "1", "name": "test project Code Carbon"}
]
self.mock_api_client.list_experiments_from_project.return_value = [
{"id": "1", "name": "test experiment Code Carbon"}
]
self.mock_api_client.create_organization.return_value = {
"id": "1",
"name": "test org Code Carbon",
}
self.mock_api_client.create_team.return_value = {
"id": "1",
"name": "test team Code Carbon",
}
self.mock_api_client.create_project.return_value = {
"id": "1",
"name": "test project Code Carbon",
}
self.mock_api_client.add_experiment.return_value = {
"id": "1",
"name": "test experiment Code Carbon",
}
def test_app(self, MockApiClient):
result = self.runner.invoke(codecarbon, ["--version"])
self.assertEqual(result.exit_code, 0)
self.assertIn(__app_name__, result.stdout)
self.assertIn(__version__, result.stdout)
@patch("codecarbon.cli.main.show_config")
@patch("codecarbon.cli.main.get_api_key")
@patch("codecarbon.cli.main.Path.exists")
@patch("codecarbon.cli.main.Confirm.ask")
@patch("codecarbon.cli.main.questionary_prompt")
@patch("codecarbon.cli.auth.get_access_token")
@patch("typer.prompt")
def test_config_no_local_new_all(
self,
mock_typer_prompt,
mock_token,
mock_prompt,
mock_confirm,
mock_path_exists,
mock_get_api_key,
mock_show_config,
MockApiClient,
):
temp_dir = os.getenv("RUNNER_TEMP", tempfile.gettempdir())
temp_codecarbon_config = tempfile.NamedTemporaryFile(
mode="w+t", delete=False, dir=temp_dir
)
def side_effect_wrapper(*args, **kwargs):
"""Side effect wrapper to simulate the first call to path.exists to avoid picking up global config"""
if side_effect_wrapper.call_count == 0:
side_effect_wrapper.call_count += 1
return False
else:
return True
side_effect_wrapper.call_count = 0
mock_path_exists.side_effect = side_effect_wrapper
MockApiClient.return_value = self.mock_api_client
mock_get_api_key.return_value = "api_key"
mock_token.return_value = "user_token"
mock_show_config.return_value = None # Mock show_config to avoid issues
# Set up typer.prompt to return appropriate values
mock_typer_prompt.side_effect = [
temp_codecarbon_config.name, # file path in create_new_config_file
"https://api.codecarbon.io", # api_endpoint
"Code Carbon user test", # org_name
"Code Carbon user test", # org_description
"Code Carbon user test", # project_name
"Code Carbon user test", # project_description
"Code Carbon user test", # exp_name
"Code Carbon user test", # exp_description
"n", # Running on cloud
"France", # country_name
"FRA", # country_iso_code
"FR", # region
"Europe/Paris", # TODO: This one more line make test works, there is a bug somewhere...
]
mock_prompt.side_effect = [
"Create New Organization",
"Create New Project",
"Create New Experiment",
]
mock_confirm.side_effect = [True, False, False, False]
try:
result = self.runner.invoke(
codecarbon,
["config"],
)
if result.exception:
import traceback
print("Traceback:")
traceback.print_exception(
type(result.exception),
result.exception,
result.exception.__traceback__,
)
self.assertEqual(result.exit_code, 0)
# Since we mocked show_config, we might not see the expected output
# Just check that the command completed successfully
self.assertIn(
"Creating new experiment",
result.stdout,
)
self.assertIn(
"Consult configuration documentation for more configuration options",
result.stdout,
)
finally:
# Clean up the temporary file
try:
os.unlink(temp_codecarbon_config.name)
except OSError:
pass
@patch("codecarbon.cli.auth.get_access_token")
@patch("codecarbon.cli.main.Path.exists")
@patch("codecarbon.cli.main.get_config")
@patch("codecarbon.cli.main.questionary_prompt")
def test_init_use_local(
self, mock_prompt, mock_config, mock_path_exists, mock_token, MockApiClient
):
mock_prompt.return_value = "~/.codecarbon.config"
mock_config.return_value = {
"api_endpoint": "http://localhost:8008",
"organization_id": "114",
"project_id": "133",
"experiment_id": "yolo123",
}
mock_token.return_value = "mock_token"
def side_effect_wrapper(*args, **kwargs):
"""Side effect wrapper to simulate the first call to path.exists to avoid picking up global config"""
if side_effect_wrapper.call_count == 1:
side_effect_wrapper.call_count += 1
return False
else:
side_effect_wrapper.call_count += 1
return True
side_effect_wrapper.call_count = 0
mock_path_exists.side_effects = side_effect_wrapper
result = self.runner.invoke(codecarbon, ["config"], input="n")
self.assertEqual(result.exit_code, 0)
self.assertIn(
"Using already existing global config file ",
result.stdout,
)
def custom_questionary_side_effect(*args, **kwargs):
default_value = kwargs.get("default")
return MagicMock(return_value=default_value)
class TestQuestionaryPrompt(unittest.TestCase):
@patch("questionary.select")
def test_questionary_prompt_returns_selected_value(self, mock_select):
from codecarbon.cli.main import questionary_prompt
mock_select.return_value.ask.return_value = "selected"
result = questionary_prompt("Pick one", ["a", "b"], "a")
self.assertEqual(result, "selected")
mock_select.assert_called_once_with("Pick one", ["a", "b"], "a")
if __name__ == "__main__":
unittest.main()

Xet Storage Details

Size:
7.38 kB
·
Xet hash:
bb9bcca6331b2adc9bef48defef391aee41eb28892c614a1621c298eeaa27a72

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.