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_read_channel(): """Read individual channel""" folder = om.Folder(persist) metadata = {} for channel in folder: channel.read() metadata.update({channel.name: channel.data})
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_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_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_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_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_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_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_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_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_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_defaultfileextension(): """Default file extensions of Channel works""" folder = om.Folder(persist) channel = om.Channel('testing_dfe.txt', folder) channel.data = {'key': {'hello': 5}}
def test_read_folder(): """Read full folder""" folder = om.Folder(persist) folder.read() folder.data
# Before we recurse, ensure this is not a path. isroot = False # TODO # Find a way to optimize this. Channel is being read here # to find the isRoot property which is used solely to # determine whether or not to continue searching. # This is an expensive operation, and whats worse, # the channel is being re-read in `cascade`. if current_channel: data = current_channel.read().data or {} if data.get('isRoot') is True: isroot = True if not isroot: return _findchannels(parent, term, result) return result # def cascade(folder, term): if __name__ == '__main__': import openmetadata as om package = os.getcwd() path = os.path.join(package, 'test', 'persist') path = om.Folder(r's:\content\jobs\test\content\shots') # print cascade(path, 'properties')