Skip to content

Commit

Permalink
Tests: remove legacy tests/utils/configuration.py (#5500)
Browse files Browse the repository at this point in the history
These utilities have been replaced with `pytest` fixtures defined in
`conftest.py`, most notable, `empty_config` and `profile_factory`. The
tests that were using the legacy utils have been refactored.
  • Loading branch information
sphuber authored May 13, 2022
1 parent abc735a commit 137bf8e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 236 deletions.
1 change: 1 addition & 0 deletions aiida/manage/configuration/migrations/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def downgrade(self, config: ConfigType) -> None:
CONFIG_LOGGER.warning(f'changing profile name from `{profile_name}` to `{profile_name_new}`.')
profile_name = profile_name_new

profile['test_profile'] = test_profile
profiles[profile_name] = profile


Expand Down
181 changes: 83 additions & 98 deletions tests/cmdline/commands/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,142 +7,127 @@
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for `verdi profile`."""
# pylint: disable=redefined-outer-name
"""Tests for ``verdi profile``."""
from pgtest.pgtest import PGTest
import pytest

from aiida.cmdline.commands import cmd_profile, cmd_verdi
from aiida.manage import configuration
from tests.utils.configuration import create_mock_profile


@pytest.fixture(scope='class')
@pytest.fixture(scope='module')
def pg_test_cluster():
"""Create a standalone Postgres cluster, for setup tests."""
pg_test = PGTest()
yield pg_test
pg_test.close()


class TestVerdiProfileSetup:
"""Tests for `verdi profile`."""
@pytest.fixture
def mock_profiles(empty_config, profile_factory):
"""Create mock profiles and a runner object to invoke the CLI commands.
@pytest.fixture(autouse=True)
def init_profile(self, pg_test_cluster, empty_config, run_cli_command): # pylint: disable=redefined-outer-name,unused-argument
"""Initialize the profile."""
# pylint: disable=attribute-defined-outside-init
self.storage_backend_name = 'psql_dos'
self.pg_test = pg_test_cluster
self.cli_runner = run_cli_command
self.config = configuration.get_config()
self.profile_list = []
Note: this cannot be done in the `setUp` or `setUpClass` methods, because the temporary configuration instance
is not generated until the test function is entered, which calls the `config_with_profile` test fixture.
"""

def mock_profiles(self, **kwargs):
"""Create mock profiles and a runner object to invoke the CLI commands.
def _factory(**kwargs):
config = empty_config
profile_list = ['mock_profile1', 'mock_profile2', 'mock_profile3', 'mock_profile4']

Note: this cannot be done in the `setUp` or `setUpClass` methods, because the temporary configuration instance
is not generated until the test function is entered, which calls the `config_with_profile` test fixture.
"""
# pylint: disable=attribute-defined-outside-init
self.profile_list = ['mock_profile1', 'mock_profile2', 'mock_profile3', 'mock_profile4']
for profile_name in profile_list:
profile = profile_factory(profile_name, **kwargs)
config.add_profile(profile)

for profile_name in self.profile_list:
profile = create_mock_profile(profile_name, **kwargs)
self.config.add_profile(profile)
config.set_default_profile(profile_list[0], overwrite=True).store()

self.config.set_default_profile(self.profile_list[0], overwrite=True).store()
return profile_list

def test_help(self):
"""Tests help text for all `verdi profile` commands."""
self.mock_profiles()
return _factory

options = ['--help']

result = self.cli_runner(cmd_profile.profile_list, options)
assert 'Usage' in result.output
@pytest.mark.parametrize(
'command',
(cmd_profile.profile_list, cmd_profile.profile_setdefault, cmd_profile.profile_delete, cmd_profile.profile_show)
)
def test_help(run_cli_command, command):
"""Tests help text for all ``verdi profile`` commands."""
result = run_cli_command(command, ['--help'])
assert 'Usage' in result.output

result = self.cli_runner(cmd_profile.profile_setdefault, options)
assert 'Usage' in result.output

result = self.cli_runner(cmd_profile.profile_delete, options)
assert 'Usage' in result.output
def test_list(run_cli_command, mock_profiles):
"""Test the ``verdi profile list`` command."""
profile_list = mock_profiles()
result = run_cli_command(cmd_profile.profile_list)
assert 'Report: configuration folder:' in result.output
assert f'* {profile_list[0]}' in result.output
assert profile_list[1] in result.output

result = self.cli_runner(cmd_profile.profile_show, options)
assert 'Usage' in result.output

def test_list(self):
"""Test the `verdi profile list` command."""
self.mock_profiles()
def test_setdefault(run_cli_command, mock_profiles):
"""Test the ``verdi profile setdefault`` command."""
profile_list = mock_profiles()
run_cli_command(cmd_profile.profile_setdefault, [profile_list[1]])
result = run_cli_command(cmd_profile.profile_list)

result = self.cli_runner(cmd_profile.profile_list)
assert f'Report: configuration folder: {self.config.dirpath}' in result.output
assert f'* {self.profile_list[0]}' in result.output
assert self.profile_list[1] in result.output
assert 'Report: configuration folder:' in result.output
assert f'* {profile_list[1]}' in result.output

def test_setdefault(self):
"""Test the `verdi profile setdefault` command."""
self.mock_profiles()

self.cli_runner(cmd_profile.profile_setdefault, [self.profile_list[1]])
result = self.cli_runner(cmd_profile.profile_list)
def test_show(run_cli_command, mock_profiles):
"""Test the ``verdi profile show`` command."""
config = configuration.get_config()
profile_list = mock_profiles()
profile_name = profile_list[0]
profile = config.get_profile(profile_name)

assert f'Report: configuration folder: {self.config.dirpath}' in result.output
assert f'* {self.profile_list[1]}' in result.output
result = run_cli_command(cmd_profile.profile_show, [profile_name])
for key, value in profile.dictionary.items():
if isinstance(value, str):
assert key in result.output
assert value in result.output

def test_show(self):
"""Test the `verdi profile show` command."""
self.mock_profiles()

config = configuration.get_config()
profile_name = self.profile_list[0]
profile = config.get_profile(profile_name)
def test_show_with_profile_option(run_cli_command, mock_profiles):
"""Test the ``verdi profile show`` command in combination with ``-p/--profile``."""
profile_list = mock_profiles()
profile_name_non_default = profile_list[1]

result = self.cli_runner(cmd_profile.profile_show, [profile_name])
for key, value in profile.dictionary.items():
if isinstance(value, str):
assert key in result.output
assert value in result.output
# Specifying the non-default profile as argument should override the default
result = run_cli_command(cmd_profile.profile_show, [profile_name_non_default])
assert profile_name_non_default in result.output

def test_show_with_profile_option(self):
"""Test the `verdi profile show` command in combination with `-p/--profile."""
self.mock_profiles()
# Specifying ``-p/--profile`` should not override the argument default (which should be the default profile)
result = run_cli_command(cmd_verdi.verdi, ['-p', profile_name_non_default, 'profile', 'show'])
assert profile_name_non_default not in result.output

profile_name_non_default = self.profile_list[1]

# Specifying the non-default profile as argument should override the default
result = self.cli_runner(cmd_profile.profile_show, [profile_name_non_default])
assert profile_name_non_default in result.output
def test_delete_partial(run_cli_command, mock_profiles):
"""Test the ``verdi profile delete`` command.
# Specifying `-p/--profile` should not override the argument default (which should be the default profile)
result = self.cli_runner(cmd_verdi.verdi, ['-p', profile_name_non_default, 'profile', 'show'])
assert profile_name_non_default not in result.output
.. note:: we skip deleting the database as this might require sudo rights and this is tested in the CI tests
defined in the file ``.github/system_tests/test_profile.py``
"""
profile_list = mock_profiles()
run_cli_command(cmd_profile.profile_delete, ['--force', '--skip-db', profile_list[1]])
result = run_cli_command(cmd_profile.profile_list)
assert profile_list[1] not in result.output

def test_delete_partial(self):
"""Test the `verdi profile delete` command.

.. note:: we skip deleting the database as this might require sudo rights and this is tested in the CI tests
defined in the file `.github/system_tests/test_profile.py`
"""
self.mock_profiles()
def test_delete(run_cli_command, mock_profiles, pg_test_cluster):
"""Test for verdi profile delete command."""
kwargs = {'database_port': pg_test_cluster.dsn['port']}
profile_list = mock_profiles(**kwargs)

self.cli_runner(cmd_profile.profile_delete, ['--force', '--skip-db', self.profile_list[1]])
result = self.cli_runner(cmd_profile.profile_list)
assert self.profile_list[1] not in result.output
# Delete single profile
run_cli_command(cmd_profile.profile_delete, ['--force', profile_list[1]])
result = run_cli_command(cmd_profile.profile_list)
assert profile_list[1] not in result.output

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

kwargs = {'database_port': self.pg_test.dsn['port']}
self.mock_profiles(**kwargs)

# Delete single profile
self.cli_runner(profile_delete, ['--force', self.profile_list[1]])
result = self.cli_runner(profile_list)
assert self.profile_list[1] not in result.output

# Delete multiple profiles
self.cli_runner(profile_delete, ['--force', self.profile_list[2], self.profile_list[3]])
result = self.cli_runner(profile_list)
assert self.profile_list[2] not in result.output
assert self.profile_list[3] not in result.output
# Delete multiple profiles
run_cli_command(cmd_profile.profile_delete, ['--force', profile_list[2], profile_list[3]])
result = run_cli_command(cmd_profile.profile_list)
assert profile_list[2] not in result.output
assert profile_list[3] not in result.output
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def _create_profile(name='test-profile', **kwargs):
'broker_virtual_host': kwargs.pop('broker_virtual_host', ''),
'broker_parameters': kwargs.pop('broker_parameters', {}),
}
}
},
'test_profile': kwargs.pop('test_profile', True)
}

return Profile(name, profile_dictionary)
Expand Down
8 changes: 3 additions & 5 deletions tests/manage/configuration/migrations/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def test_add_test_profile_key_downgrade_profile(empty_config, profile_factory, c
exception should be raised.
"""
config = empty_config
profile = profile_factory('test_profile')
profile.is_test_profile = False
profile = profile_factory('test_profile', test_profile=False)
config.add_profile(profile)

config_migrated = downgrade_config(config.dictionary, 7)
Expand All @@ -122,16 +121,15 @@ def test_add_test_profile_key_downgrade_test_profile(empty_config, profile_facto
determine whether a profile is a test profile or not.
"""
config = empty_config
profile = profile_factory('profile')
profile.is_test_profile = True
profile = profile_factory('profile', test_profile=True)
config.add_profile(profile)

config_migrated = downgrade_config(config.dictionary, 7)
assert list(config_migrated['profiles'].keys()) == ['test_profile']
assert 'profile `profile` is a test profile but does not start with' in caplog.records[0].message
assert 'changing profile name from `profile` to `test_profile`.' in caplog.records[1].message

profile = profile_factory('test_profile')
profile = profile_factory('test_profile', test_profile=True)
config.add_profile(profile)

with pytest.raises(ConfigurationError, match=r'cannot change `.*` to `.*` because it already exists.'):
Expand Down
Loading

0 comments on commit 137bf8e

Please sign in to comment.