Exemple #1
0
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)
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)
Exemple #3
0
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