Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22from collections import Counter, namedtuple 

23from glob import glob 

24import os 

25import re 

26import unittest 

27 

28from lsst.daf.butler.core.utils import findFileResources, getFullTypeName, globToRegex, iterable, Singleton 

29from lsst.daf.butler import Formatter, Registry 

30from lsst.daf.butler import NamedKeyDict, NamedValueSet, StorageClass 

31 

32 

33TESTDIR = os.path.dirname(__file__) 

34 

35 

36class IterableTestCase(unittest.TestCase): 

37 """Tests for `iterable` helper. 

38 """ 

39 

40 def testNonIterable(self): 

41 self.assertEqual(list(iterable(0)), [0, ]) 

42 

43 def testString(self): 

44 self.assertEqual(list(iterable("hello")), ["hello", ]) 

45 

46 def testIterableNoString(self): 

47 self.assertEqual(list(iterable([0, 1, 2])), [0, 1, 2]) 

48 self.assertEqual(list(iterable(["hello", "world"])), ["hello", "world"]) 

49 

50 

51class SingletonTestCase(unittest.TestCase): 

52 """Tests of the Singleton metaclass""" 

53 

54 class IsSingleton(metaclass=Singleton): 

55 def __init__(self): 

56 self.data = {} 

57 self.id = 0 

58 

59 class IsBadSingleton(IsSingleton): 

60 def __init__(self, arg): 

61 """A singleton can not accept any arguments.""" 

62 self.arg = arg 

63 

64 class IsSingletonSubclass(IsSingleton): 

65 def __init__(self): 

66 super().__init__() 

67 

68 def testSingleton(self): 

69 one = SingletonTestCase.IsSingleton() 

70 two = SingletonTestCase.IsSingleton() 

71 

72 # Now update the first one and check the second 

73 one.data["test"] = 52 

74 self.assertEqual(one.data, two.data) 

75 two.id += 1 

76 self.assertEqual(one.id, two.id) 

77 

78 three = SingletonTestCase.IsSingletonSubclass() 

79 self.assertNotEqual(one.id, three.id) 

80 

81 with self.assertRaises(TypeError): 

82 SingletonTestCase.IsBadSingleton(52) 

83 

84 

85class NamedKeyDictTest(unittest.TestCase): 

86 

87 def setUp(self): 

88 self.TestTuple = namedtuple("TestTuple", ("name", "id")) 

89 self.a = self.TestTuple(name="a", id=1) 

90 self.b = self.TestTuple(name="b", id=2) 

91 self.dictionary = {self.a: 10, self.b: 20} 

92 self.names = {self.a.name, self.b.name} 

93 

94 def check(self, nkd): 

95 self.assertEqual(len(nkd), 2) 

96 self.assertEqual(nkd.names, self.names) 

97 self.assertEqual(nkd.keys(), self.dictionary.keys()) 

98 self.assertEqual(list(nkd.values()), list(self.dictionary.values())) 

99 self.assertEqual(list(nkd.items()), list(self.dictionary.items())) 

100 self.assertEqual(list(nkd.byName().values()), list(self.dictionary.values())) 

101 self.assertEqual(list(nkd.byName().keys()), list(nkd.names)) 

102 

103 def testConstruction(self): 

104 self.check(NamedKeyDict(self.dictionary)) 

105 self.check(NamedKeyDict(iter(self.dictionary.items()))) 

106 

107 def testDuplicateNameConstruction(self): 

108 self.dictionary[self.TestTuple(name="a", id=3)] = 30 

109 with self.assertRaises(AssertionError): 

110 NamedKeyDict(self.dictionary) 

111 with self.assertRaises(AssertionError): 

112 NamedKeyDict(iter(self.dictionary.items())) 

113 

114 def testNoNameConstruction(self): 

115 self.dictionary["a"] = 30 

116 with self.assertRaises(AttributeError): 

117 NamedKeyDict(self.dictionary) 

118 with self.assertRaises(AttributeError): 

119 NamedKeyDict(iter(self.dictionary.items())) 

120 

121 def testGetItem(self): 

