-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
500 lines (347 loc) · 12.9 KB
/
test.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#!/usr/bin python
from __future__ import absolute_import
import os
from nose.tools import *
import openmetadata as om
cwd = os.getcwd()
root = os.path.join(cwd, 'test')
dynamic = os.path.join(root, 'dynamic')
persist = os.path.join(root, 'persist')
stress = os.path.join(root, 'stresstest')
def test_children(root=None):
"""Children are returned as appropriate objects
Conditions:
- Children of Folders are always Channel
- Children of Channel are either Key or Folder
- Key has no children
"""
root = root or om.Folder(persist)
if isinstance(root, om.domain.AbstractParent):
for child in root.children:
if isinstance(root, om.Folder):
# Children of Folders are always channels
assert_is_instance(child, om.Channel)
if isinstance(root, om.Channel):
assert isinstance(child, om.Key) or isinstance(child, om.Folder)
# Recursively test each child
test_children(child)
else:
assert_is_instance(root, om.Key)
def test_relativepath():
"""Children contain relative paths"""
folder = om.Folder(persist)
for child in folder:
om_relpath = child.relativepath
parent_path = os.path.join(folder.path, om.constant.Meta)
manual_relpath = os.path.relpath(child.path, parent_path)
assert_equals(om_relpath, manual_relpath)
# Manually adding a child
channel = om.Channel(os.path.join(persist, r'.meta\chan.txt'), folder)
assert_equals(channel.relativepath, os.path.relpath(channel.path, os.path.join(folder.path, om.constant.Meta)))
channel = om.Channel(os.path.join(dynamic, r'.meta\chan.txt'), folder)
assert_true(os.path.isabs(channel.relativepath))
def test_extension():
"""Test extension returns extension including dot"""
folder = om.Folder(persist)
for channel in folder:
assert_equals(channel.extension, "." + channel.basename.rsplit(".", 1)[1])
assert_equals(channel.extension, os.path.splitext(channel.path)[1])
def test_clear_file():
"""Clear individual file"""
folder = om.Folder(persist)
# Add channel to it
channel = om.Channel('new_channel.txt', folder)
file = om.Key('document.txt', channel)
file.data = "This is some data"
file.write()
file.clear()
assert_equals(file.exists, False)
# Clean up
channel.clear()
def test_clear_channel():
"""Clear individual channel"""
folder = om.Folder(persist)
# Add channel to it
channel = om.Channel('new_channel.txt', folder)
file = om.Key('document.txt', channel)
file.data = "This is some data"
file.write()
channel.clear()
assert_equals(channel.exists, False)
def test_clear_folder():
"""Remove ALL metadata"""
folder = om.Folder(dynamic)
# Add channel to it
channel = om.Channel('new_channel.txt', folder)
file = om.Key('document.txt', channel)
file.data = "This is some data"
file.write()
folder.clear()
assert_equals(folder.exists, False)
def test_comparison():
"""Separate instances of same path are equal"""
folder = om.Folder(dynamic)
# Add channel to it
channel = om.Channel('new_channel.txt', folder)
file = om.Key('document.txt', channel)
file_other = om.Key('document.txt', channel)
assert_equals(file, file_other)
def test_iterator():
"""Iterator (for channel in folder) works"""
folder = om.Folder(root)
for channel in folder:
# Child is indeed a channel
assert_is_instance(channel, om.Channel)
# Result of iterator is the same as calling
# .children manually.
assert_true(channel in folder.children)
for file in channel:
assert_is_instance(file, om.Key)
assert_true(file in channel.children)
def test_trash():
"""Deleted items end up in trash"""
pass
def test_revisions():
"""Edited items are backed up in revisions"""
pass
# def test_om_read():
# """`om.read()` convenience method"""
# metadata = om.read(persist)
# for channel, content in metadata.iteritems():
# assert isinstance(content, dict) or isinstance(content, basestring)
def test_instancefactory(root=None):
"""Stress-test instance.Factory
Traverse stresstest folder and ensure only appropriate
folders get assigned their respective objects.
The rules are:
- Any folder with .meta within is a Folder,
unless it's parent is also a .meta, then it is
a Channel that may be considered a Folder
- Any folder within .meta is a channel
if is has an extension.
- Any file whose parents parent is a .meta folder
is a Key object.
!Todo!
How:
- Get all files and folders of root manually
- Compare and contrast resulting children
- No objects containing the word "invalid_" should be returned
- No objects containing the word "no_" should be returned
- All other objects should be returned
!Todo!
"""
root = root or om.Factory.create(stress)
if isinstance(root, om.domain.AbstractParent):
for child in root.children:
test_instancefactory(child)
# Inserting a .meta folder directly
metafolder = om.Factory.create(os.path.join(persist, om.constant.Meta))
assert_is_instance(metafolder, om.domain.Folder)
# def test_om_write():
# """`om.write()` convenience method"""
# om.write(dynamic, 'some text')
# def test_om_update():
# """`om.read()` convenience method"""
# existing_file = os.path.join(persist, r'.meta\chan.txt\document.txt')
# om.update(existing_file, 'updated data!')
# def test_om_delete():
# """`om.delete()` convenience method"""
# meta = om.Folder(os.path.join(dynamic, om.constant.Meta))
# chan = om.Channel('chan.txt', parent=meta)
# file = om.Key('document.txt', parent=chan)
# data = 'some text'
# file.data = data
# file.write()
# om.delete(meta.path)
def test_factory_folder():
"""Create Folder via Factory"""
obj = om.Factory.create(persist)
assert_is_instance(obj, om.Folder)
def test_factory_channel():
"""Create Channel via Factory"""
chan = os.path.join(persist, '.meta', 'chan.txt')
channel = om.Factory.create(chan)
assert_is_instance(channel, om.Channel)
def test_factory_file():
"""Create Key via Factory"""
chan = os.path.join(persist, '.meta', 'chan.txt', 'document.txt')
channel = om.Factory.create(chan)
assert_is_instance(channel, om.Key)
def test_full_template():
"""New metadata from scratch using templates"""
folder = om.Folder(dynamic)
chan = om.Channel('chan.txt', parent=folder)
file = om.Key('document.txt', parent=chan)
data = 'some text'
file.data = data
file.write()
# Read it back in
file_instance = om.Factory.create(file.path)
file_instance.read()
assert_equals(file_instance.data, data)
om.delete(folder.path)
def test_append_file_to_existing():
"""Append file to existing channel"""
folder = om.Folder(persist)
channel = folder.children[0]
data = "new content"
file_template = om.Key('appended.txt', channel)
file_template.data = data
file_template.write()
# Read it back in
file_instance = om.Factory.create(file_template.path)
file_instance.read()
assert_is_instance(file_instance, om.Key)
assert_equals(file_instance.data, data)
om.delete(file_instance.path)
def test_append_metadata_to_channel():
"""Append metadata to existing channel"""
meta = om.Folder(persist)
channel = meta.children[0]
submeta = om.Folder('.meta', parent=channel)
subchannel = om.Channel('subchan.txt', parent=submeta)
data = 'some text'
file = om.Key('document.txt', parent=subchannel)
file.data = data
file.write()
# Read it back in
file_instance = om.Factory.create(file.path)
file_instance.read()
assert_is_instance(file_instance, om.Key)
assert_equals(file_instance.data, data)
om.delete(file_instance.path)
def test_hidden():
"""Special channels works
Persist has a hidden channel called __hidden__
"""
folder = om.Folder(persist)
hidden = None
for channel in folder:
if channel.hidden:
hidden = channel
assert_true(hidden is not None)
def test_hidden_channels():
"""Hidden channels works
Pre-conditions:
"persist" has atleast one hidden folder
"""
folder = om.Folder(persist)
hidden = folder.hiddenchildren
assert_true(hidden is not [])
# def test_unique_channelname():
# """Channel names must be unique"""
# folder = om.Folder(persist)
# # These channels have the same name, even though they
# # differ in their extensions. This is not valid due to
# # convenience method om.read() returns channel names
# # without extension.
# channel1 = om.Channel('unique1.txt', folder)
# channel2 = om.Channel('unique1.kvs', folder)
# file1 = om.Key('temp.txt', channel1)
# file2 = om.Key('temp.txt', channel2)
# # file1.write()
# assert_raises(1/0, DivisionByZeroException)
def test_read_channel():
"""Read individual channel"""
folder = om.Folder(persist)
metadata = {}
for channel in folder:
channel.read()
metadata.update({channel.name: channel.data})
# print metadata
def test_read_folder():
"""Read full folder"""
folder = om.Folder(persist)
folder.read()
folder.data
def test_cascading_metadata():
"""Cascading metadata behaves appropriately
The child folder contains overriding properties from
the parent `persist` folder.
Namely
Persist -> par1: {key1: {val1: Original, val2: Original}, key2: {val2: Original}}
Child -> par1: {key1: {val1: Changed, val3: Original}, key2: Changed}
The result should be
par1: {key1: {val1: Changed, val2: Original, val3: Original}, key2: Changed}
"""
# persist_obj = om.Folder(persist)
path = os.path.join(persist, 'child')
csmetadata = om.transaction.cascade(path, 'cascading')
# A is overriden
assert_equals(csmetadata['root']['A'], 'Overidden')
# B is not
assert_equals(csmetadata['root']['B'], 'Original')
# more remains
assert_equals(csmetadata['more'], {'key': 'value'})
def test_channel_set_multiple_times():
"""Set channel data multiple times"""
folder = om.Factory.create(root)
channel = om.Channel('testing.kvs', folder)
channel.data = {u'file1': {u'some data': u'data'}, 'file2': {'some': u'data'}}
channel.data = {u'file1': {u'some data': u'data'}, 'file2': {'some': u'data'}}
channel.data = {u'file4': {u'some data': u'data'}, 'file1': {'some': u'data'}}
print channel.data
def test_defaultfileextension():
"""Default file extensions of Channel works"""
folder = om.Folder(persist)
channel = om.Channel('testing_dfe.txt', folder)
channel.data = {'key': {'hello': 5}}
# channel.write()
# channel.read()
# data = channel.data['key']
# print "%r(%s)" % (data.__class__, data)
# channel.write()
# channel.clear()
def test_individual_channel_convenience():
"""Accessing individual channel via om.read works"""
data = om.read(path=persist, channel='testing')
assert_false(data == {})
# Non-existing channel returns dictionary
assert_is_instance(om.read(persist, 'NON_EXISTANT'), dict)
def test_individual_file_convenience():
"""Accessing individual file via om.read works"""
data = om.read(path=persist, channel='testing', key='file1')
assert_false(data == {})
data = om.read(persist, 'testing', 'NON_EXISTANT')
assert_equals(data, None)
def test_om_write():
channel_data = {'channel1': {'key1': 'data'}}
key_data = 'data'
# Write to channel
# om.write(path=persist, channel='testing', data=channel_data)
# Write to key
# om.write(path=persist, channel='testing', key='temp', data=key_data)
if __name__ == '__main__':
import logging
log = logging.getLogger('openmetadata')
log.setLevel(logging.ERROR)
import nose
nose.run(defaultTest=__name__)
# test_cascading_metadata()
# test_om_write()
# test_inmemorychildren()
# test_individual_channel_convenience()
# test_individual_file_convenience()
# test_defaultfileextension()
# test_channel_set_multiple_times()
# test_cascading_metadata()
# test_relativepath()
# print metadata
# test_clear_file()
# test_clear_folder()
# test_relativepath()
# test_children()
# test_om_read()
# test_factory_folder()
# test_factory_channel()
# test_instancefactory()
# test_factory_file()
# test_om_write()
# test_full_template()
# test_append_file_to_existing()
# test_append_metadata_to_channel()
# test_hardlink_reference()
# test_copy_reference()
# test_read_channel()
# test_hidden_channels()
# test_cascading_metadata()