cubicweb/test/unittest_cwctl.py
changeset 12692 8673da7c2f85
parent 12691 3f0e64630d94
child 12693 5e7212209ea6
equal deleted inserted replaced
12691:3f0e64630d94 12692:8673da7c2f85
    22 import unittest
    22 import unittest
    23 from unittest.mock import patch, MagicMock
    23 from unittest.mock import patch, MagicMock
    24 
    24 
    25 from logilab.common.clcommands import CommandLine
    25 from logilab.common.clcommands import CommandLine
    26 
    26 
       
    27 from cubicweb import cwctl
    27 from cubicweb.cwctl import ListCommand, InstanceCommand
    28 from cubicweb.cwctl import ListCommand, InstanceCommand
    28 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.server.migractions import ServerMigrationHelper
    30 from cubicweb.server.migractions import ServerMigrationHelper
    30 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
    31 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
    31 from cubicweb.__pkginfo__ import version as cw_version
    32 from cubicweb.__pkginfo__ import version as cw_version
    87 
    88 
    88     def test_instance(self, appid):
    89     def test_instance(self, appid):
    89         pass
    90         pass
    90 
    91 
    91 
    92 
       
    93 class _TestFailCommand(InstanceCommand):
       
    94     "I need some doc"
       
    95     name = "test_fail"
       
    96 
       
    97     def test_fail_instance(self, appid):
       
    98         raise Exception()
       
    99 
       
   100 
    92 class InstanceCommandTest(unittest.TestCase):
   101 class InstanceCommandTest(unittest.TestCase):
    93     def test_getting_called(self):
   102     def setUp(self):
    94         CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
   103         self.CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.',
    95                             version=cw_version, check_duplicated_command=False)
   104                                  version=cw_version, check_duplicated_command=False)
    96         cwcfg.load_cwctl_plugins()
   105         cwcfg.load_cwctl_plugins()
    97         CWCTL.register(_TestCommand)
   106         self.CWCTL.register(_TestCommand)
    98 
   107         self.CWCTL.register(_TestFailCommand)
    99         _TestCommand.test_instance = MagicMock(return_value=0)
       
   100 
   108 
   101         # pretend that this instance exists
   109         # pretend that this instance exists
   102         cwcfg.config_for = MagicMock(return_value=object())
   110         cwcfg.config_for = MagicMock(return_value=object())
   103 
   111 
       
   112     def test_getting_called(self):
       
   113         _TestCommand.test_instance = MagicMock(return_value=0)
       
   114 
   104         try:
   115         try:
   105             CWCTL.run(["test", "some_instance"])
   116             self.CWCTL.run(["test", "some_instance"])
   106         except SystemExit as ex:  # CWCTL will finish the program after that
   117         except SystemExit as ex:  # CWCTL will finish the program after that
   107             self.assertEqual(ex.code, 0)
   118             self.assertEqual(ex.code, 0)
   108         _TestCommand.test_instance.assert_called_with("some_instance")
   119         _TestCommand.test_instance.assert_called_with("some_instance")
   109 
   120 
       
   121     @patch.object(cwctl, 'get_pdb')
       
   122     def test_pdb_not_called(self, get_pdb):
       
   123         try:
       
   124             self.CWCTL.run(["test", "some_instance"])
       
   125         except SystemExit as ex:  # CWCTL will finish the program after that
       
   126             self.assertEqual(ex.code, 0)
       
   127 
       
   128         get_pdb.assert_not_called()
       
   129 
       
   130     @patch.object(cwctl, 'get_pdb')
       
   131     def test_pdb_called(self, get_pdb):
       
   132         post_mortem = get_pdb.return_value.post_mortem
       
   133         try:
       
   134             self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
       
   135         except SystemExit as ex:  # CWCTL will finish the program after that
       
   136             self.assertEqual(ex.code, 8)
       
   137 
       
   138         get_pdb.assert_called_once()
       
   139         post_mortem.assert_called_once()
       
   140 
       
   141     @patch.dict(sys.modules, ipdb=MagicMock())
       
   142     def test_ipdb_selected_and_called(self):
       
   143         ipdb = sys.modules['ipdb']
       
   144         try:
       
   145             self.CWCTL.run(["test_fail", "some_instance", "--pdb"])
       
   146         except SystemExit as ex:  # CWCTL will finish the program after that
       
   147             self.assertEqual(ex.code, 8)
       
   148 
       
   149         ipdb.post_mortem.assert_called_once()
       
   150 
   110 
   151 
   111 if __name__ == '__main__':
   152 if __name__ == '__main__':
   112     unittest.main()
   153     unittest.main()