# Path: syncano_cli/main.py
# def main():
# 
# Path: tests/base.py
# class BaseCLITest(InstanceMixin, IntegrationTest):
# 
#     @classmethod
#     def setUpClass(cls):
#         super(BaseCLITest, cls).setUpClass()
#         cls.yml_file = 'syncano.yml'
#         cls.scripts_dir = 'scripts/'
# 
#     def setUp(self):
#         self.runner.invoke(cli, args=['login', '--instance-name', self.instance.name], obj={})
#         self.assert_config_variable_exists(ACCOUNT_CONFIG, 'DEFAULT', 'key')
#         self.assert_config_variable_exists(ACCOUNT_CONFIG, 'DEFAULT', 'instance_name')
# 
#     def tearDown(self):
#         # remove the .syncano file
#         if os.path.isfile(ACCOUNT_CONFIG_PATH):
#             os.remove(ACCOUNT_CONFIG_PATH)
#         self.assertFalse(os.path.isfile(ACCOUNT_CONFIG_PATH))
# 
#     def assert_file_exists(self, path):
#         self.assertTrue(os.path.isfile(path))
# 
#     def assert_config_variable_exists(self, config, section, key):
#         self.assertTrue(config.get(section, key))
# 
#     def assert_config_variable_equal(self, config, section, key, value):
#         self.assertEqual(config.get(section, key), value)
# 
#     def assert_class_yml_file(self, unique):
#         with open(self.yml_file) as syncano_yml:
#             yml_content = yaml.safe_load(syncano_yml)
#             self.assertIn('test_class{unique}'.format(unique=unique), yml_content['classes'])
#             self.assertIn('test_field{unique}'.format(unique=unique),
#                           yml_content['classes']['test_class{unique}'.format(unique=unique)]['fields'])
# 
#     def assert_field_in_schema(self, syncano_class, unique):
#         has_field = False
#         for field_schema in syncano_class.schema:
#             if field_schema['name'] == 'test_field{unique}'.format(unique=unique) and field_schema['type'] == 'string':
#                 has_field = True
#                 break
# 
#         self.assertTrue(has_field)
# 
#     def assert_script_source(self, script_path, assert_source):
#         with open(script_path, 'r+') as f:
#             source = f.read()
#             self.assertEqual(source, assert_source)
# 
#     def assert_script_remote(self, script_name, source):
#         is_script = False
#         check_script = None
#         scripts = self.instance.scripts.all()
#         for script in scripts:
#             if script.label == script_name:
#                 is_script = True
#                 check_script = script  # be explicit;
#                 break
#         self.assertTrue(is_script)
#         self.assertEqual(check_script.source, source)
# 
#     def get_script_path(self, unique):
#         return '{dir_name}{script_name}.py'.format(
#             dir_name=self.scripts_dir,
#             script_name='script_{unique}'.format(unique=unique)
#         )
# 
#     def modify_yml_file(self, key, object, operation='update'):
#         with open(self.yml_file, 'rb') as f:
#             yml_syncano = yaml.safe_load(f)
#         if operation == 'update':
#             yml_syncano[key].update(object)
#         elif operation == 'append':
#             yml_syncano[key].append(object)
#         else:
#             raise Exception('not supported operation')
# 
#         with open(self.yml_file, 'wt') as f:
#             f.write(yaml.safe_dump(yml_syncano, default_flow_style=False))
# 
#     def create_syncano_class(self, unique):
#         self.instance.classes.create(
#             name='test_class{unique}'.format(unique=unique),
#             schema=[
#                 {'name': 'test_field{unique}'.format(unique=unique), 'type': 'string'}
#             ]
#         )
# 
#     def get_class_object_dict(self, unique):
#         return {
#             'test_class{unique}'.format(unique=unique): {
#                 'fields': {
#                     'test_field{unique}'.format(unique=unique): 'string'
#                 }
#             }
#         }
# 
#     def get_syncano_class(self, unique):
#         return self.instance.classes.get(name='test_class{unique}'.format(unique=unique))
# 
#     def create_script(self, unique):
#         self.instance.scripts.create(
#             runtime_name=RuntimeChoices.PYTHON_V5_0,
#             source='print(12)',
#             label='script_{unique}'.format(unique=unique)
#         )
# 
#     def create_script_locally(self, source, unique):
#         script_path = 'scripts/script_{unique}.py'.format(unique=unique)
#         new_script = {
#             'runtime': 'python_library_v5.0',
#             'label': 'script_{unique}'.format(unique=unique),
#             'script': script_path
#         }
# 
#         self.modify_yml_file('scripts', new_script, operation='append')
# 
#         # create a file also;
#         with open(script_path, 'w+') as f:
#             f.write(source)


from syncano_cli.main import cli
from tests.base import BaseCLITest
        self._add_config('test', '123')
        result = self._list_config()

        self.assertIn('test', result.output)
        self.assertIn('123', result.output)

    def test_add_config_var(self):
        # test basic add;
        result = self._add_config('api-key', '981xx')
        self.assertIn('Variable `api-key` set to `981xx`', result.output)

        result = self._list_config()
        self.assertIn('api-key', result.output)
        self.assertIn('981xx', result.output)

        # test if ERROR is displayed;
        result = self._add_config('api-key', '981xx')
        self.assertIn('already in config', result.output)

        # test multiple add;
        self._add_config('totally-new-name', 'new-value')
        result = self._list_config()
        self.assertIn('api-key', result.output)
        self.assertIn('981xx', result.output)
        self.assertIn('totally-new-name', result.output)
        self.assertIn('new-value', result.output)

    def test_modify_config_var(self):
        self._add_config('api-key-mod', '981xx')


