Coverage for tests/command/builder/test_command.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-01-30 18:57 -0500

1"""Unit test file for command.builder.command.py.""" 

2from src.probable_fiesta.command.builder import command 

3from src.probable_fiesta.logger.logging_config import set_logger 

4 

5from logging import DEBUG 

6from unittest import TestCase 

7 

8# Create a logger 

9LOG = set_logger("test_command", DEBUG) 

10 

11 

12class TestCommandBuilderCommand(TestCase): 

13 

14 def setUp(self): 

15 self.command = None 

16 

17 def test_init(self): 

18 LOG.info("Test init") 

19 self.command = command.Command(None, None, None) 

20 self.assertEqual(self.command.name, None) 

21 self.assertEqual(self.command.function, None) 

22 self.assertEqual(self.command.args, (None,)) 

23 # test _str__ 

24 LOG.debug(str(self.command)) 

25 self.assertEqual(str(self.command), "Command: {'name': None, 'function': None, 'args': (None,)}") 

26 

27 def test_init_with_args(self): 

28 LOG.info("Test new with args") 

29 function = lambda: "Hello World!" 

30 self.command = command.Command("test", function, "--version") 

31 self.assertEqual(self.command.name, "test") 

32 self.assertAlmostEqual(self.command.function, function) 

33 self.assertEqual(self.command.args, ('--version',)) 

34 # test _str__ 

35 LOG.debug(str(self.command)) 

36 self.assertEqual(str(self.command), "Command: {'name': 'test', "+f"'function': {function},"+" 'args': ('--version',)}") 

37 

38 def test_factory(self): 

39 LOG.info("Test factory") 

40 function = lambda: "Hello World!" 

41 self.command = command.Command.Factory.new_command("test", function, "--version") 

42 self.assertEqual(self.command.name, "test") 

43 self.assertEqual(self.command.function, function) 

44 self.assertEqual(self.command.args, ('--version',)) 

45 # test _str__ 

46 LOG.debug(str(self.command)) 

47 self.assertEqual(str(self.command), "Command: {'name': 'test', "+f"'function': {function},"+" 'args': ('--version',)}") 

48 

49 def test_invoke(self): 

50 LOG.info("Test invoke") 

51 self.command = command.Command("test", lambda: "Hello World!", None) 

52 stdout = self.command.invoke() 

53 print(stdout) 

54 LOG.debug(stdout) 

55 self.assertEqual(stdout, "Hello World!") 

56 

57 def test_invoke_with_args(self): 

58 LOG.info("Test invoke with args") 

59 function = lambda x: (self.command.args) 

60 args = "--version" 

61 self.command = command.Command("test", function, args) 

62 stdout = self.command.invoke() 

63 LOG.debug(stdout) 

64 self.assertEqual(stdout, self.command.args) 

65 

66 def test_invoke_without_function(self): 

67 LOG.info("Test invoke without function expect error log") 

68 self.command = command.Command(None, None, None) 

69 stdout = self.command.invoke() 

70 LOG.debug(stdout) 

71 self.assertEqual(stdout, None) 

72