8
# Next imports are only necessary for this test suite
9
from tables import Group, Leaf, Table, Array
11
from test_all import verbose, heavy
13
#heavy = 1 # Uncomment this only for test purposes!
16
class Record(IsDescription):
17
var1 = StringCol(length=4) # 4-character String
18
var2 = IntCol() # integer
19
var3 = Int16Col() # short integer
20
var4 = FloatCol() # double (double-precision)
21
var5 = Float32Col() # float (single-precision)
23
class TreeTestCase(unittest.TestCase):
25
title = "This is the table title"
30
# Create a temporary file
31
self.file = tempfile.mktemp(".h5")
32
# Create an instance of HDF5 Table
33
self.h5file = openFile(self.file, self.mode, self.title)
37
def populateFile(self):
38
group = self.h5file.root
40
maxint = 2147483647 # (2 ** 31 - 1)
43
table = self.h5file.createTable(group, 'table'+str(j), Record,
46
expectedrows = self.expectedrows)
47
# Get the record object associated with the new table
50
for i in xrange(self.expectedrows):
51
d['var1'] = '%04d' % (self.expectedrows - i)
53
d['var3'] = i % maxshort
56
d.append() # This injects the Record values
57
# Flush the buffer for this table
60
# Create a couple of arrays in each group
61
var1List = [ x['var1'] for x in table.iterrows() ]
62
var4List = [ x['var4'] for x in table.iterrows() ]
64
self.h5file.createArray(group, 'var1', var1List, "1")
65
self.h5file.createArray(group, 'var4', var4List, "4")
67
# Create a new group (descendant of group)
68
group2 = self.h5file.createGroup(group, 'group'+str(j))
69
# Iterate over this new group (group2)
74
if self.h5file.isopen:
79
#----------------------------------------
81
def test00_getNode(self):
82
"Checking the File.getNode() with string node names"
86
print "Running %s.test00_getNode..." % self.__class__.__name__
88
self.h5file = openFile(self.file, "r")
89
nodelist = ['/', '/table0', '/group0/var1', '/group0/group1/var4']
92
object = self.h5file.getNode(node)
93
nodenames.append(object._v_pathname)
95
assert nodenames == nodelist
98
print "getNode(pathname) test passed"
100
nodegroups = ['/', '/group0', '/group0/group1', '/group0/group1/group2']
101
nodenames = ['var1', 'var4']
103
for group in nodegroups:
104
for name in nodenames:
106
object = self.h5file.getNode(group, name)
110
nodepaths.append(object._v_pathname)
112
assert nodepaths == ['/var1', '/var4',
113
'/group0/var1', '/group0/var4',
114
'/group0/group1/var1', '/group0/group1/var4',
118
print "getNode(groupname, name) test passed"
121
nodelist = ['/', '/group0', '/group0/group1', '/group0/group1/group2',
125
#warnings.filterwarnings("error", category=UserWarning)
126
for node in nodelist:
128
object = self.h5file.getNode(node, classname = 'Group')
131
(type, value, traceback) = sys.exc_info()
132
print "\nGreat!, the next LookupError was catched!"
135
nodenames.append(object._v_pathname)
136
groupobjects.append(object)
138
assert nodenames == ['/', '/group0', '/group0/group1',
139
'/group0/group1/group2',
143
print "getNode(groupname, classname='Group') test passed"
146
#warnings.filterwarnings("default", category=UserWarning)
148
nodenames = ['var1', 'var4']
150
for group in groupobjects:
151
for name in nodenames:
153
object = self.h5file.getNode(group, name, 'Array')
157
nodearrays.append(object._v_pathname)
159
assert nodearrays == ['/var1', '/var4',
160
'/group0/var1', '/group0/var4',
161
'/group0/group1/var1', '/group0/group1/var4',
165
print "getNode(groupobject, name, classname='Array') test passed"
168
def test01_getNodeClass(self):
169
"Checking the File.getNode() with instances"
172
print '\n', '-=' * 30
173
print "Running %s.test01_getNodeClass..." % self.__class__.__name__
175
self.h5file = openFile(self.file, "r")
176
# This tree ways of getNode usage should return a table instance
177
table = self.h5file.getNode("/group0/table1")
178
assert isinstance(table, Table)
179
table = self.h5file.getNode("/group0", "table1")
180
assert isinstance(table, Table)
181
table = self.h5file.getNode(self.h5file.root.group0, "table1")
182
assert isinstance(table, Table)
184
# This should return an array instance
185
arr = self.h5file.getNode("/group0/var1")
186
assert isinstance(arr, Array)
187
assert isinstance(arr, Leaf)
190
group = self.h5file.getNode("/group0", "group1", "Group")
191
assert isinstance(group, Group)
194
def test02_listNodes(self):
195
"Checking the File.listNodes() method"
198
print '\n', '-=' * 30
199
print "Running %s.test02_listNodes..." % self.__class__.__name__
201
# Made the warnings to raise an error
202
#warnings.filterwarnings("error", category=UserWarning)
203
self.h5file = openFile(self.file, "r")
204
nodelist = ['/', '/group0', '/group0/table1', '/group0/group1/group2',
208
for node in nodelist:
210
objectlist = self.h5file.listNodes(node)
214
objects.extend(objectlist)
215
for object in objectlist:
216
nodenames.append(object._v_pathname)
218
assert nodenames == ['/group0', '/table0', '/var1', '/var4',
219
'/group0/group1', '/group0/table1',
220
'/group0/var1', '/group0/var4',
224
print "listNodes(pathname) test passed"
229
objectlist = self.h5file.listNodes(node)
233
for object in objectlist:
234
nodenames.append(object._v_pathname)
236
assert nodenames == ['/group0/group1', '/group0/table1',
237
'/group0/var1', '/group0/var4',
238
'/group0/group1/group2', '/group0/group1/table2',
239
'/group0/group1/var1', '/group0/group1/var4',
243
print "listNodes(groupobject) test passed"
248
objectlist = self.h5file.listNodes(node, 'Leaf')
251
(type, value, traceback) = sys.exc_info()
252
print "\nGreat!, the next LookupError was catched!"
255
for object in objectlist:
256
nodenames.append(object._v_pathname)
258
assert nodenames == ['/group0/table1',
259
'/group0/var1', '/group0/var4',
260
'/group0/group1/table2',
261
'/group0/group1/var1', '/group0/group1/var4',
265
print "listNodes(groupobject, classname = 'Leaf') test passed"
270
objectlist = self.h5file.listNodes(node, 'Table')
273
(type, value, traceback) = sys.exc_info()
274
print "\nGreat!, the next LookupError was catched!"
277
for object in objectlist:
278
nodenames.append(object._v_pathname)
280
assert nodenames == ['/group0/table1',
281
'/group0/group1/table2',
285
print "listNodes(groupobject, classname = 'Table') test passed"
288
#warnings.filterwarnings("default", category=UserWarning)
290
def test03_TraverseTree(self):
291
"Checking the File.walkGroups() method"
294
print '\n', '-=' * 30
295
print "Running %s.test03_TraverseTree..." % self.__class__.__name__
297
self.h5file = openFile(self.file, "r")
301
for group in self.h5file.walkGroups():
302
groups.append(group._v_pathname)
303
for table in self.h5file.listNodes(group, 'Table'):
304
tables.append(table._v_pathname)
305
for arr in self.h5file.listNodes(group, 'Array'):
306
arrays.append(arr._v_pathname)
308
assert groups == ["/", "/group0", "/group0/group1",
309
"/group0/group1/group2"]
311
assert tables == ["/table0", "/group0/table1", "/group0/group1/table2"]
313
assert arrays == ['/var1', '/var4',
314
'/group0/var1', '/group0/var4',
315
'/group0/group1/var1', '/group0/group1/var4']
318
print "walkGroups() test passed"
323
for group in self.h5file.walkGroups("/group0/group1"):
324
groups.append(group._v_pathname)
325
for table in self.h5file.listNodes(group, 'Table'):
326
tables.append(table._v_pathname)
327
for arr in self.h5file.listNodes(group, 'Array'):
328
arrays.append(arr._v_pathname)
330
assert groups == ["/group0/group1",
331
"/group0/group1/group2"]
333
assert tables == ["/group0/group1/table2"]
335
assert arrays == ['/group0/group1/var1', '/group0/group1/var4']
338
print "walkGroups(pathname) test passed"
340
def test04_walkNodes(self):
341
"Checking File.walkNodes"
344
print '\n', '-=' * 30
345
print "Running %s.test04_walkNodes..." % self.__class__.__name__
347
self.h5file = openFile(self.file, "r")
352
for group in self.h5file.walkNodes(classname="Group"):
353
groups.append(group._v_pathname)
354
for table in group._f_walkNodes(classname='Table'):
355
tables.append(table._v_pathname)
357
# Test the recursivity
358
for table in self.h5file.root._f_walkNodes('Table', recursive=1):
359
tables2.append(table._v_pathname)
361
for arr in self.h5file.walkNodes(classname='Array'):
362
arrays.append(arr._v_pathname)
364
assert groups == ["/", "/group0", "/group0/group1",
365
"/group0/group1/group2"]
366
assert tables == ["/table0", "/group0/table1",
367
"/group0/group1/table2"]
368
assert tables2 == ["/table0", "/group0/table1",
369
"/group0/group1/table2"]
370
assert arrays == ['/var1', '/var4',
371
'/group0/var1', '/group0/var4',
372
'/group0/group1/var1', '/group0/group1/var4']
375
print "File.__iter__() and Group.__iter__ test passed"
380
for group in self.h5file.walkNodes("/group0/group1", classname="Group"):
381
groups.append(group._v_pathname)
382
for table in group._f_walkNodes('Table'):
383
tables.append(table._v_pathname)
384
for arr in self.h5file.walkNodes(group, 'Array'):
385
arrays.append(arr._v_pathname)
387
assert groups == ["/group0/group1",
388
"/group0/group1/group2"]
390
assert tables == ["/group0/group1/table2"]
392
assert arrays == ['/group0/group1/var1', '/group0/group1/var4']
395
print "walkNodes(pathname, classname) test passed"
398
class DeepTreeTestCase(unittest.TestCase):
399
"""Checks for maximum deepest level in PyTables trees.
401
Right now, the maximum depth for object tree is determined by the
402
maximum recursion level offered by Python (which for my platform
403
is a number between 768 and 1024).
406
def test00_deepTree(self):
407
"Checking creation of large depth object tree Variable"
409
# Here we put a more conservative limit to deal with more platforms
410
# With maxdepth = 512 this test would take less than 20 MB
411
# of main memory to run, which is quite reasonable nowadays.
412
# With maxdepth = 1024 this test will take over 40 MB.
414
maxdepth = 1024 # Only for big machines!
416
maxdepth = 256 # This should be safe for most machines
419
print '\n', '-=' * 30
420
print "Running %s.test00_deepTree..." % \
421
self.__class__.__name__
422
print "Maximum depth tested :", maxdepth
424
# Open a new empty HDF5 file
425
file = tempfile.mktemp(".h5")
427
fileh = openFile(file, mode = "w")
431
print "Depth writing progress: ",
432
# Iterate until maxdepth
433
for depth in range(maxdepth):
434
# Save it on the HDF5 file
436
print "%3d," % (depth),
438
#fileh.createArray(group, 'array', a, "depth: %d" % depth)
439
fileh.createArray(pathname, 'array', a, "depth: %d" % depth)
440
#group = fileh.createGroup(group, 'group' + str(depth))
441
group = fileh.createGroup(pathname, 'group' + str(depth))
442
pathname = group._v_pathname
446
# Open the previous HDF5 file in read-only mode
447
fileh = openFile(file, mode = "r")
451
print "\nDepth reading progress: ",
452
# Get the metadata on the previosly saved arrays
453
for depth in range(maxdepth):
455
print "%3d," % (depth),
456
# Create an array for later comparison
458
# Get the actual array
459
b = group.array.read()
460
# Arrays a and b must be equal
462
# Iterate over the next group
463
group = fileh.getNode(pathname, 'group' + str(depth))
464
#group = fileh.getNode(group, 'group' + str(depth))
465
pathname = group._v_pathname
467
print # This flush the stdout buffer
471
# Then, delete the file
474
class WideTreeTestCase(unittest.TestCase):
475
"""Checks for maximum number of children for a Group.
479
def test00_Leafs(self):
480
"""Checking creation of large number of leafs (1024) per group
482
Variable 'maxchildren' controls this check. PyTables support
483
up to 4096 children per group, but this would take too much
484
memory (up to 64 MB) for testing purposes (may be we can add a
485
test for big platforms). A 1024 children run takes up to 30 MB.
486
A 512 children test takes around 25 MB.
496
print '\n', '-=' * 30
497
print "Running %s.test00_wideTree..." % \
498
self.__class__.__name__
499
print "Maximum number of children tested :", maxchildren
500
# Open a new empty HDF5 file
501
file = tempfile.mktemp(".h5")
502
#file = "test_widetree.h5"
505
fileh = openFile(file, mode = "w")
507
print "Children writing progress: ",
508
for child in range(maxchildren):
510
print "%3d," % (child),
511
fileh.createArray(fileh.root, 'array' + str(child),
512
a, "child: %d" % child)
520
# Open the previous HDF5 file in read-only mode
521
fileh = openFile(file, mode = "r")
523
print "\nTime spent opening a file with %d arrays: %s s" % \
524
(maxchildren, time.time()-t1)
525
print "\nChildren reading progress: ",
526
# Get the metadata on the previosly saved arrays
527
for child in range(maxchildren):
529
print "%3d," % (child),
530
# Create an array for later comparison
531
# Get the actual array
532
array_ = getattr(fileh.root, 'array' + str(child))
534
# Arrays a and b must be equal
537
print # This flush the stdout buffer
540
# Then, delete the file
543
def test01_wideTree(self):
544
"""Checking creation of large number of groups (1024) per group
546
Variable 'maxchildren' controls this check. PyTables support
547
up to 4096 children per group, but this would take too much
548
memory (up to 64 MB) for testing purposes (may be we can add a
549
test for big platforms). A 1024 children run takes up to 30 MB.
550
A 512 children test takes around 25 MB.
559
# for standard platforms
562
print '\n', '-=' * 30
563
print "Running %s.test00_wideTree..." % \
564
self.__class__.__name__
565
print "Maximum number of children tested :", maxchildren
566
# Open a new empty HDF5 file
567
file = tempfile.mktemp(".h5")
568
#file = "test_widetree.h5"
570
fileh = openFile(file, mode = "w")
572
print "Children writing progress: ",
573
for child in range(maxchildren):
575
print "%3d," % (child),
576
fileh.createGroup(fileh.root, 'group' + str(child),
584
# Open the previous HDF5 file in read-only mode
585
fileh = openFile(file, mode = "r")
587
print "\nTime spent opening a file with %d groups: %s s" % \
588
(maxchildren, time.time()-t1)
589
print "\nChildren reading progress: ",
590
# Get the metadata on the previosly saved arrays
591
for child in range(maxchildren):
593
print "%3d," % (child),
594
# Get the actual group
595
group = getattr(fileh.root, 'group' + str(child))
596
# Arrays a and b must be equal
597
assert group._v_title == "child: %d" % child
599
print # This flush the stdout buffer
602
# Then, delete the file
605
#----------------------------------------------------------------------
608
theSuite = unittest.TestSuite()
609
# This counter is useful when detecting memory leaks
611
#heavy=1 # see at the beginning
613
#theSuite.addTest(unittest.makeSuite(DeepTreeTestCase))
615
for i in range(niter):
616
theSuite.addTest(unittest.makeSuite(TreeTestCase))
617
theSuite.addTest(unittest.makeSuite(DeepTreeTestCase))
618
theSuite.addTest(unittest.makeSuite(WideTreeTestCase))
623
if __name__ == '__main__':
624
unittest.main( defaultTest='suite' )