Coverage for tests/test_app/management/commands/groups.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.3.4, created at 2024-01-20 18:02 +0000

1import typing as t 

2 

3from django.utils.translation import gettext_lazy as _ 

4from typer import Argument, Option 

5 

6from django_typer import TyperCommand, command, group 

7 

8 

9class Command(TyperCommand): 

10 help = _("Test multiple groups commands and callbacks") 

11 

12 precision = 2 

13 verbosity = 1 

14 

15 @command() 

16 def echo(self, message: str): 

17 """ 

18 Echo the given message. 

19 """ 

20 assert issubclass(self.__class__, Command) 

21 return message 

22 

23 @group() 

24 def math(self, precision: int = precision): 

25 """ 

26 Do some math at the given precision. 

27 """ 

28 assert issubclass(self.__class__, Command) 

29 self.precision = precision 

30 

31 @math.command() 

32 def multiply( 

33 self, 

34 number1: t.Annotated[ 

35 float, Argument(help=_("The first number."), show_default=False) 

36 ], 

37 number2: t.Annotated[ 

38 float, Argument(help=_("The second number."), show_default=False) 

39 ], 

40 numbers: t.Annotated[ 

41 t.List[float], 

42 Argument( 

43 help=_("The list of numbers to multiply: n1*n2*n3*...*nN. "), 

44 show_default=False, 

45 ), 

46 ], 

47 ): 

48 """ 

49 Multiply the given numbers. 

50 """ 

51 assert issubclass(self.__class__, Command) 

52 res = number1 * number2 

53 for n in numbers: 

54 res *= n 

55 return f"{res:.{self.precision}f}" 

56 

57 @math.command() 

58 def divide( 

59 self, 

60 number1: t.Annotated[ 

61 float, Argument(help=_("The numerator."), show_default=False) 

62 ], 

63 number2: t.Annotated[ 

64 float, Argument(help=_("The denominator."), show_default=False) 

65 ], 

66 numbers: t.Annotated[ 

67 t.List[float], 

68 Argument( 

69 help=_("Additional denominators: n1/n2/n3/.../nN."), show_default=False 

70 ), 

71 ], 

72 ): 

73 """ 

74 Divide the given numbers. 

75 """ 

76 assert issubclass(self.__class__, Command) 

77 res = number1 / number2 

78 for n in numbers: 

79 res /= n 

80 return f"{res:.{self.precision}f}" 

81 

82 @group() 

83 def string( 

84 self, 

85 string: t.Annotated[ 

86 str, Argument(help=_("The string to operate on."), show_default=False) 

87 ], 

88 ): 

89 """ 

90 String operations. 

91 """ 

92 assert issubclass(self.__class__, Command) 

93 self.op_string = string 

94 

95 @string.group() 

96 def case(self): 

97 """ 

98 Case operations. 

99 """ 

100 assert issubclass(self.__class__, Command) 

101 

102 @case.command() 

103 def upper( 

104 self, 

105 begin: t.Annotated[ 

106 int, Argument(help=_("The starting index of the string to operate on.")) 

107 ] = 0, 

108 end: t.Annotated[ 

109 t.Optional[int], 

110 Argument(help=_("The ending index of the string to operate on.")), 

111 ] = None, 

112 ): 

113 """ 

114 Convert the given string to upper case. 

115 """ 

116 assert issubclass(self.__class__, Command) 

117 return f'{self.op_string[0:begin]}{self.op_string[begin:end].upper()}{self.op_string[end:None] if end else ""}' 

118 

119 @case.command() 

120 def lower( 

121 self, 

122 begin: t.Annotated[ 

123 int, Option(help=_("The starting index of the string to operate on.")) 

124 ] = 0, 

125 end: t.Annotated[ 

126 t.Optional[int], 

127 Option(help=_("The ending index of the string to operate on.")), 

128 ] = None, 

129 ): 

130 """ 

131 Convert the given string to upper case. 

132 """ 

133 assert issubclass(self.__class__, Command) 

134 return f'{self.op_string[0:begin]}{self.op_string[begin:end].lower()}{self.op_string[end:None] if end else ""}' 

135 

136 @string.command() 

137 def split(self, sep: str = " "): 

138 """ 

139 Split the given string on the given separator. 

140 """ 

141 assert issubclass(self.__class__, Command) 

142 return " ".join(self.op_string.split(sep))