Skip to content
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

Migrate verdi profile to use the click infrastructure (#1591) #1651

Merged
merged 13 commits into from
Jul 10, 2018
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
aiida/cmdline/commands/code.py|
aiida/cmdline/commands/graph.py|
aiida/cmdline/commands/group.py|
aiida/cmdline/commands/profile.py|
aiida/cmdline/commands/node.py|
aiida/cmdline/commands/rehash.py|
aiida/cmdline/commands/restapi.py|
Expand Down
1 change: 1 addition & 0 deletions aiida/backends/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'cmdline.commands.group': ['aiida.backends.tests.cmdline.commands.test_group'],
'cmdline.commands.node': ['aiida.backends.tests.cmdline.commands.test_node'],
'cmdline.commands.workflow': ['aiida.backends.tests.cmdline.commands.test_workflow'],
'cmdline.commands.profile': ['aiida.backends.tests.cmdline.commands.test_profile'],
'cmdline.params.types.calculation': ['aiida.backends.tests.cmdline.params.types.test_calculation'],
'cmdline.params.types.code': ['aiida.backends.tests.cmdline.params.types.test_code'],
'cmdline.params.types.computer': ['aiida.backends.tests.cmdline.params.types.test_computer'],
Expand Down
151 changes: 151 additions & 0 deletions aiida/backends/tests/cmdline/commands/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""
Test suite to test verdi profile command
"""

from click.testing import CliRunner
from aiida.backends.testbase import AiidaTestCase
from aiida.common import setup as aiida_cfg


class TestVerdiProfileSetup(AiidaTestCase):
"""
Test suite to test verdi profile command
"""

@classmethod
def setUpClass(cls, *args, **kwargs):
"""
Create dummy 5 profiles and set first profile as default profile.
All tests will run on these dummy profiles.

:param args: list of arguments
:param kwargs: list of keyword arguments
"""
super(TestVerdiProfileSetup, cls).setUpClass()

import tempfile

cls._old_aiida_config_folder = None
cls._new_aiida_config_folder = tempfile.mkdtemp()

cls._old_aiida_config_folder = aiida_cfg.AIIDA_CONFIG_FOLDER
aiida_cfg.AIIDA_CONFIG_FOLDER = cls._new_aiida_config_folder
aiida_cfg.create_base_dirs()

dummy_profile_list = ['dummy_profile1', 'dummy_profile2', 'dummy_profile3', 'dummy_profile4', 'dummy_profile5']
dummy_profile_list = ["{}_{}".format(profile_name, get_random_string(4)) for profile_name in dummy_profile_list]

for profile_name in dummy_profile_list:
dummy_profile = {}
dummy_profile['backend'] = 'django'
dummy_profile['db_host'] = 'localhost'
dummy_profile['db_port'] = '5432'
dummy_profile['email'] = 'dummy@localhost'
dummy_profile['db_name'] = profile_name
dummy_profile['db_user'] = 'dummy_user'
dummy_profile['db_pass'] = 'dummy_pass'
dummy_profile['repo'] = aiida_cfg.AIIDA_CONFIG_FOLDER + '/repository_' + profile_name
aiida_cfg.create_config_noninteractive(profile=profile_name, **dummy_profile)

aiida_cfg.set_default_profile(dummy_profile_list[0], force_rewrite=True)
cls.dummy_profile_list = dummy_profile_list

@classmethod
def tearDownClass(cls, *args, **kwargs):
"""
Remove dummy profiles created in setUpClass.

:param args: list of arguments
:param kwargs: list of keyword arguments
"""

aiida_cfg.AIIDA_CONFIG_FOLDER = cls._old_aiida_config_folder

import os
import shutil
if os.path.isdir(cls._new_aiida_config_folder):
shutil.rmtree(cls._new_aiida_config_folder)

def setUp(self):
"""
Create runner object to run tests
"""
self.runner = CliRunner()

def test_help(self):
"""
Tests help text for all profile sub commands
"""
options = ["--help"]
from aiida.cmdline.commands.profile import (profile_list, profile_setdefault, profile_delete)

result = self.runner.invoke(profile_list, options)
self.assertIsNone(result.exception)
self.assertIn('Usage', result.output)

result = self.runner.invoke(profile_setdefault, options)
self.assertIsNone(result.exception)
self.assertIn('Usage', result.output)

result = self.runner.invoke(profile_delete, options)
self.assertIsNone(result.exception)
self.assertIn('Usage', result.output)

def test_list(self):
"""
Test for verdi profile list command
"""
from aiida.cmdline.commands.profile import profile_list
result = self.runner.invoke(profile_list)
self.assertIsNone(result.exception)
self.assertIn('Configuration folder: ' + self._new_aiida_config_folder, result.output)
self.assertIn('* {}'.format(self.dummy_profile_list[0]), result.output)
self.assertIn(self.dummy_profile_list[1], result.output)

def test_setdefault(self):
"""
Test for verdi profile setdefault command
"""
from aiida.cmdline.commands.profile import profile_setdefault
result = self.runner.invoke(profile_setdefault, [self.dummy_profile_list[1]])
self.assertIsNone(result.exception)

from aiida.cmdline.commands.profile import profile_list
result = self.runner.invoke(profile_list)

self.assertIsNone(result.exception)
self.assertIn('Configuration folder: ' + self._new_aiida_config_folder, result.output)
self.assertIn('* {}'.format(self.dummy_profile_list[1]), result.output)
self.assertIsNone(result.exception)

def test_delete(self):
"""
Test for verdi profile delete command
"""
from aiida.cmdline.commands.profile import profile_delete, profile_list

### delete single profile
result = self.runner.invoke(profile_delete, ["--force", self.dummy_profile_list[2]])
self.assertIsNone(result.exception)

result = self.runner.invoke(profile_list)
self.assertIsNone(result.exception)

self.assertNotIn(self.dummy_profile_list[2], result.output)
self.assertIsNone(result.exception)

### delete multiple profile
result = self.runner.invoke(profile_delete, ["--force", self.dummy_profile_list[3], self.dummy_profile_list[4]])
self.assertIsNone(result.exception)

result = self.runner.invoke(profile_list)
self.assertIsNone(result.exception)
self.assertNotIn(self.dummy_profile_list[3], result.output)
self.assertNotIn(self.dummy_profile_list[4], result.output)
self.assertIsNone(result.exception)

def get_random_string(length):
import string
import random

return ''.join(random.choice(string.ascii_letters) for m in range(length))
Loading