def test_pull_existing(self): """Read from existing metadata""" entry = om.Entry("standard_int.int", parent=self.project) om.pull(entry) self.assertEquals(entry.value, 10) self.assertEquals(entry.path.basename, "standard_int.int") self.assertEquals(entry.path.suffix, "int")
def test_add_entries_to_nongroup(self): """Add entries to nongroup Adding entries to an entry that isn't a group will cast it to a group. """ nongroup_entry = om.Entry('nongroup', value='A string', parent=self.root) self.assertTrue(nongroup_entry.type == 'string') om.flush(nongroup_entry) invalid_child = om.Entry('invalid_child', value='a string', parent=nongroup_entry) # By adding a child, the nongroup becomes a group. # Just like it would in a dynamic programming language: # >>> myint = 5 # >>> myint = list() self.assertEquals(nongroup_entry.type, 'dict') om.flush(nongroup_entry) self.assertTrue(os.path.exists(nongroup_entry.path.as_str)) om.pull(nongroup_entry) self.assertTrue(os.path.exists(invalid_child.path.as_str)) self.assertTrue(os.path.isdir(nongroup_entry.path.as_str)) self.assertEquals(nongroup_entry.type, 'dict')
def test_pull_unknown_string(self): """Pull from entry whose value is string but suffix is misnamed""" entry = om.Entry('unknown_string.abc', parent=self.project) self.assertTrue(os.path.exists(entry.path.as_str)) om.pull(entry) self.assertEquals(entry.value, u'this is of type string') self.assertEquals(entry.path.suffix, 'string')
def test_new_group(self): """Write a new group""" entry = om.Entry('a group.dict', parent=self.root) om.flush(entry) om.pull(entry) self.assertEquals(entry.type, 'dict') self.assertTrue(os.path.isdir(entry.path.as_str))
def test_absolutename(self): """If entry exists, suffix will be implied by find()""" entry = om.Entry('custom.int', value=10, parent=self.root) om.flush(entry) entry = om.Entry('custom.string', value="Hello", parent=self.root) om.pull(entry) self.assertEquals(entry.type, 'int')
def test_dict(self): """Write dict""" name = 'mydict.dict' dic = om.Entry(name, parent=self.root) om.flush(dic) om.pull(dic) self.assertEqual(dic.type, 'dict') self.assertEqual(dic.name, 'mydict')
def test_int(self): name = 'integer' value = 10 integer = om.Entry(name, value=value, parent=self.root) om.flush(integer) om.pull(integer) self.assertEquals(integer.value, value) self.assertEquals(integer.name, name) self.assertEquals(integer.type, 'int')
def test_pull_unknown_corrupt(self): """Pull from unknown extension and corrupt value Entries are all JSON-formatted. This one however is mis-formatted and will not be successfully parsed. """ entry = om.Entry('unknown_corrupt.abc', parent=self.project) om.pull(entry) self.assertEquals(entry.value, None)
def test_flush_existing(self): """Overwrite existing entry""" # Make it exist standard_int = om.Entry('standard_int', value=10, parent=self.root) om.flush(standard_int) self.assertTrue(os.path.exists(standard_int.path.as_str)) # Then flush it again standard_int = om.Entry('standard_int', value=15, parent=self.root) om.flush(standard_int) om.pull(standard_int) self.assertEquals(standard_int.value, 15)
def test_case_sensitivity(self): case_sensitive_location = om.Location(self.case_path) data = om.Entry('data', parent=case_sensitive_location) om.pull(data) self.assertEquals(data.value, 'value here') wrong_case = om.Location(self.case_path.lower()) data = om.Entry('data', parent=wrong_case) if sys.platform == 'win32': om.pull(data) self.assertEquals(data.value, 'value here') else: self.assertRaises(om.error.Exists, om.pull, data)
def flush_multiple(self): parent = om.Entry('parent', parent=self.root) for key, value in self.data.iteritems(): om.Entry(key, value=value, parent=parent) om.flush(parent) # Read data back from disk and re-build it om.pull(parent) pulled_data = dict() for child in parent: om.pull(child) pulled_data[child.path.basename] = child.value self.assertEquals(self.data, pulled_data)
def test_integration(self): """Test a combination of features""" entry = om.Entry('test.string', value="Hello", parent=self.root) child = om.Entry('child.int', value=1, parent=entry) self.assertEquals(entry.type, 'dict') om.flush(entry) self.assertTrue(os.path.exists(entry.path.as_str)) om.pull(entry) self.assertEquals(entry.type, 'dict') entry.value = "Hello" self.assertEquals(entry.type, 'string') self.assertEquals(entry.value, "Hello") om.flush(entry) om.pull(entry) self.assertFalse(os.path.exists(child.path.as_str)) child = om.Entry('child.int', value=1, parent=entry) om.flush(entry) self.assertEquals(om.read(self.root_path, 'test/child'), 1) om.write(self.root_path, '/test/child', 2) self.assertEquals(om.read(self.root_path, 'test/child'), 2) om.write(self.root_path, '/root/test/another', 10) self.assertEquals(om.read(self.root_path, 'root/test/another'), 10)
def test_pull_unknown(self): """Pull from unknown extension without value""" entry = om.Entry('unknown.abc', parent=self.project) om.pull(entry) self.assertEquals(entry.value, None)
def test_suffix_and_type_mismatch(self): height = om.Entry('height.int', value=10.1, parent=self.root) self.assertEquals(height.type, 'float') om.flush(height) om.pull(height) self.assertEquals(height.type, 'float')
level3 = os.path.join(level2, 'level3') om.write(level1, '/address/street', value='Code Street 5') om.write(level1, '/address/postcode', value='Level 1 postcode') om.write(level2, '/address/city', value='Code City') om.write(level2, '/address/postcode', value='Level 2 postcode') assert os.path.exists(os.path.join(level1, '.meta')) assert os.path.exists(os.path.join(level2, '.meta')) # Now that we have written a hierarchy of values, # let's try and reach `street` from level2, even # though `street` resides in level1 location = om.Location(level2) om.pull(location) address = location['address'] om.pull(address) try: postcode = address['street'] except KeyError: print "This is supposed to happen" # That won't work, since `street` isn't in level2, # but in level1. Let's try doing that again, but # this time, we will inherit data from parents of # level2. location = om.Location(level2)
'clear', 'find', # 'find_all', # 'exists', # 'existing', 'inherit', # 'history', # 'restore', 'islocation', 'isentry' ] if __name__ == '__main__': import doctest doctest.testmod() # from pprint import pprint import openmetadata as om om.setup_log('openmetadata') path = r'c:\users\marcus\om2' location = om.Location(path) om.pull(location) history = location['.history'] om.pull(history) # gen = history.children # gen.next() # age = om.history(location['age']) # imprint = age.next() # om.restore(imprint)