add tests for display command classes and hooks
Change-Id: Icf17fbcedf3ea9481abef0fd9ebb435648693be3 Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
		 Doug Hellmann
					Doug Hellmann
				
			
				
					committed by
					
						 Rajath Agasthya
						Rajath Agasthya
					
				
			
			
				
	
			
			
			 Rajath Agasthya
						Rajath Agasthya
					
				
			
						parent
						
							c6d258da1c
						
					
				
				
					commit
					d025ad9cd3
				
			| @@ -14,6 +14,8 @@ from cliff import app as application | ||||
| from cliff import command | ||||
| from cliff import commandmanager | ||||
| from cliff import hooks | ||||
| from cliff import lister | ||||
| from cliff import show | ||||
| from cliff.tests import base | ||||
|  | ||||
| import mock | ||||
| @@ -60,6 +62,22 @@ class TestCommand(command.Command): | ||||
|         return 42 | ||||
|  | ||||
|  | ||||
| class TestShowCommand(show.ShowOne): | ||||
|     """Description of command. | ||||
|     """ | ||||
|  | ||||
|     def take_action(self, parsed_args): | ||||
|         return (('Name',), ('value',)) | ||||
|  | ||||
|  | ||||
| class TestListerCommand(lister.Lister): | ||||
|     """Description of command. | ||||
|     """ | ||||
|  | ||||
|     def take_action(self, parsed_args): | ||||
|         return (('Name',), [('value',)]) | ||||
|  | ||||
|  | ||||
| class TestHook(hooks.CommandHook): | ||||
|  | ||||
|     _before_called = False | ||||
| @@ -131,3 +149,87 @@ class TestHooks(base.TestBase): | ||||
|         self.assertFalse(self.hook._after_called) | ||||
|         self.cmd.run(None) | ||||
|         self.assertTrue(self.hook._after_called) | ||||
|  | ||||
|  | ||||
| class TestShowOneHooks(base.TestBase): | ||||
|  | ||||
|     def setUp(self): | ||||
|         super(TestShowOneHooks, self).setUp() | ||||
|         self.app = make_app() | ||||
|         self.cmd = TestShowCommand(self.app, None, cmd_name='test') | ||||
|         self.hook = TestHook(self.cmd) | ||||
|         self.mgr = extension.ExtensionManager.make_test_instance( | ||||
|             [extension.Extension( | ||||
|                 'parser-hook', | ||||
|                 None, | ||||
|                 None, | ||||
|                 self.hook)], | ||||
|         ) | ||||
|         # Replace the auto-loaded hooks with our explicitly created | ||||
|         # manager. | ||||
|         self.cmd._hooks = self.mgr | ||||
|  | ||||
|     def test_get_parser(self): | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.assertEqual(results.added_by_hook, 'value') | ||||
|  | ||||
|     def test_get_epilog(self): | ||||
|         results = self.cmd.get_epilog() | ||||
|         self.assertIn('hook epilog', results) | ||||
|  | ||||
|     def test_before(self): | ||||
|         self.assertFalse(self.hook._before_called) | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.cmd.run(results) | ||||
|         self.assertTrue(self.hook._before_called) | ||||
|  | ||||
|     def test_after(self): | ||||
|         self.assertFalse(self.hook._after_called) | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.cmd.run(results) | ||||
|         self.assertTrue(self.hook._after_called) | ||||
|  | ||||
|  | ||||
| class TestListerHooks(base.TestBase): | ||||
|  | ||||
|     def setUp(self): | ||||
|         super(TestListerHooks, self).setUp() | ||||
|         self.app = make_app() | ||||
|         self.cmd = TestListerCommand(self.app, None, cmd_name='test') | ||||
|         self.hook = TestHook(self.cmd) | ||||
|         self.mgr = extension.ExtensionManager.make_test_instance( | ||||
|             [extension.Extension( | ||||
|                 'parser-hook', | ||||
|                 None, | ||||
|                 None, | ||||
|                 self.hook)], | ||||
|         ) | ||||
|         # Replace the auto-loaded hooks with our explicitly created | ||||
|         # manager. | ||||
|         self.cmd._hooks = self.mgr | ||||
|  | ||||
|     def test_get_parser(self): | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.assertEqual(results.added_by_hook, 'value') | ||||
|  | ||||
|     def test_get_epilog(self): | ||||
|         results = self.cmd.get_epilog() | ||||
|         self.assertIn('hook epilog', results) | ||||
|  | ||||
|     def test_before(self): | ||||
|         self.assertFalse(self.hook._before_called) | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.cmd.run(results) | ||||
|         self.assertTrue(self.hook._before_called) | ||||
|  | ||||
|     def test_after(self): | ||||
|         self.assertFalse(self.hook._after_called) | ||||
|         parser = self.cmd.get_parser('test') | ||||
|         results = parser.parse_args(['--added-by-hook', 'value']) | ||||
|         self.cmd.run(results) | ||||
|         self.assertTrue(self.hook._after_called) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user