Coverage for src/km3pipe/tests/test_tools.py: 98%

155 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-29 03:15 +0000

1# Filename: test_tools.py 

2# pylint: disable=locally-disabled,C0111,R0904,C0103 

3 

4from io import StringIO 

5 

6import numpy as np 

7 

8from km3pipe.testing import TestCase, patch 

9from km3pipe.tools import ( 

10 unpack_nfirst, 

11 split, 

12 namedtuple_with_defaults, 

13 remain_file_pointer, 

14 decamelise, 

15 camelise, 

16 issorted, 

17 lstrip, 

18 chunks, 

19 is_coherent, 

20 istype, 

21 get_jpp_version, 

22) 

23 

24__author__ = "Tamas Gal" 

25__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration." 

26__credits__ = [] 

27__license__ = "MIT" 

28__maintainer__ = "Tamas Gal" 

29__email__ = "tgal@km3net.de" 

30__status__ = "Development" 

31 

32 

33class TestTools(TestCase): 

34 def setUp(self): 

35 self.vecs = np.array( 

36 [ 

37 [0.0, 1.0, 5.0], 

38 [1.0, 1.0, 4.0], 

39 [2.0, 1.0, 3.0], 

40 [3.0, 1.0, 2.0], 

41 [4.0, 1.0, 1.0], 

42 ] 

43 ) 

44 self.v = (1, 2, 3) 

45 self.unit_v = np.array([0.26726124, 0.53452248, 0.80178373]) 

46 self.unit_vecs = np.array( 

47 [ 

48 [0.0, 0.19611614, 0.98058068], 

49 [0.23570226, 0.23570226, 0.94280904], 

50 [0.53452248, 0.26726124, 0.80178373], 

51 [0.80178373, 0.26726124, 0.53452248], 

52 [0.94280904, 0.23570226, 0.23570226], 

53 ] 

54 ) 

55 

56 def test_unpack_nfirst(self): 

57 a_tuple = (1, 2, 3, 4, 5) 

58 a, b, c, rest = unpack_nfirst(a_tuple, 3) 

59 self.assertEqual(1, a) 

60 self.assertEqual(2, b) 

61 self.assertEqual(3, c) 

62 self.assertTupleEqual((4, 5), rest) 

63 

64 def test_unpack_nfirst_callback(self): 

65 a_string = "12345" 

66 a, b, c, rest = unpack_nfirst(a_string, 3, callback=int) 

67 self.assertEqual(1, a) 

68 self.assertEqual(2, b) 

69 self.assertEqual(3, c) 

70 self.assertTupleEqual(("4", "5"), rest) 

71 

72 def test_split_splits_strings(self): 

73 string = "1 2 3 4" 

74 parts = split(string) 

75 self.assertListEqual(["1", "2", "3", "4"], parts) 

76 

77 def test_split_splits_strings_with_separator(self): 

78 string = "1,2,3,4" 

79 parts = split(string, sep=",") 

80 self.assertListEqual(["1", "2", "3", "4"], parts) 

81 

82 def test_split_callback_converts_correctly(self): 

83 string = "1 2 3 4" 

84 parts = split(string, int) 

85 self.assertListEqual([1, 2, 3, 4], parts) 

86 

87 string = "1.0 2.1 3.2 4.3" 

88 parts = split(string, float) 

89 self.assertListEqual([1.0, 2.1, 3.2, 4.3], parts) 

90 

91 def test_namedtuple_with_defaults_initialises_with_none(self): 

92 Node = namedtuple_with_defaults("Node", "val left right") 

93 node = Node() 

94 self.assertIsNone(node.val) 

95 self.assertIsNone(node.left) 

96 self.assertIsNone(node.right) 

97 

98 def test_namedtuple_with_defaults_initialises_with_given_values(self): 

99 Node = namedtuple_with_defaults("Node", "val left right", [1, 2, 3]) 

100 node = Node() 

101 self.assertEqual(1, node.val) 

102 self.assertEqual(2, node.left) 

103 self.assertEqual(3, node.right) 

104 

105 

106class TestRemainFilePointer(TestCase): 

107 def test_remains_file_pointer_in_function(self): 

108 dummy_file = StringIO("abcdefg") 

109 

110 @remain_file_pointer 

111 def seek_into_file(file_obj): 

112 file_obj.seek(1, 0) 

113 

114 dummy_file.seek(2, 0) 

115 self.assertEqual(2, dummy_file.tell()) 

116 seek_into_file(dummy_file) 

117 self.assertEqual(2, dummy_file.tell()) 

118 

