Coverage for tests/test_utils.py: 27%
Shortcuts 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
Shortcuts 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/>.
22from collections import namedtuple
23import os
24import re
25import unittest
27from lsst.daf.butler.core.utils import globToRegex
28from lsst.daf.butler import NamedKeyDict, NamedValueSet
30TESTDIR = os.path.dirname(__file__)
33class NamedKeyDictTest(unittest.TestCase):
35 def setUp(self):
36 self.TestTuple = namedtuple("TestTuple", ("name", "id"))
37 self.a = self.TestTuple(name="a", id=1)
38 self.b = self.TestTuple(name="b", id=2)
39 self.dictionary = {self.a: 10, self.b: 20}
40 self.names = {self.a.name, self.b.name}
42 def check(self, nkd):
43 self.assertEqual(len(nkd), 2)
44 self.assertEqual(nkd.names, self.names)
45 self.assertEqual(nkd.keys(), self.dictionary.keys())
46 self.assertEqual(list(nkd.values()), list(self.dictionary.values()))
47 self.assertEqual(list(nkd.items()), list(self.dictionary.items()))
48 self.assertEqual(list(nkd.byName().values()), list(self.dictionary.values()))
49 self.assertEqual(list(nkd.byName().keys()), list(nkd.names))
51 def testConstruction(self):
52 self.check(NamedKeyDict(self.dictionary))
53 self.check(NamedKeyDict(iter(self.dictionary.items())))
55 def testDuplicateNameConstruction(self):
56 self.dictionary[self.TestTuple(name="a", id=3)] = 30
57 with self.assertRaises(AssertionError):
58 NamedKeyDict(self.dictionary)
59 with self.assertRaises(AssertionError):
60 NamedKeyDict(iter(self.dictionary.items()))
62 def testNoNameConstruction(self):
63 self.dictionary["a"] = 30
64 with self.assertRaises(AttributeError):
65 NamedKeyDict(self.dictionary)
66 with self.assertRaises(AttributeError):
67 NamedKeyDict(iter(self.dictionary.items()))
69 def testGetItem(self):
70 nkd = NamedKeyDict(self.dictionary)
71 self.assertEqual(nkd["a"], 10)
72 self.assertEqual(nkd[self.a], 10)
73 self.assertEqual(nkd["b"], 20)
74 self.assertEqual(nkd[self.b], 20)
75 self.assertIn("a", nkd)
76 self.assertIn(self.b, nkd)
78 def testSetItem(self):
79 nkd = NamedKeyDict(self.dictionary)
80 nkd[self.a] = 30
81 self.assertEqual(nkd["a"], 30)
82 nkd["b"] = 40
83 self.assertEqual(nkd[self.b], 40)
84 with self.assertRaises(KeyError):
85 nkd["c"] = 50
86 with self.assertRaises(AssertionError):
87 nkd[self.TestTuple("a", 3)] = 60
89 def testDelItem(self):
90 nkd = NamedKeyDict(self.dictionary)
91 del nkd[self.a]
92 self.assertNotIn("a", nkd)
93 del nkd["b"]
94 self.assertNotIn(self.b, nkd)
95 self.assertEqual(len(nkd), 0)
97 def testIter(self):
98 self.assertEqual(set(iter(NamedKeyDict(self.dictionary))), set(self.dictionary))
100 def testEquality(self):
101 nkd = NamedKeyDict(self.dictionary)
102 self.assertEqual(nkd, self.dictionary)
103 self.assertEqual(self.dictionary, nkd)
106class NamedValueSetTest(unittest.TestCase):
108 def setUp(self):
109 self.TestTuple = namedtuple("TestTuple", ("name", "id"))
110 self.a = self.TestTuple(name="a", id=1)
111 self.b = self.TestTuple(name="b", id=2)
112 self.c = self.TestTuple(name="c", id=3)
114 def testConstruction(self):
115 for arg in ({self.a, self.b}, (self.a, self.b)):
116 for nvs in (NamedValueSet(arg), NamedValueSet(arg).freeze()):
117 self.assertEqual(len(nvs), 2)
118 self.assertEqual(nvs.names, {"a", "b"})
119 self.assertCountEqual(nvs, {self.a, self.b})
120 self.assertCountEqual(nvs.asMapping().items(), [(self.a.name, self.a), (self.b.name, self.b)])
122 def testNoNameConstruction(self):
123 with self.assertRaises(AttributeError):
124 NamedValueSet([self.a, "a"])
126 def testGetItem(self):
127 nvs = NamedValueSet({self.a, self.b, self.c})
128 self.assertEqual(nvs["a"], self.a)
129 self.assertEqual(nvs[self.a], self.a)
130 self.assertEqual(nvs["b"], self.b)
131 self.assertEqual(nvs[self.b], self.b)
132 self.assertIn("a", nvs)
133 self.assertIn(self.b, nvs)
135 def testEquality(self):
136 s = {self.a, self.b, self.c}
137 nvs = NamedValueSet(s)
138 self.assertEqual(nvs, s)
139 self.assertEqual(s, nvs)
141 def checkOperator(self, result, expected):
142 self.assertIsInstance(result, NamedValueSet)
143 self.assertEqual(result, expected)
145 def testOperators(self):
146 ab = NamedValueSet({self.a, self.b})
147 bc = NamedValueSet({self.b, self.c})
148 self.checkOperator(ab & bc, {self.b})
149 self.checkOperator(ab | bc, {self.a, self.b, self.c})
150 self.checkOperator(ab ^ bc, {self.a, self.c})
151 self.checkOperator(ab - bc, {self.a})
154class GlobToRegexTestCase(unittest.TestCase):
156 def testStarInList(self):
157 """Test that if a one of the items in the expression list is a star
158 (stand-alone) then ``...`` is returned (which implies no restrictions)
159 """
160 self.assertIs(globToRegex(["foo", "*", "bar"]), ...)
162 def testGlobList(self):
163 """Test that a list of glob strings converts as expected to a regex and
164 returns in the expected list.
165 """
166 # test an absolute string
167 patterns = globToRegex(["bar"])
168 self.assertEqual(len(patterns), 1)
169 self.assertTrue(bool(re.fullmatch(patterns[0], "bar")))
170 self.assertIsNone(re.fullmatch(patterns[0], "boz"))
172 # test leading & trailing wildcard in multiple patterns
173 patterns = globToRegex(["ba*", "*.fits"])
174 self.assertEqual(len(patterns), 2)
175 # check the "ba*" pattern:
176 self.assertTrue(bool(re.fullmatch(patterns[0], "bar")))
177 self.assertTrue(bool(re.fullmatch(patterns[0], "baz")))
178 self.assertIsNone(re.fullmatch(patterns[0], "boz.fits"))
179 # check the "*.fits" pattern:
180 self.assertTrue(bool(re.fullmatch(patterns[1], "bar.fits")))
181 self.assertTrue(re.fullmatch(patterns[1], "boz.fits"))
182 self.assertIsNone(re.fullmatch(patterns[1], "boz.hdf5"))
185if __name__ == "__main__": 185 ↛ 186line 185 didn't jump to line 186, because the condition on line 185 was never true
186 unittest.main()