Coverage for tests/test_utils.py : 30%

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/>.
22import unittest
23from collections import namedtuple
25from lsst.daf.butler.core.utils import iterable, getFullTypeName, Singleton, NamedKeyDict
26from lsst.daf.butler.core.formatter import Formatter
27from lsst.daf.butler import StorageClass
30class IterableTestCase(unittest.TestCase):
31 """Tests for `iterable` helper.
32 """
34 def testNonIterable(self):
35 self.assertEqual(list(iterable(0)), [0, ])
37 def testString(self):
38 self.assertEqual(list(iterable("hello")), ["hello", ])
40 def testIterableNoString(self):
41 self.assertEqual(list(iterable([0, 1, 2])), [0, 1, 2])
42 self.assertEqual(list(iterable(["hello", "world"])), ["hello", "world"])
45class SingletonTestCase(unittest.TestCase):
46 """Tests of the Singleton metaclass"""
48 class IsSingleton(metaclass=Singleton):
49 def __init__(self):
50 self.data = {}
51 self.id = 0
53 class IsBadSingleton(IsSingleton):
54 def __init__(self, arg):
55 """A singleton can not accept any arguments."""
56 self.arg = arg
58 class IsSingletonSubclass(IsSingleton):
59 def __init__(self):
60 super().__init__()
62 def testSingleton(self):
63 one = SingletonTestCase.IsSingleton()
64 two = SingletonTestCase.IsSingleton()
66 # Now update the first one and check the second
67 one.data["test"] = 52
68 self.assertEqual(one.data, two.data)
69 two.id += 1
70 self.assertEqual(one.id, two.id)
72 three = SingletonTestCase.IsSingletonSubclass()
73 self.assertNotEqual(one.id, three.id)
75 with self.assertRaises(TypeError):
76 SingletonTestCase.IsBadSingleton(52)
79class NamedKeyDictTest(unittest.TestCase):
81 def setUp(self):
82 self.TestTuple = namedtuple("TestTuple", ("name", "id"))
83 self.a = self.TestTuple(name="a", id=1)
84 self.b = self.TestTuple(name="b", id=2)
85 self.dictionary = {self.a: 10, self.b: 20}
86 self.names = {self.a.name, self.b.name}
88 def check(self, nkd):
89 self.assertEqual(len(nkd), 2)
90 self.assertEqual(nkd.names, self.names)
91 self.assertEqual(nkd.keys(), self.dictionary.keys())
92 self.assertEqual(list(nkd.values()), list(self.dictionary.values()))
93 self.assertEqual(list(nkd.items()), list(self.dictionary.items()))
94 self.assertEqual(list(nkd.byName().values()), list(self.dictionary.values()))
95 self.assertEqual(list(nkd.byName().keys()), list(nkd.names))
97 def testConstruction(self):
98 self.check(NamedKeyDict(self.dictionary))
99 self.check(NamedKeyDict(iter(self.dictionary.items())))
101 def testDuplicateNameConstruction(self):
102 self.dictionary[self.TestTuple(name="a", id=3)] = 30
103 with self.assertRaises(AssertionError):
104 NamedKeyDict(self.dictionary)
105 with self.assertRaises(AssertionError):
106 NamedKeyDict(iter(self.dictionary.items()))
108 def testNoNameConstruction(self):
109 self.dictionary["a"] = 30
110 with self.assertRaises(AttributeError):
111 NamedKeyDict(self.dictionary)
112 with self.assertRaises(AttributeError):
113 NamedKeyDict(iter(self.dictionary.items()))
115 def testGetItem(self):
116 nkd = NamedKeyDict(self.dictionary)
117 self.assertEqual(nkd["a"], 10)
118 self.assertEqual(nkd[self.a], 10)
119 self.assertEqual(nkd["b"], 20)
120 self.assertEqual(nkd[self.b], 20)
121 self.assertIn("a", nkd)
122 self.assertIn(self.b, nkd)
124 def testSetItem(self):
125 nkd = NamedKeyDict(self.dictionary)
126 nkd[self.a] = 30
127 self.assertEqual(nkd["a"], 30)
128 nkd["b"] = 40
129 self.assertEqual(nkd[self.b], 40)
130 with self.assertRaises(KeyError):
131 nkd["c"] = 50
132 with self.assertRaises(AssertionError):
133 nkd[self.TestTuple("a", 3)] = 60
135 def testDelItem(self):
136 nkd = NamedKeyDict(self.dictionary)
137 del nkd[self.a]
138 self.assertNotIn("a", nkd)
139 del nkd["b"]
140 self.assertNotIn(self.b, nkd)
141 self.assertEqual(len(nkd), 0)
143 def testIter(self):
144 self.assertEqual(set(iter(NamedKeyDict(self.dictionary))), set(self.dictionary))
146 def testEquality(self):
147 nkd = NamedKeyDict(self.dictionary)
148 self.assertEqual(nkd, self.dictionary)
149 self.assertEqual(self.dictionary, nkd)
152class TestButlerUtils(unittest.TestCase):
153 """Tests of the simple utilities."""
155 def testTypeNames(self):
156 # Check types and also an object
157 tests = [(Formatter, "lsst.daf.butler.core.formatter.Formatter"),
158 (int, "int"),
159 (StorageClass, "lsst.daf.butler.core.storageClass.StorageClass"),
160 (StorageClass(None), "lsst.daf.butler.core.storageClass.StorageClass")]
162 for item, typeName in tests:
163 self.assertEqual(getFullTypeName(item), typeName)
166if __name__ == "__main__": 166 ↛ 167line 166 didn't jump to line 167, because the condition on line 166 was never true
167 unittest.main()