119 def test_remains_file_pointer_and_return_value_in_function(self): 

120 dummy_file = StringIO("abcdefg") 

121 

122 @remain_file_pointer 

123 def seek_into_file(file_obj): 

124 file_obj.seek(1, 0) 

125 return 1 

126 

127 dummy_file.seek(2, 0) 

128 self.assertEqual(2, dummy_file.tell()) 

129 return_value = seek_into_file(dummy_file) 

130 self.assertEqual(2, dummy_file.tell()) 

131 self.assertEqual(1, return_value) 

132 

133 def test_remains_file_pointer_in_class_method(self): 

134 class FileSeekerClass(object): 

135 def __init__(self): 

136 self.dummy_file = StringIO("abcdefg") 

137 

138 @remain_file_pointer 

139 def seek_into_file(self, file_obj): 

140 file_obj.seek(1, 0) 

141 

142 fileseeker = FileSeekerClass() 

143 fileseeker.dummy_file.seek(2, 0) 

144 self.assertEqual(2, fileseeker.dummy_file.tell()) 

145 fileseeker.seek_into_file(fileseeker.dummy_file) 

146 self.assertEqual(2, fileseeker.dummy_file.tell()) 

147 

148 def test_remains_file_pointer_and_return_value_in_class_method(self): 

149 class FileSeekerClass(object): 

150 def __init__(self): 

151 self.dummy_file = StringIO("abcdefg") 

152 

153 @remain_file_pointer 

154 def seek_into_file(self, file_obj): 

155 file_obj.seek(1, 0) 

156 return 1 

157 

158 fileseeker = FileSeekerClass() 

159 fileseeker.dummy_file.seek(2, 0) 

160 self.assertEqual(2, fileseeker.dummy_file.tell()) 

161 return_value = fileseeker.seek_into_file(fileseeker.dummy_file) 

162 self.assertEqual(2, fileseeker.dummy_file.tell()) 

163 self.assertEqual(1, return_value) 

164 

165 

166class TestCamelCaseConverter(TestCase): 

167 def test_decamelise(self): 

168 text = "TestCase" 

169 self.assertEqual("test_case", decamelise(text)) 

170 text = "TestCaseXYZ" 

171 self.assertEqual("test_case_xyz", decamelise(text)) 

172 text = "1TestCase" 

173 self.assertEqual("1_test_case", decamelise(text)) 

174 text = "test_case" 

175 self.assertEqual("test_case", decamelise(text)) 

176 

177 def test_camelise(self): 

178 text = "camel_case" 

179 self.assertEqual("CamelCase", camelise(text)) 

180 text = "camel_case" 

181 self.assertEqual("camelCase", camelise(text, capital_first=False)) 

182 

183 

184class TestMisc(TestCase): 

185 def test_issorted(self): 

186 assert issorted([1, 2, 3]) 

187 assert not issorted([2, 3, 1]) 

188 

189 def test_is_coherent(self): 

190 assert is_coherent([1, 2, 3, 4, 5]) 

191 assert not is_coherent([1, 3, 4, 5]) 

192 

193 

194class TestLstrip(TestCase): 

195 def test_lstrip(self): 

196 text = """ 

197 a 

198 b 

199 c 

200 d 

201 e 

202 """ 

203 self.assertEqual("a\nb\nc\nd\ne\n", lstrip(text)) 

204 

205 def test_lstrip_on_single_line(self): 

206 text = " a " 

207 self.assertEqual(text.lstrip(), lstrip(text)) 

208 

209 

210class TestChunks(TestCase): 

211 def test_chunks(self): 

212 l = [1, 2, 3, 4, 5, 6, 7, 8, 9] 

213 self.assertEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]], list(chunks(l, 3))) 

214 self.assertEqual([[1, 2, 3, 4], [5, 6, 7, 8], [9]], list(chunks(l, 4))) 

215 

216 

217class TestIstype(TestCase): 

218 def test_a_type(self): 

219 a = [1] 

220 assert istype(a, "list") 

221 

222 def test_another_type(self): 

223 b = "string" 

224 if isinstance(b, str): 

225 assert istype(b, "str") 

226 else: 

227 assert istype(b, "unicode") 

228 

229 

230class TestJppRevision(TestCase): 

231 @patch("subprocess.check_output") 

232 def test_version(self, co_mock): 

233 co_mock.return_value = b"version: 8519\nname space: KM3NET\n" 

234 assert "8519" == get_jpp_version() 

235 

236 def test_version(self): 

237 assert get_jpp_version(via_command="a_non_existing_command") is None