# Path: syncano_cli/main.py
# def main():
# 
# Path: tests/base.py
# class BaseCLITest(InstanceMixin, IntegrationTest):
# 
#     @classmethod
#     def setUpClass(cls):
#         super(BaseCLITest, cls).setUpClass()
#         cls.yml_file = 'syncano.yml'
#         cls.scripts_dir = 'scripts/'
# 
#     def setUp(self):
#         self.runner.invoke(cli, args=['login', '--instance-name', self.instance.name], obj={})
#         self.assert_config_variable_exists(ACCOUNT_CONFIG, 'DEFAULT', 'key')
#         self.assert_config_variable_exists(ACCOUNT_CONFIG, 'DEFAULT', 'instance_name')
# 
#     def tearDown(self):
#         # remove the .syncano file
#         if os.path.isfile(ACCOUNT_CONFIG_PATH):
#             os.remove(ACCOUNT_CONFIG_PATH)
#         self.assertFalse(os.path.isfile(ACCOUNT_CONFIG_PATH))
# 
#     def assert_file_exists(self, path):
#         self.assertTrue(os.path.isfile(path))
# 
#     def assert_config_variable_exists(self, config, section, key):
#         self.assertTrue(config.get(section, key))
# 
#     def assert_config_variable_equal(self, config, section, key, value):
#         self.assertEqual(config.get(section, key), value)
# 
#     def assert_class_yml_file(self, unique):
#         with open(self.yml_file) as syncano_yml:
#             yml_content = yaml.safe_load(syncano_yml)
#             self.assertIn('test_class{unique}'.format(unique=unique), yml_content['classes'])
#             self.assertIn('test_field{unique}'.format(unique=unique),
#                           yml_content['classes']['test_class{unique}'.format(unique=unique)]['fields'])
# 
#     def assert_field_in_schema(self, syncano_class, unique):
#         has_field = False
#         for field_schema in syncano_class.schema:
#             if field_schema['name'] == 'test_field{unique}'.format(unique=unique) and field_schema['type'] == 'string':
#                 has_field = True
#                 break
# 
#         self.assertTrue(has_field)
# 
#     def assert_script_source(self, script_path, assert_source):
#         with open(script_path, 'r+') as f:
#             source = f.read()
#             self.assertEqual(source, assert_source)
# 
#     def assert_script_remote(self, script_name, source):
#         is_script = False
#         check_script = None
#         scripts = self.instance.scripts.all()
#         for script in scripts:
#             if script.label == script_name:
#                 is_script = True
#                 check_script = script  # be explicit;
#                 break
#         self.assertTrue(is_script)
#         self.assertEqual(check_script.source, source)
# 
#     def get_script_path(self, unique):
#         return '{dir_name}{script_name}.py'.format(
#             dir_name=self.scripts_dir,
#             script_name='script_{unique}'.format(unique=unique)
#         )
# 
#     def modify_yml_file(self, key, object, operation='update'):
#         with open(self.yml_file, 'rb') as f:
#             yml_syncano = yaml.safe_load(f)
#         if operation == 'update':
#             yml_syncano[key].update(object)
#         elif operation == 'append':
#             yml_syncano[key].append(object)
#         else:
#             raise Exception('not supported operation')
# 
#         with open(self.yml_file, 'wt') as f:
#             f.write(yaml.safe_dump(yml_syncano, default_flow_style=False))
# 
#     def create_syncano_class(self, unique):
#         self.instance.classes.create(
#             name='test_class{unique}'.format(unique=unique),
#             schema=[
#                 {'name': 'test_field{unique}'.format(unique=unique), 'type': 'string'}
#             ]
#         )
# 
#     def get_class_object_dict(self, unique):
#         return {
#             'test_class{unique}'.format(unique=unique): {
#                 'fields': {
#                     'test_field{unique}'.format(unique=unique): 'string'
#                 }
#             }
#         }
# 
#     def get_syncano_class(self, unique):
#         return self.instance.classes.get(name='test_class{unique}'.format(unique=unique))
# 
#     def create_script(self, unique):
#         self.instance.scripts.create(
#             runtime_name=RuntimeChoices.PYTHON_V5_0,
#             source='print(12)',
#             label='script_{unique}'.format(unique=unique)
#         )
# 
#     def create_script_locally(self, source, unique):
#         script_path = 'scripts/script_{unique}.py'.format(unique=unique)
#         new_script = {
#             'runtime': 'python_library_v5.0',
#             'label': 'script_{unique}'.format(unique=unique),
#             'script': script_path
#         }
# 
#         self.modify_yml_file('scripts', new_script, operation='append')
# 
#         # create a file also;
#         with open(script_path, 'w+') as f:
#             f.write(source)


# Path: tests/test_config_commands.py
from syncano_cli.main import cli
from tests.base import BaseCLITest

        self._add_config('test', '123')
        result = self._list_config()

        self.assertIn('test', result.output)
        self.assertIn('123', result.output)

    def test_add_config_var(self):
        # test basic add;
        result = self._add_config('api-key', '981xx')
        self.assertIn('Variable `api-key` set to `981xx`', result.output)

        result = self._list_config()
        self.assertIn('api-key', result.output)
        self.assertIn('981xx', result.output)

        # test if ERROR is displayed;
        result = self._add_config('api-key', '981xx')
        self.assertIn('already in config', result.output)

        # test multiple add;
        self._add_config('totally-new-name', 'new-value')
        result = self._list_config()
        self.assertIn('api-key', result.output)
        self.assertIn('981xx', result.output)
        self.assertIn('totally-new-name', result.output)
        self.assertIn('new-value', result.output)

    def test_modify_config_var(self):
        self._add_config('api-key-mod', '981xx')


        result = self.runner.invoke(cli, args=['config', 'modify', 'api-key-mod', '134'], obj={})