122 nkd = NamedKeyDict(self.dictionary) 

123 self.assertEqual(nkd["a"], 10) 

124 self.assertEqual(nkd[self.a], 10) 

125 self.assertEqual(nkd["b"], 20) 

126 self.assertEqual(nkd[self.b], 20) 

127 self.assertIn("a", nkd) 

128 self.assertIn(self.b, nkd) 

129 

130 def testSetItem(self): 

131 nkd = NamedKeyDict(self.dictionary) 

132 nkd[self.a] = 30 

133 self.assertEqual(nkd["a"], 30) 

134 nkd["b"] = 40 

135 self.assertEqual(nkd[self.b], 40) 

136 with self.assertRaises(KeyError): 

137 nkd["c"] = 50 

138 with self.assertRaises(AssertionError): 

139 nkd[self.TestTuple("a", 3)] = 60 

140 

141 def testDelItem(self): 

142 nkd = NamedKeyDict(self.dictionary) 

143 del nkd[self.a] 

144 self.assertNotIn("a", nkd) 

145 del nkd["b"] 

146 self.assertNotIn(self.b, nkd) 

147 self.assertEqual(len(nkd), 0) 

148 

149 def testIter(self): 

150 self.assertEqual(set(iter(NamedKeyDict(self.dictionary))), set(self.dictionary)) 

151 

152 def testEquality(self): 

153 nkd = NamedKeyDict(self.dictionary) 

154 self.assertEqual(nkd, self.dictionary) 

155 self.assertEqual(self.dictionary, nkd) 

156 

157 

158class NamedValueSetTest(unittest.TestCase): 

159 

160 def setUp(self): 

161 self.TestTuple = namedtuple("TestTuple", ("name", "id")) 

162 self.a = self.TestTuple(name="a", id=1) 

163 self.b = self.TestTuple(name="b", id=2) 

164 self.c = self.TestTuple(name="c", id=3) 

165 

166 def testConstruction(self): 

167 for arg in ({self.a, self.b}, (self.a, self.b)): 

168 for nvs in (NamedValueSet(arg), NamedValueSet(arg).freeze()): 

169 self.assertEqual(len(nvs), 2) 

170 self.assertEqual(nvs.names, {"a", "b"}) 

171 self.assertCountEqual(nvs, {self.a, self.b}) 

172 self.assertCountEqual(nvs.asMapping().items(), [(self.a.name, self.a), (self.b.name, self.b)]) 

173 

174 def testNoNameConstruction(self): 

175 with self.assertRaises(AttributeError): 

176 NamedValueSet([self.a, "a"]) 

177 

178 def testGetItem(self): 

179 nvs = NamedValueSet({self.a, self.b, self.c}) 

180 self.assertEqual(nvs["a"], self.a) 

181 self.assertEqual(nvs[self.a], self.a) 

182 self.assertEqual(nvs["b"], self.b) 

183 self.assertEqual(nvs[self.b], self.b) 

184 self.assertIn("a", nvs) 

185 self.assertIn(self.b, nvs) 

186 

187 def testEquality(self): 

188 s = {self.a, self.b, self.c} 

189 nvs = NamedValueSet(s) 

190 self.assertEqual(nvs, s) 

191 self.assertEqual(s, nvs) 

192 

193 def checkOperator(self, result, expected): 

194 self.assertIsInstance(result, NamedValueSet) 

195 self.assertEqual(result, expected) 

196 

197 def testOperators(self): 

198 ab = NamedValueSet({self.a, self.b}) 

199 bc = NamedValueSet({self.b, self.c}) 

200 self.checkOperator(ab & bc, {self.b}) 

201 self.checkOperator(ab | bc, {self.a, self.b, self.c}) 

202 self.checkOperator(ab ^ bc, {self.a, self.c}) 

203 self.checkOperator(ab - bc, {self.a}) 

204 

205 

206class TestButlerUtils(unittest.TestCase): 

207 """Tests of the simple utilities.""" 

208 

209 def testTypeNames(self): 

210 # Check types and also an object 

