-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rdir.py
208 lines (173 loc) · 8.01 KB
/
test_rdir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import unittest
import os
from fixes import ROOT
from ROOT import gDirectory, TFile
from rdir import pathspec, savepwd, Rdir
class test_pathspec(unittest.TestCase):
def setUp(self):
self.full_path = pathspec('foo.root:/bar/baz')
self.only_rfile1 = pathspec('foo.root')
self.only_rfile2 = pathspec('foo.root:')
self.abs_rpath = pathspec('/foo/bar/baz')
self.rel_rpath1 = pathspec('foo/bar/baz')
self.rel_rpath2 = pathspec('../../foo/bar/baz')
self.rpath_no_slash = pathspec('foo/bar/baz/')
def test_full_path(self):
self.assertTrue(self.full_path.rfile)
self.assertTrue(self.full_path.rpath)
self.assertFalse(self.full_path.relative)
def test_rfile(self):
self.assertTrue(self.only_rfile1.norpath)
self.assertTrue(self.only_rfile2.norpath)
self.assertTrue(self.abs_rpath.norfile)
self.assertTrue(self.rel_rpath1.norfile)
self.assertTrue(self.rel_rpath2.norfile)
def test_rpath(self):
self.assertFalse(self.abs_rpath.relative)
self.assertTrue(self.rel_rpath1.relative)
self.assertTrue(self.rel_rpath2.relative)
self.assertLess(self.rpath_no_slash.rpath.find('/', -1), 0)
self.assertRaises(ValueError, pathspec, 'foo.root:../bar/baz')
class test_savepwd(unittest.TestCase):
def setUp(self):
self.rfile = TFile.Open('/tmp/test_savepwd.root', 'recreate')
self.dir1 = self.rfile.mkdir('dir1')
self.dir2 = self.rfile.mkdir('dir2')
def tearDown(self):
os.remove('/tmp/test_savepwd.root')
def test_restore_pass(self):
self.dir1.cd()
oldpwd = gDirectory.GetName()
with savepwd():
self.dir2.cd()
self.assertEqual(oldpwd, gDirectory.GetName())
def test_restore_fail(self):
self.dir1.cd()
oldpwd = gDirectory.GetName()
try:
with savepwd():
self.dir2.cd()
raise KeyboardInterrupt('Testing')
except KeyboardInterrupt:
pass
self.assertEqual(oldpwd, gDirectory.GetName())
class test_Rdir(unittest.TestCase):
def setUp(self):
"""Make ROOT files with some contents.
Structure:
/tmp/test_Rdir{0,1}.root:/dira/hista
/tmp/test_Rdir{0,1}.root:/dirb/histb
/tmp/test_Rdir{0,1}.root:/dirc/histx
/tmp/test_Rdir{0,1}.root:/dirc/dird/histy
/tmp/test_Rdir{0,1}.root:/dirc/dird/dire/histz
/tmp/test_Rdir{0,1}.root:/hist{0,1,2}
"""
self.fnames, self.rfiles = [], []
for i in range(2):
self.fnames.append('/tmp/test_Rdir{}.root'.format(i))
self.rfiles.append(TFile.Open(self.fnames[-1], 'recreate'))
for f in self.rfiles:
rdir = f.mkdir('dira')
rdir.WriteTObject(ROOT.TH1C('hista', '', 10, 0, 10))
rdir = f.mkdir('dirb')
rdir.WriteTObject(ROOT.TH1C('histb', '', 10, 0, 10))
rdir = f.mkdir('dirc/dird/dire')
rdir.cd()
gDirectory.WriteTObject(ROOT.TH1C('histx', '', 10, 0, 10))
rdir.cd('dird')
gDirectory.WriteTObject(ROOT.TH1C('histy', '', 10, 0, 10))
rdir.cd('dird/dire')
gDirectory.WriteTObject(ROOT.TH1C('histz', '', 10, 0, 10))
for i in range(3):
f.WriteTObject(ROOT.TH1C('hist{}'.format(i), '', 10, 0, 10))
for f in self.rfiles:
f.Write()
f.Close()
def tearDown(self):
for fname in self.fnames:
os.remove(fname)
def test_init(self):
self.assertTrue(Rdir(self.fnames))
self.assertRaises(TypeError, Rdir, self.rfiles)
def test_get_dir_full(self):
rdir_helper = Rdir(self.fnames)
self.assertTrue(rdir_helper.get_dir('/tmp/test_Rdir1.root:/dira'))
self.assertTrue(rdir_helper.get_dir('/tmp/test_Rdir1.root:'))
self.assertTrue(rdir_helper.get_dir('/tmp/test_Rdir1.root'))
# not a directory
self.assertFalse(rdir_helper.get_dir('/tmp/test_Rdir1.root:/dira/hista'))
# non-existent
self.assertFalse(rdir_helper.get_dir('/tmp/test_Rdir1.root:/boohoo'))
def test_get_dir_rel(self):
rdir_helper = Rdir(self.fnames)
rdir = rdir_helper.get_dir('/tmp/test_Rdir0.root:/dirc/dird/dire')
if rdir.cd():
self.assertTrue(rdir_helper.get_dir('../../'))
self.assertTrue(rdir_helper.get_dir('../../../'))
else:
self.fail(msg='Prep for Rdir.get_dir() with relative path failed')
def test_ls_full(self):
from functools import reduce
rdir_helper = Rdir(self.fnames)
keys_t = rdir_helper.ls('/tmp/test_Rdir0.root:/dirb')
keys_r = rdir_helper.files[0].GetDirectory('/dirb').GetListOfKeys()
res = map(lambda i, j: i.GetName() == j.GetName(), keys_t, keys_r)
self.assertTrue(reduce(lambda i, j: i and j, res),
msg='Keys do not match')
# FIXME: change files to see if any effects show up, need better test
keys_t = rdir_helper.ls('/tmp/test_Rdir1.root')
keys_r = rdir_helper.files[1].GetListOfKeys()
res = map(lambda i, j: i.GetName() == j.GetName(), keys_t, keys_r)
self.assertTrue(reduce(lambda i, j: i and j, res),
msg='Keys do not match')
def test_ls_rel(self):
from functools import reduce
rdir_helper = Rdir(self.fnames)
rdir = rdir_helper.get_dir('/tmp/test_Rdir0.root:/dirc/dird/dire')
if rdir.cd():
keys_t = rdir_helper.ls('../../')
keys_r = rdir.GetDirectory('../../').GetListOfKeys()
res = map(lambda i, j: i.GetName() == j.GetName(), keys_t, keys_r)
self.assertTrue(reduce(lambda i, j: i and j, res),
msg='Keys do not match')
keys_t = rdir_helper.ls('../../../')
keys_r = rdir.GetDirectory('../../../').GetListOfKeys()
res = map(lambda i, j: i.GetName() == j.GetName(), keys_t, keys_r)
self.assertTrue(reduce(lambda i, j: i and j, res),
msg='Keys do not match')
else:
self.fail(msg='Prep for Rdir.ls() with relative path failed')
def test_ls_names(self):
rdir_helper = Rdir(self.fnames)
keys_t = rdir_helper.ls_names('/tmp/test_Rdir0.root:/hist0')
keys_r = [rdir_helper.files[0].GetKey('hist0').GetName()]
self.assertListEqual(keys_r, keys_t)
keys_t = rdir_helper.ls_names('/tmp/test_Rdir1.root')
keys_r = [k.GetName() for k in rdir_helper.files[1].GetListOfKeys()]
self.assertListEqual(keys_r, keys_t)
def test_read(self):
from functools import reduce
rdir_helper = Rdir(self.fnames)
objs_t = rdir_helper.read('/tmp/test_Rdir1.root')
objs_r = [k.ReadObj() for k in rdir_helper.files[1].GetListOfKeys()]
# self.assertListEqual(objs_r, objs_t) # doesn't work'
res = map(lambda i, j: i.GetName() == j.GetName(), objs_t, objs_r)
self.assertTrue(reduce(lambda i, j: i and j, res),
msg='Keys do not match')
objs_t = rdir_helper.read('/tmp/test_Rdir1.root', metainfo=True)
res = map(lambda o: o.file == '/tmp/test_Rdir1.root', objs_t)
self.assertTrue(reduce(lambda i, j: i and j, res))
def test_filter(self):
rdir_helper = Rdir(self.fnames)
keys_t = [k for k in rdir_helper.ls('/tmp/test_Rdir0.root:',
robj_t=ROOT.TDirectoryFile)]
keys_r = [k for k in rdir_helper.files[0].GetListOfKeys()
if ROOT.TClass.GetClass(k.GetClassName())
.InheritsFrom(ROOT.TDirectoryFile.Class())]
self.assertListEqual(keys_r, keys_t)
keys_t = [k for k in rdir_helper.ls(
'/tmp/test_Rdir0.root:',
robj_p=lambda k: k.GetName().find('hist') >= 0)]
keys_r = [k for k in rdir_helper.files[0].GetListOfKeys()
if k.GetName().find('hist') >= 0]
self.assertListEqual(keys_r, keys_t)