-
Notifications
You must be signed in to change notification settings - Fork 177
/
test_cli.py
139 lines (122 loc) · 4.91 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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.cli.main.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.Path.exists")
@patch("codecarbon.cli.main.Confirm.ask")
@patch("codecarbon.cli.main.questionary_prompt")
@patch("codecarbon.cli.main._get_access_token")
def test_config_no_local_new_all(
self, mock_token, mock_prompt, mock_confirm, mock_path_exists, 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_token.return_value = "user_token"
mock_prompt.side_effect = [
"Create New Organization",
"Create New Project",
"Create New Experiment",
]
mock_confirm.side_effect = [True, False, False, False]
result = self.runner.invoke(
codecarbon,
["config"],
input=f"{temp_codecarbon_config.name}\n",
)
self.assertEqual(result.exit_code, 0)
self.assertIn(
"Creating new experiment\nExperiment name : [Code Carbon user test]",
result.stdout,
)
self.assertIn(
"Consult configuration documentation for more configuration options",
result.stdout,
)
@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, 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",
}
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)
if __name__ == "__main__":
unittest.main()