Skip to content

Commit bcb0bb4

Browse files
Vaghinak BasentsyanVaghinak Basentsyan
authored andcommitted
Fixed init flow
1 parent 147ab88 commit bcb0bb4

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/superannotate/lib/app/interface/cli_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def init():
5555
config = repo.get_one(uuid=constances.TOKEN_UUID)
5656
if config:
5757
if not input(
58-
f"File {config} exists. Do you want to overwrite? [y/n] : "
58+
f"File {repo.config_path} exists. Do you want to overwrite? [y/n] : "
5959
).lower() in ("y", "yes"):
6060
return
6161
token = input(

src/superannotate/lib/infrastructure/repositories.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class ConfigRepository(BaseManageableRepository):
3030
def __init__(self, config_path: str = constance.CONFIG_FILE_LOCATION):
3131
self._config_path = f"{config_path}"
3232

33+
@property
34+
def config_path(self):
35+
return self._config_path
36+
3337
def _create_config(self):
3438
"""
3539
Create a config file

tests/unit/test_controller_init.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import sys
2+
from io import StringIO
3+
import json
4+
from contextlib import contextmanager
5+
import pkg_resources
6+
from unittest import TestCase
7+
from unittest.mock import mock_open
8+
from unittest.mock import patch
9+
10+
from src.superannotate.lib.app.interface.cli_interface import CLIFacade
11+
from src.superannotate.lib.core import CONFIG_FILE_LOCATION
12+
13+
14+
try:
15+
CLI_VERSION = pkg_resources.get_distribution("superannotate").version
16+
except Exception:
17+
CLI_VERSION = None
18+
19+
20+
@contextmanager
21+
def catch_prints():
22+
out = StringIO()
23+
sys.stdout = out
24+
yield out
25+
26+
27+
class CLITest(TestCase):
28+
29+
@patch('builtins.input')
30+
def test_init_update(self, input_mock):
31+
input_mock.side_effect = ["y", "token"]
32+
with open(CONFIG_FILE_LOCATION) as f:
33+
config_data = f.read()
34+
with patch('builtins.open', mock_open(read_data=config_data)) as config_file:
35+
try:
36+
with catch_prints() as out:
37+
cli = CLIFacade()
38+
cli.init()
39+
except SystemExit:
40+
input_args = [i[0][0] for i in input_mock.call_args_list]
41+
self.assertIn(f"File {CONFIG_FILE_LOCATION} exists. Do you want to overwrite? [y/n] : ", input_args)
42+
input_mock.assert_called_with("Input the team SDK token from https://app.superannotate.com/team : ")
43+
config_file().write.assert_called_once_with(
44+
json.dumps(
45+
{"main_endpoint": "https://api.devsuperannotate.com", "ssl_verify": False, "token": "token"},
46+
indent=4
47+
)
48+
)
49+
self.assertEqual(out.getvalue().strip(), "Configuration file successfully updated.")
50+
51+
@patch('builtins.input')
52+
def test_init_create(self, input_mock):
53+
input_mock.side_effect = ["token"]
54+
with patch('builtins.open', mock_open(read_data="{}")) as config_file:
55+
try:
56+
with catch_prints() as out:
57+
cli = CLIFacade()
58+
cli.init()
59+
except SystemExit:
60+
input_mock.assert_called_with("Input the team SDK token from https://app.superannotate.com/team : ")
61+
config_file().write.assert_called_once_with(
62+
json.dumps(
63+
{"token": "token"},
64+
indent=4
65+
)
66+
)
67+
self.assertEqual(out.getvalue().strip(), "Configuration file successfully created.")

0 commit comments

Comments
 (0)