211 tests = [(Formatter, "lsst.daf.butler.core.formatter.Formatter"), 

212 (int, "int"), 

213 (StorageClass, "lsst.daf.butler.core.storageClass.StorageClass"), 

214 (StorageClass(None), "lsst.daf.butler.core.storageClass.StorageClass"), 

215 (Registry, "lsst.daf.butler.registry.Registry")] 

216 

217 for item, typeName in tests: 

218 self.assertEqual(getFullTypeName(item), typeName) 

219 

220 

221class FindFileResourcesTestCase(unittest.TestCase): 

222 

223 def test_getSingleFile(self): 

224 """Test getting a file by its file name.""" 

225 filename = os.path.join(TESTDIR, "config/basic/butler.yaml") 

226 self.assertEqual([filename], findFileResources([filename])) 

227 

228 def test_getAllFiles(self): 

229 """Test getting all the files by not passing a regex.""" 

230 expected = Counter([p for p in glob(os.path.join(TESTDIR, "config", "**"), recursive=True) 

231 if os.path.isfile(p)]) 

232 self.assertNotEqual(len(expected), 0) # verify some files were found 

233 files = Counter(findFileResources([os.path.join(TESTDIR, "config")])) 

234 self.assertEqual(expected, files) 

235 

236 def test_getAllFilesRegex(self): 

237 """Test getting all the files with a regex-specified file ending.""" 

238 expected = Counter(glob(os.path.join(TESTDIR, "config", "**", "*.yaml"), recursive=True)) 

239 self.assertNotEqual(len(expected), 0) # verify some files were found 

240 files = Counter(findFileResources([os.path.join(TESTDIR, "config")], r"\.yaml\b")) 

241 self.assertEqual(expected, files) 

242 

243 def test_multipleInputs(self): 

244 """Test specifying more than one location to find a files.""" 

245 expected = Counter(glob(os.path.join(TESTDIR, "config", "basic", "**", "*.yaml"), recursive=True)) 

246 expected.update(glob(os.path.join(TESTDIR, "config", "templates", "**", "*.yaml"), recursive=True)) 

247 self.assertNotEqual(len(expected), 0) # verify some files were found 

248 files = Counter(findFileResources([os.path.join(TESTDIR, "config", "basic"), 

249 os.path.join(TESTDIR, "config", "templates")], 

250 r"\.yaml\b")) 

251 self.assertEqual(expected, files) 

252 

253 

254class GlobToRegexTestCase(unittest.TestCase): 

255 

256 def testStarInList(self): 

257 """Test that if a one of the items in the expression list is a star 

258 (stand-alone) then no search terms are returned (which implies no 

259 restrictions) """ 

260 self.assertEqual(globToRegex(["foo", "*", "bar"]), []) 

261 

262 def testGlobList(self): 

263 """Test that a list of glob strings converts as expected to a regex and 

264 returns in the expected list. 

265 """ 

266 # test an absolute string 

267 patterns = globToRegex(["bar"]) 

268 self.assertEqual(len(patterns), 1) 

269 self.assertTrue(bool(re.fullmatch(patterns[0], "bar"))) 

270 self.assertIsNone(re.fullmatch(patterns[0], "boz")) 

271 

272 # test leading & trailing wildcard in multiple patterns 

273 patterns = globToRegex(["ba*", "*.fits"]) 

274 self.assertEqual(len(patterns), 2) 

275 # check the "ba*" pattern: 

276 self.assertTrue(bool(re.fullmatch(patterns[0], "bar"))) 

277 self.assertTrue(bool(re.fullmatch(patterns[0], "baz"))) 

278 self.assertIsNone(re.fullmatch(patterns[0], "boz.fits")) 

279 # check the "*.fits" pattern: 

280 self.assertTrue(bool(re.fullmatch(patterns[1], "bar.fits"))) 

281 self.assertTrue(re.fullmatch(patterns[1], "boz.fits")) 

282 self.assertIsNone(re.fullmatch(patterns[1], "boz.hdf5")) 

283 

284 

285if __name__ == "__main__": 285 ↛ 286line 285 didn't jump to line 286, because the condition on line 285 was never true

286 unittest.main()