Exemple #1
0
    def test_load_command__import_error(self):
        """
        Unit test for :func:`pyramidcms.cli.load_command`

        Test what happens if the module cannot be imported, this should
        catch the ImportError and raise a CommandError.
        """
        app = 'pcms'
        command = 'command1'

        # settings_dict contains the pyramid settings
        settings_dict = {'test_setting': 'setting_value'}

        # ImportError is caught and CommandError raised
        with patch('importlib.import_module', side_effect=ImportError):
            with self.assertRaises(CommandError):
                cli.load_command(app, command, settings_dict)
Exemple #2
0
    def test_load_command__success(self, mock_import):
        """
        Unit test for :func:`pyramidcms.cli.load_command`

        Test if the command is dynamically imported correctly from the
        :module:`pyramidcms.commands` module, then checks if the Command
        class is constructed correctly.

        This test is done using :module:`unittest.mock`, no actual
        command is being executed during this test.
        """
        app = 'pcms'
        command = 'command1'

        cli.load_command(app, command, self.mock_settings)

        # module is imported, get a reference to mock module
        mock_import.assert_called_once_with('pyramidcms.commands.command1')
        mock_module = mock_import.return_value

        # Command class in mock module is instantiated
        mock_module.Command.assert_called_once_with(app, command, self.mock_settings)