test_init.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import unittest
  2. import logging
  3. import sys
  4. import mock
  5. from kalliope import parse_args, configure_logging, main
  6. class TestInit(unittest.TestCase):
  7. def test_parse_args(self):
  8. # start option
  9. parser = parse_args(['value'])
  10. self.assertEqual(parser.action, "value")
  11. # no option
  12. with self.assertRaises(SystemExit):
  13. parse_args([])
  14. parser = parse_args(['start', '--run-synapse', 'run_synapse'])
  15. self.assertEqual(parser.run_synapse, 'run_synapse')
  16. parser = parse_args(['start', '--run-order', 'my order'])
  17. self.assertEqual(parser.run_order, 'my order')
  18. def test_configure_logging(self):
  19. logger = logging.getLogger("kalliope")
  20. # Level 10 = DEBUG
  21. configure_logging(debug=True)
  22. self.assertEqual(logger.getEffectiveLevel(), 10)
  23. logger.propagate = False
  24. # Level 20 = INFO
  25. configure_logging(debug=False)
  26. self.assertEqual(logger.getEffectiveLevel(), 20)
  27. # disable after testing
  28. logger.disabled = True
  29. def test_main(self):
  30. # test start kalliope
  31. sys.argv = ['kalliope.py', 'start']
  32. with mock.patch('kalliope.start_rest_api') as mock_rest_api:
  33. with mock.patch('kalliope.start_kalliope') as mock_start_kalliope:
  34. mock_rest_api.return_value = None
  35. main()
  36. mock_rest_api.assert_called()
  37. mock_start_kalliope.assert_called()
  38. # test start gui
  39. sys.argv = ['kalliope.py', 'gui']
  40. with mock.patch('kalliope.core.ShellGui.__init__') as mock_shell_gui:
  41. mock_shell_gui.return_value = None
  42. main()
  43. mock_shell_gui.assert_called()
  44. # test run_synapse
  45. sys.argv = ['kalliope.py', 'start', '--run-synapse', 'synapse_name']
  46. with mock.patch('kalliope.core.SynapseLauncher.start_synapse_by_name') as mock_synapse_launcher:
  47. mock_synapse_launcher.return_value = None
  48. main()
  49. mock_synapse_launcher.assert_called()
  50. # test run order
  51. sys.argv = ['kalliope.py', 'start', '--run-order', 'my order']
  52. with mock.patch('kalliope.core.SynapseLauncher.run_matching_synapse_from_order') as mock_synapse_launcher:
  53. mock_synapse_launcher.return_value = None
  54. main()
  55. mock_synapse_launcher.assert_called()
  56. # action doesn't exist
  57. sys.argv = ['kalliope.py', 'non_existing_action']
  58. with self.assertRaises(SystemExit):
  59. main()
  60. # install
  61. sys.argv = ['kalliope.py', 'install', '--git-url', 'https://my_url']
  62. with mock.patch('kalliope.core.ResourcesManager.install') as mock_resource_manager:
  63. mock_resource_manager.return_value = None
  64. main()
  65. mock_resource_manager.assert_called()
  66. # install, no URL
  67. sys.argv = ['kalliope.py', 'install']
  68. with self.assertRaises(SystemExit):
  69. main()
  70. sys.argv = ['kalliope.py', 'install', '--git-url']
  71. with self.assertRaises(SystemExit):
  72. main()
  73. # uninstall
  74. sys.argv = ['kalliope.py', 'uninstall', '--neuron-name', 'neuron_name']
  75. with mock.patch('kalliope.core.ResourcesManager.uninstall') as mock_resource_manager:
  76. mock_resource_manager.return_value = None
  77. main()
  78. mock_resource_manager.assert_called()
  79. sys.argv = ['kalliope.py', 'uninstall']
  80. with self.assertRaises(SystemExit):
  81. main()
  82. if __name__ == '__main__':
  83. unittest.main()
  84. # suite = unittest.TestSuite()
  85. # suite.addTest(TestInit("test_main"))
  86. # runner = unittest.TextTestRunner()
  87. # runner.run(suite)