-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_ls.py
233 lines (177 loc) · 7.43 KB
/
test_ls.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import tempfile
import shutil
import errno
from nose.tools import assert_raises
import pather
class TestLs(object):
@classmethod
def setup_class(cls):
cls.directory = tempfile.mkdtemp()
cls.pattern = 'project/assets/{item}/{task}/published/{version}/'
# Set current root directory
os.chdir(cls.directory)
paths = ['project/assets/ben/modeling/published/v01/',
'project/assets/ben/modeling/published/v02/',
'project/assets/ben/modeling/published/v03/',
'project/assets/ben/animation/published/v01/',
'project/assets/ben/animation/published/v02/',
'project/assets/ben/rigging/published/v01/',
'project/assets/joe/modeling/published/v01/',
'project/assets/joe/modeling/published/v02/',
'project/assets/joe/groom/published/v01/',
'project/assets/claire/modeling/published/v01/',
'project/assets/claire/modeling/work/maya/',
'project/assets/claire/modeling/data/what/is/in/here/',
'project/assets/claire/modeling/published/v02/']
for path in paths:
full_tree = os.path.join(cls.directory, path)
if not os.path.exists(full_tree):
os.makedirs(full_tree)
@classmethod
def teardown_class(cls):
pass
# try:
# shutil.rmtree(cls.directory)
# except OSError as exc:
# # ENOENT - no such file or directory
# if exc.errno != errno.ENOENT:
# raise # re-raise exception
def _in_tmpdir(self, paths):
paths = [os.path.join(self.directory, path) for path in paths]
paths = [os.path.realpath(path) for path in paths]
return paths
def test_ls_no_data(self):
"""Ls full query without any data provided to the pattern"""
expected_matches = [
'project/assets/ben/modeling/published/v01/',
'project/assets/ben/modeling/published/v02/',
'project/assets/ben/modeling/published/v03/',
'project/assets/ben/animation/published/v01/',
'project/assets/ben/animation/published/v02/',
'project/assets/ben/rigging/published/v01/',
'project/assets/joe/modeling/published/v01/',
'project/assets/joe/modeling/published/v02/',
'project/assets/joe/groom/published/v01/',
'project/assets/claire/modeling/published/v01/',
'project/assets/claire/modeling/published/v02/'
]
expected_matches = self._in_tmpdir(expected_matches)
matches = pather.ls(self.pattern)
# Compare on contents: http://stackoverflow.com/q/8866652/1838864
assert set(expected_matches) == set(matches)
def test_ls_data(self):
"""Ls a set of queries with different data provided"""
# match only item 'claire'
expected_matches = [
'project/assets/claire/modeling/published/v01/',
'project/assets/claire/modeling/published/v02/'
]
expected_matches = self._in_tmpdir(expected_matches)
matches = pather.ls(self.pattern,
include={'item': 'claire'})
assert set(expected_matches) == set(matches)
# match only item 'claire' and version 'v02'
expected_matches = [
'project/assets/claire/modeling/published/v02/'
]
expected_matches = self._in_tmpdir(expected_matches)
matches = pather.ls(self.pattern,
include={'item': 'claire',
'version': 'v02'})
assert set(expected_matches) == set(matches)
# no match
matches = pather.ls(self.pattern,
include={'item': 'd'})
assert matches == []
# no match (empty string)
matches = pather.ls(self.pattern,
include={'item': ''})
assert matches == []
def test_ls_data_type_exception(self):
"""Ls invalid data values type exception raised"""
with assert_raises(TypeError):
pather.ls(self.pattern,
include={'item': None})
with assert_raises(TypeError):
pather.ls(self.pattern,
include={'item': True})
with assert_raises(TypeError):
pather.ls(self.pattern,
include={'item': 1})
with assert_raises(TypeError):
pather.ls(self.pattern,
include={'item': object()})
class TestLsDotted(object):
@classmethod
def setup_class(cls):
cls.directory = tempfile.mkdtemp()
cls.pattern = 'test/{a}.dotted.{b}/{c}.{d}.{e}/{f}.{ext}'
# Set current root directory
os.chdir(cls.directory)
paths = ['test/folder.dotted.here/so.many.dots/testing.extension']
for path in paths:
full_tree = os.path.join(cls.directory, path)
if not os.path.exists(full_tree):
os.makedirs(full_tree)
@classmethod
def teardown_class(cls):
pass
# try:
# shutil.rmtree(cls.directory)
# except OSError as exc:
# # ENOENT - no such file or directory
# if exc.errno != errno.ENOENT:
# raise # re-raise exception
def _in_tmpdir(self, paths):
paths = [os.path.join(self.directory, path) for path in paths]
paths = [os.path.realpath(path) for path in paths]
return paths
def test_ls_dotted(self):
"""Ls dotted"""
expected_matches = [
'test/folder.dotted.here/so.many.dots/testing.extension'
]
expected_matches = self._in_tmpdir(expected_matches)
matches = pather.ls(self.pattern)
assert set(expected_matches) == set(matches)
def test_ls_dotted_data_full(self):
"""Ls dotted with data full"""
expected_matches = [
'test/folder.dotted.here/so.many.dots/testing.extension'
]
expected_matches = self._in_tmpdir(expected_matches)
include = {'a': 'folder',
'b': 'here',
'c': 'so',
'd': 'many',
'e': 'dots',
'f': 'testing',
'ext': 'extension'}
matches = pather.ls(self.pattern, include=include)
assert set(expected_matches) == set(matches)
def test_ls_dotted_data_partial(self):
"""Ls dotted with data partial"""
expected_matches = [
'test/folder.dotted.here/so.many.dots/testing.extension'
]
expected_matches = self._in_tmpdir(expected_matches)
include = {'a': 'folder',
'b': 'here',
'd': 'many',
'e': 'dots',
'f': 'testing'}
matches = pather.ls(self.pattern, include=include)
assert set(expected_matches) == set(matches)
def test_ls_dotted_with_matches(self):
"""Ls dotted with matches"""
results = pather.ls(self.pattern, with_matches=True)
path, match_data = results[0]
expected_data = {'a': 'folder',
'b': 'here',
'c': 'so',
'd': 'many',
'e': 'dots',
'f': 'testing',
'ext': 'extension'}
assert expected_data == match_data