class TestYamlDB(unittest.TestCase): def setUp(self): self.dbfd, self.dbpath = mkstemp( prefix='cloudmesh.db.strdb.TestYamlDB.') self.db = YamlDB(path=self.dbpath) def tearDown(self): os.close(self.dbfd) os.unlink(self.dbpath) @given(st.text(), st.text()) def test_insert(self, key, value): self.db[key] = value @given(st.text(), st.text()) def test_in(self, key, value): self.db[key] = value self.assertIn(key, self.db) @given(st.text(), st.text()) def test_get(self, key, value): self.db[key] = value dbvalue = self.db[key] self.assertEqual(value, dbvalue) @given(st.text(), st.text()) def test_del(self, key, value): self.db[key] = value del self.db[key] self.assertNotIn(key, self.db) @given(st.text(), st.text()) def test_persistence(self, key, value): self.db[key] = value with open(self.dbpath, 'rb') as dbfile: db = yaml.safe_load(dbfile) self.assertIn(key, db) self.assertEqual(value, db[key]) @given(st.text(), st.text()) def test_iter(self, key, value): self.db[key] = value for k in self.db: pass @given(st.text(), st.text()) def test_clear(self, key, value): self.db[key] = value self.db.clear() self.assertNotIn(key, self.db) @given(st.lists(st.tuples(st.text(), st.text()))) def test_len(self, entries): self.db.clear() duplicates = 0 for k, v in entries: duplicates += 1 if k in self.db else 0 self.db[k] = v self.assertEqual(len(self.db), len(entries) - duplicates)
def setup(self): self.key = "key" self.value = "value" self.dbfd, self.dbpath = mkstemp( prefix='cloudmesh.db.strdb.TestYamlDB.' ) self.db = YamlDB(path=self.dbpath)
class TestYamlDB: def setup(self): self.key = "key" self.value = "value" self.dbfd, self.dbpath = mkstemp( prefix='cloudmesh.db.strdb.TestYamlDB.' ) self.db = YamlDB(path=self.dbpath) def test_insert(self): self.db[self.key] = self.value def test_in(self): self.db[self.key] = self.value assert self.key in self.db def test_get(self): self.db[self.key] = self.value dbvalue = self.db[self.key] assert self.value == dbvalue def test_del(self): self.db[self.key] = self.value del self.db[self.key] assert self.key not in self.db def test_persistence(self): self.db[self.key] = self.value with open(self.dbpath, 'rb') as dbfile: db = yaml.safe_load(dbfile) assert self.key in db assert self.value == db[self.key] def test_iter(self): self.db[self.key] = self.value for k in self.db: pass def test_clear(self): self.db[self.key] = self.value self.db.clear() assert self.key not in self.db def test_len(self): self.db.clear() entries = ['a', 'b', 'c'] for k in entries: self.db[k] = k assert len(self.db) == len(entries) def tearDown(self): os.close(self.dbfd) os.unlink(self.dbpath)
def __init__(self, filename=None): """ initializes the default variables. The default file is in "~/.cloudmesh/default-data" :param filename: """ if filename is None: self.filename = Path(path_expand("~/.cloudmesh/default-data")) self.data = YamlDB(str(self.filename))
class Variables(object): def __init__(self, filename=None): self.filename = path_expand(filename or "~/.cloudmesh/var-data") self.data = YamlDB(self.filename) def __getitem__(self, key): if key not in self.data: return None else: return self.data[key] def __setitem__(self, key, value): # print("set", key, value) self.data[str(key)] = value def __delitem__(self, key): if key in self.data: del self.data[str(key)] def __contains__(self, item): return str(item) in self.data def __str__(self): return str(self.data) def __len__(self): return len(self.data) def __add__(self, directory): for key in directory: self.data[key] = directory[key] def __sub__(self, keys): for key in keys: del self.data[key] def __iter__(self): return iter(self.data) def close(self): self.data.close() def clear(self): self.data.clear() def dict(self): return self.data._db def parameter(self, attribute, position=0): value = str(self.data[attribute]) expand = Parameter.expand(value)[position] return expand
def __init__(self, filename=None): self.filename = path_expand(filename or "~/.cloudmesh/var-data") self.data = YamlDB(self.filename)
def __init__(self, filename=None): if filename is None: self.filename = Path(path_expand("~/.cloudmesh/default-data")) self.data = YamlDB(str(self.filename))
class Default(object): def _index(self, context, key): return str(context) + "," + str(key) def __init__(self, filename=None): if filename is None: self.filename = Path(path_expand("~/.cloudmesh/default-data")) self.data = YamlDB(str(self.filename)) def __getitem__(self, context_key): try: if type(context_key) == tuple: context, key = context_key index = self._index(context, key) if index not in self.data: return None else: return self.data[index] else: d = self.__dict__() if context_key not in d: return None else: return self.__dict__()[context_key] except: return None def __setitem__(self, context_key, value): context, key = context_key self.data[self._index(context, key)] = value def __delitem__(self, context_key): print("DEL") if type(context_key) == tuple: context, key = context_key del self.data[self._index(context, key)] else: context = context_key for element in self.data: print("E", element, context) if element.startswith(context + ","): del self.data[element] def __contains__(self, item): for key in self.data: if item == self.data[key]: return True return False def __str__(self): d = {} for element in self.data: context, key = element.split(",") value = self.data[element] if context not in d: d[context] = {} d[context][key] = value # return (str(self.data)) return str(self.__dict__()) def __dict__(self): d = {} for element in self.data: context, key = element.split(",") value = self.data[element] if context not in d: d[context] = {} d[context][key] = value return d def __repr__(self): return str(self.data) def __len__(self): return len(self.data) # def __add__(self, directory): # for key in directory: # self.data[key] = directory[key] # def __sub__(self, keys): # for key in keys: # del self.data[key] def close(self): self.data.close()
def setUp(self): self.dbfd, self.dbpath = mkstemp( prefix='cloudmesh.db.strdb.TestYamlDB.') self.db = YamlDB(path=self.dbpath)