Source code for schrodinger.test.af2commontests
# Unit testing framework
import unittest
from unittest.mock import MagicMock
from unittest.mock import patch
# Support modules
from schrodinger.Qt import QtWidgets # noqa: F401
from schrodinger.test import test_markers # noqa: F401
# Modules under test
from schrodinger.ui.qt.appframework2 import af2
from schrodinger.ui.qt.appframework2 import baseapp
from schrodinger.ui.qt.appframework2 import validation
#===============================================================================
# baseapp
#===============================================================================
[docs]class TestMixinBase:
[docs] def getTestObj(self):
raise NotImplementedError('Test class must implement getTestObj()')
[docs] def getTestObjClass(self):
raise NotImplementedError('Test class must implement getTestObj()')
[docs]class BasePanelTestMixin(TestMixinBase):
[docs] def getTestObj(self, stop_before=baseapp.SETUP):
TestObjClass = self.getTestObjClass()
return TestObjClass(stop_before=stop_before)
[docs] def test_init_kwargs(self):
PClass = self.getTestObjClass()
try:
PClass(stop_before=0)
except TypeError:
self.fail('Ensure that __init__() passes kwargs.')
[docs] def test_setup(self):
panel = self.getTestObj()
panel.setWindowTitle('')
panel.title = 'Test panel title'
panel.setup()
self.assertEqual(panel.title, panel.windowTitle())
[docs] def test_layOut(self):
panel = self.getTestObj()
panel.setup()
panel.layOut()
[docs] def test_setDefaults(self):
panel = self.getTestObj()
panel.setup()
panel.setDefaults()
[docs] @patch('schrodinger.Qt.QtWidgets.QApplication')
def test_startUp(self, mock_qapp):
mock_qapp.instance.return_value = None
with patch('schrodinger.ui.qt.appframework2.baseapp.maestro', None):
panel = self.getTestObj()
self.assertEqual(panel.run_mode, baseapp.MODE_STANDALONE)
mock_qapp.instance.return_value = True
with patch('schrodinger.ui.qt.appframework2.baseapp.maestro', None):
panel = self.getTestObj()
self.assertEqual(panel.run_mode, baseapp.MODE_SUBPANEL)
with patch('schrodinger.ui.qt.appframework2.baseapp.maestro', True):
panel = self.getTestObj()
self.assertEqual(panel.run_mode, baseapp.MODE_MAESTRO)
#===============================================================================
# validation
#===============================================================================
[docs]class ValidationTestMixin(TestMixinBase):
[docs] def test_reportValidation_implemented(self):
try:
obj = self.getTestObj()
results = validation.ValidationResults()
obj.reportValidation(results)
except NotImplementedError:
self.fail('reportValidation() not implemented')
#===============================================================================
# af2
#===============================================================================
[docs]class AppMixin(BasePanelTestMixin, ValidationTestMixin):
[docs] def test_hasTitle(self):
app = self.getTestObj()
app.setup()
app.setPanelOptions()
self.assertFalse(
str(app.title) == baseapp.PANEL_HAS_NO_TITLE,
"Panel title must be set in App.setPanelOptions()")
[docs]class JobAppMixin(AppMixin):
[docs] def test_startMethodDefined(self):
app = self.getTestObj()
app.setup()
self.assertTrue(af2.appmethods.START in app.app_methods,
'JobApp must define a start method')
[docs] def test_configDialogSet(self):
app = self.getTestObjClass()(stop_before=baseapp.SETUP)
cd = app.__dict__.get('config_dlg', None)
self.assertFalse(
cd, 'config_dlg should not be directly set. Instead, '
'implement the getConfigDialog method().')
# This test would be nice to have, but it would need some way of
# dry-firing a panel.
[docs] @unittest.skip('Panels generally require valid input to write')
@patch('schrodinger.ui.qt.appframework2.af2.jobcontrol.launch_job')
def test_writeMethodNoLaunch(self, mock_launch_job):
app = self.getTestObjClass()(stop_before=None)
app.writeJobCmd = MagicMock()
app.runValidation = MagicMock()
app.runValidation.return_value = True
app._write()
self.assertFalse(mock_launch_job.called)