1import unittest 2 3from .pathtree import PathTree 4from pprint import pprint 5 6class PathTreeTest(unittest.TestCase): 7 def test_set_depth_1(self): 8 pt = PathTree() 9 pt['/a'] = 1 10 11 def test_set_depth_2(self): 12 pt = PathTree() 13 pt['/a/b'] = 2 14 15 def test_get_no_key(self): 16 pt = PathTree() 17 with self.assertRaises(KeyError): 18 pt['/foo'] 19 20 def test_get_depth_1(self): 21 pt = PathTree() 22 pt['/a'] = 1 23 self.assertEqual(1, pt['/a']) 24 25 def test_get_depth_2(self): 26 pt = PathTree() 27 pt['/a/b'] = 2 28 self.assertEqual(set(['/a', '/a/b']), set(pt.keys())) 29 self.assertEqual(2, pt['/a/b']) 30 31 def test_get_default(self): 32 self.assertEquals(1, PathTree().get('/a', 1)) 33 34 def test_get_present(self): 35 pt = PathTree() 36 pt['/a'] = 1 37 self.assertEquals(1, pt.get('/a')) 38 39 def test_set_2_depth_1(self): 40 pt = PathTree() 41 pt['/a'] = 1.1 42 pt['/b'] = 1.2 43 self.assertEqual(set(['/a', '/b']), set(pt.keys())) 44 self.assertEqual(1.1, pt['/a']) 45 self.assertEqual(1.2, pt['/b']) 46 47 def test_set_2_depth_2_common_parent(self): 48 pt = PathTree() 49 pt['/a/b'] = 1.1 50 pt['/a/c'] = 1.2 51 self.assertEqual(set(['/a', '/a/b', '/a/c']), set(pt.keys())) 52 self.assertEqual(1.1, pt['/a/b']) 53 self.assertEqual(1.2, pt['/a/c']) 54 55 def test_set_2_depth_2_separate_parent(self): 56 pt = PathTree() 57 pt['/a/b'] = 1.1 58 pt['/b/c'] = 1.2 59 self.assertEqual(set(['/a', '/b', '/a/b', '/b/c']), set(pt.keys())) 60 self.assertEqual(1.1, pt['/a/b']) 61 self.assertEqual(1.2, pt['/b/c']) 62 63 def test_dumpd_empty(self): 64 pt = PathTree() 65 self.assertEquals(dict(), pt.dumpd()) 66 67 def test_dumpd_populated(self): 68 pt = PathTree() 69 pt['/a/b'] = { 1 : 1.1 } 70 pt['/b/c'] = { 2 : 1.2 } 71 dump = pt.dumpd() 72 self.assertEquals(set(['/']), set(dump.keys())) 73 self.assertEquals(set(['a', 'b']), set(dump['/'].keys())) 74 self.assertEquals(set(['b']), set(dump['/']['a'].keys())) 75 self.assertEquals(set(['c']), set(dump['/']['b'].keys())) 76 77 def test_del_set_1_depth_1(self): 78 pt = PathTree() 79 pt['/a'] = 1 80 del pt['/a'] 81 self.assertEquals(0, len(pt.keys())) 82 83 def test_del_set_2_depth_1(self): 84 pt = PathTree() 85 pt['/a'] = 1 86 pt['/b'] = 2 87 del pt['/a'] 88 self.assertEquals(set(['/b']), set(pt.keys())) 89 90 def test_del_set_2_depth_2(self): 91 pt = PathTree() 92 pt['/a/b'] = 1 93 pt['/b/c'] = 2 94 del pt['/a/b'] 95 self.assertEquals(set(['/b/c', '/b']), set(pt.keys())) 96 97 def test_setdefault_present(self): 98 pt = PathTree() 99 pt['/a'] = 1 100 self.assertEquals(1, pt.setdefault('/a', 2)) 101 102 def test_setdefault_absent(self): 103 self.assertEquals(1, PathTree().setdefault('/a', 1)) 104 105 def test_del_no_key(self): 106 with self.assertRaises(KeyError): 107 del PathTree()['/a'] 108 109 def test_values_1(self): 110 pt = PathTree() 111 pt['/a'] = 1 112 self.assertEquals(set([1]), set(pt.values())) 113 114 def test_values_2(self): 115 pt = PathTree() 116 pt['/a'] = 1 117 pt['/b'] = 2 118 self.assertEquals(set([1, 2]), set(pt.values())) 119 120 def test_items_1(self): 121 pt = PathTree() 122 pt['/a'] = 1 123 self.assertEquals(set([('/a', 1)]), set(pt.items())) 124 125 def test_items_2(self): 126 pt = PathTree() 127 pt['/a'] = 1 128 pt['/b'] = 2 129 self.assertEquals(set([('/a', 1), ('/b', 2)]), set(pt.items())) 130 131 def test_items_depth_2(self): 132 pt = PathTree() 133 pt['/a/b'] = 1 134 self.assertEquals(set([('/a', None), ('/a/b', 1)]), set(pt.items())) 135 136 def test_dataitems_0(self): 137 pt = PathTree() 138 self.assertEquals(set(), set(pt.dataitems())) 139 140 def test_dataitems_1(self): 141 pt = PathTree() 142 pt['/b'] = 1 143 self.assertEquals(set([('/b', 1)]), set(pt.dataitems())) 144 145 def test_dataitems_2(self): 146 pt = PathTree() 147 pt['/a'] = 1 148 pt['/b/c'] = 2 149 self.assertEquals(set([('/a', 1), ('/b/c', 2)]), set(pt.dataitems())) 150 151 def test_get_children(self): 152 pt = PathTree() 153 pt['/a'] = 1 154 self.assertEquals(set(['a']), set(pt.get_children('/'))) 155 self.assertEquals(set(), set(pt.get_children('/a'))) 156 157 def test_get_children_nested(self): 158 pt = PathTree() 159 pt['/a/b'] = 1 160 self.assertEquals(set(['a']), set(pt.get_children('/'))) 161 self.assertEquals(set(['b']), set(pt.get_children('/a'))) 162 self.assertEquals(set(), set(pt.get_children('/a/b'))) 163 164 def test_demote_1(self): 165 pt = PathTree() 166 pt['/a'] = 1 167 self.assertEquals([1], pt.values()) 168 pt.demote('/a') 169 self.assertEquals([None], pt.values()) 170 171 def test_demote_2(self): 172 pt = PathTree() 173 pt['/a'] = 1 174 pt['/b'] = 2 175 self.assertEquals(set([1, 2]), set(pt.values())) 176 pt.demote('/a') 177 self.assertEquals(set([None, 2]), set(pt.values())) 178 179 def test_demote_nested(self): 180 pt = PathTree() 181 pt['/a'] = 1 182 pt['/a/b'] = 2 183 self.assertEquals(set([1, 2]), set(pt.values())) 184 pt.demote('/a/b') 185 self.assertEquals(set([1, None]), set(pt.values())) 186 187 def test_iter(self): 188 pt = PathTree() 189 pt['/a'] = 1 190 i = iter(pt) 191 k, v = next(i) 192 self.assertEquals('/a', k) 193 self.assertEquals(1, v) 194 with self.assertRaises(StopIteration): 195 next(i) 196 197 def test_iter_2(self): 198 pt = PathTree() 199 pt['/a'] = 1 200 pt['/b'] = 2 201 i = iter(pt) 202 k, v = next(i) 203 self.assertEquals('/a', k) 204 self.assertEquals(1, v) 205 k, v = next(i) 206 self.assertEquals('/b', k) 207 self.assertEquals(2, v) 208 with self.assertRaises(StopIteration): 209 next(i) 210 211 def test_iter_2_nested(self): 212 pt = PathTree() 213 pt['/a'] = 1 214 pt['/a/b'] = 2 215 i = iter(pt) 216 k, v = next(i) 217 self.assertEquals('/a', k) 218 self.assertEquals(1, v) 219 k, v = next(i) 220 self.assertEquals('/a/b', k) 221 self.assertEquals(2, v) 222 with self.assertRaises(StopIteration): 223 next(i) 224 225 def test_keys_2_nested_depth_1(self): 226 pt = PathTree() 227 pt['/a'] = 1 228 pt['/a/b'] = 2 229 self.assertEquals(set(['/a']), set(pt.keys(depth=1))) 230 231 def test_values_2_nested_depth_1(self): 232 pt = PathTree() 233 pt['/a'] = 1 234 pt['/a/b'] = 2 235 self.assertEquals(set([1]), set(pt.values(depth=1))) 236 237 def test_items_2_nested_depth_1(self): 238 pt = PathTree() 239 pt['/a'] = 1 240 pt['/a/b'] = 2 241 self.assertEquals(set([('/a', 1)]), set(pt.items(depth=1))) 242 243 def test_dataitems_2_nested_depth_1(self): 244 pt = PathTree() 245 pt['/a'] = 1 246 pt['/a/b'] = 2 247 pt['/b'] = None 248 pt['/b/c'] = 1 249 self.assertEquals(set([('/a', 1)]), set(pt.dataitems(depth=1))) 250 251 def test_keys_2_nested_subtree(self): 252 pt = PathTree() 253 pt['/a'] = 1 254 pt['/a/b'] = 2 255 pt['/b'] = 3 256 self.assertEquals(set(['/a/b']), set(pt.keys(subtree='/a'))) 257 258 def test_values_2_nested_subtree(self): 259 pt = PathTree() 260 pt['/a'] = 1 261 pt['/a/b'] = 2 262 pt['/b'] = 3 263 self.assertEquals(set([2]), set(pt.values(subtree='/a'))) 264 265 def test_items_2_nested_subtree(self): 266 pt = PathTree() 267 pt['/a'] = 1 268 pt['/a/b'] = 2 269 pt['/b'] = 3 270 self.assertEquals(set([('/a/b', 2)]), set(pt.items(subtree='/a'))) 271 272 def test_dataitems_2_nested_subtree(self): 273 pt = PathTree() 274 pt['/a'] = 1 275 pt['/a/b'] = 2 276 pt['/a/c'] = None 277 pt['/b'] = 3 278 self.assertEquals(set([('/a/b', 2)]), set(pt.dataitems(subtree='/a'))) 279 280 def test_keys_3_nested_subtree_depth_1(self): 281 pt = PathTree() 282 pt['/a'] = 1 283 pt['/a/b'] = 2 284 pt['/a/b/c'] = 3 285 pt['/b'] = 4 286 self.assertEquals(set(['/a/b']), set(pt.keys(subtree='/a', depth=1))) 287 288 def test_values_3_nested_subtree_depth_1(self): 289 pt = PathTree() 290 pt['/a'] = 1 291 pt['/a/b'] = 2 292 pt['/a/b/c'] = 3 293 pt['/b'] = 4 294 self.assertEquals(set([2]), set(pt.values(subtree='/a', depth=1))) 295 296 def test_items_3_nested_subtree_depth_1(self): 297 pt = PathTree() 298 pt['/a'] = 1 299 pt['/a/b'] = 2 300 pt['/a/b/c'] = 3 301 pt['/b'] = 4 302 self.assertEquals(set([('/a/b', 2)]), set(pt.items(subtree='/a', depth=1))) 303 304 def test_items_3_nested_subtree_depth_1(self): 305 pt = PathTree() 306 pt['/a'] = 1 307 pt['/a/b'] = 2 308 pt['/a/b/c'] = 3 309 pt['/a/c'] = None 310 pt['/b'] = 4 311 self.assertEquals(set([('/a/b', 2)]), set(pt.dataitems(subtree='/a', depth=1))) 312