Source code for schrodinger.test.pytest.fixture
"""
Features that a developer might use when writing tests.
"""
import os
import pathlib
import pytest
from unittest.mock import patch
from schrodinger import get_mmshare_version
from . import exetest
[docs]@pytest.fixture
def unittest_preset_manager():
"""
A fixture that mocks out the PresetManager with a PresetManager
that uses the current working directory as the presets directory.
This prevents reliance on system state.
"""
def _getPresetsDirectory(self):
return os.path.join('.', 'foo', self._panel_name)
with patch('schrodinger.models.presets.PresetManager._getPresetsDirectory',
_getPresetsDirectory):
yield
[docs]@pytest.fixture
def json_version():
"""
A fixture that mocks out the version used by
schrodinger.models.json.JsonableClassMixin. By default, the version returned
will be the current mmshare version. The return_value of the fixture can be
adjusted to change the version. Example::
def my_test(json_version):
from schrodinger.models import json
from schrodinger import get_mmshare_version
foo = json.JsonableClassMixin()
assert foo.get_version() == get_mmshare_version()
json_version.return_value = 10
assert foo.get_version() == 10
"""
with patch('schrodinger.models.json.JsonableClassMixin.get_version'
) as get_version_mock:
get_version_mock.return_value = get_mmshare_version()
yield get_version_mock
[docs]@pytest.fixture
def tmp_cwd(tmpdir_factory, request, monkeypatch):
nodeid = os.path.basename(request.node.nodeid)
nodeid = nodeid.replace(':', '_').replace('.', '_')
# nodeid = 'sort_test.sdaf'
d = tmpdir_factory.mktemp(nodeid)
setattr(request.node, 'schro_tmp_cwd', str(d))
cwd = os.getcwd()
try:
os.chdir(d)
yield str(d)
finally:
os.chdir(cwd)
class _allow_memtest:
"""
Run a command from a Python test.
For legacy mmlibs tests
Raises an exception if the command fails or if memtest fails.
"""
def __init__(self, request):
self.node = request.node
def check_call(self, cmd):
# This is all stuff that is required for memtest to accurately
# find suppressions, for instance.
t = exetest.ExecutableTest.from_parent(
name=self.node.fspath.basename, parent=self.node)
executable = pathlib.Path(cmd[0])
if not executable.exists():
executable = executable.with_suffix('.exe')
executable = os.fspath(executable.absolute())
cmd = [executable] + cmd[1:]
t.command = cmd
t.runtest(capture=False)
[docs]@pytest.fixture
def allow_memtest(request):
# class-based fixtures are not supported.
return _allow_memtest(request)
[docs]@pytest.fixture
def fast_jobdj():
"""
Use this fixture to make jobDJ use minimal delays in updating, at the
expense of throttling the CPU.
"""
with patch("schrodinger.job.queue.USE_JOB_CONTROL_MESSAGES", False):
with patch("schrodinger.job.queue.MONITOR_DELAY", 0.1):
yield