Ejemplo n.º 1
0
 def tearDownClass():
     # cleanup
     with kdb.KDB() as db:
         ks = kdb.KeySet(100)
         db.get(ks, TEST_NS)
         ks.cut(kdb.Key(TEST_NS))
         db.set(ks, TEST_NS)
Ejemplo n.º 2
0
    def test_properties(self):
        self.assertEqual(self.key.name, "user/foo/bar")
        self.assertEqual(self.key.value, "value")
        self.assertEqual(self.key.basename, "bar")
        self.assertEqual(self.key.fullname, "user:myowner/foo/bar")

        self.assertEqual(self.bkey.name, "system/bkey")
        self.assertEqual(self.bkey.value, b"bvalue\0\0")
        self.assertEqual(self.bkey.basename, "bkey")
        self.assertEqual(self.bkey.fullname, "system/bkey")

        k = kdb.Key("user/key1", kdb.KEY_VALUE, "value")
        self.assertFalse(k.isbinary())
        self.assertIsNone(k.getmeta("binary"))

        k.name = "system/key2"
        k.basename = "key3"
        k.value = b"bvalue\0\0"
        self.assertEqual(k.name, "system/key3")
        self.assertEqual(k.value, b"bvalue\0\0")
        self.assertTrue(k.isbinary())
        self.assertIsInstance(self.bkey.getmeta("binary"), kdb.Key)

        k = kdb.Key("user/key2")
        with self.assertRaises(kdb.KeyInvalidName):
            k.name = "foo"
Ejemplo n.º 3
0
    def setUp(self):
        self.key = kdb.Key("user/foo/bar", kdb.KEY_VALUE, "value",
                           kdb.KEY_META, "by", "manuel", kdb.KEY_META, "owner",
                           "myowner")

        self.bkey = kdb.Key("system/bkey", kdb.KEY_VALUE, b"bvalue\0\0",
                            kdb.KEY_END, kdb.KEY_META, "lost", "lost")
Ejemplo n.º 4
0
	def test_get(self):
		with kdb.KDB() as db:
			ks = kdb.KeySet()
			db.get(ks, "system/elektra")

			key = ks["system/elektra/version/constants/KDB_VERSION"]
			self.assertEqual(key.value, kdb.VERSION)
Ejemplo n.º 5
0
    def test_ctor(self):
        self.assertIsInstance(self.ks, kdb.KeySet)

        ks = kdb.KeySet(0)
        self.assertIsInstance(ks, kdb.KeySet)

        ks = kdb.KeySet(self.ks)
        self.assertIsInstance(ks, kdb.KeySet)
Ejemplo n.º 6
0
    def test_get(self):
        with kdb.KDB() as db:
            ks = kdb.KeySet()
            db.get(ks, "system/elektra")

            import os
            if os.getenv("CHECK_VERSION") is None:
                key = ks["system/elektra/version/constants/KDB_VERSION"]
                self.assertEqual(key.value, kdb.VERSION)
Ejemplo n.º 7
0
    def test_functions(self):
        self.assertTrue(self.key.isuser())
        self.assertTrue(self.bkey.issystem())
        self.assertTrue(self.key.isstring())
        self.assertTrue(self.bkey.isbinary())
        self.assertTrue(self.key.isbelow(kdb.Key("user/foo")))

        k = kdb.Key("user/key1", kdb.KEY_VALUE, "value")
        self.assertEqual(k.get(), "value")
        k.set(b"bvalue\0\0")
        self.assertEqual(k.get(), b"bvalue\0\0")
Ejemplo n.º 8
0
    def test_set(self):
        with kdb.KDB() as db:
            ks = kdb.KeySet(100)
            db.get(ks, TEST_NS)

            try:
                key = ks[TEST_NS + "/mykey"]
            except KeyError:
                key = kdb.Key(TEST_NS + "/mykey")
                ks.append(key)
            key.value = "new_value"

            db.set(ks, TEST_NS)

        with kdb.KDB() as db:
            ks = kdb.KeySet(100)
            db.get(ks, TEST_NS)
            self.assertEqual(ks[TEST_NS + "/mykey"].value, "new_value")
Ejemplo n.º 9
0
    def test_ctor(self):
        self.assertIsInstance(self.key, kdb.Key)
        self.assertIsInstance(self.bkey, kdb.Key)

        k = kdb.Key("/cascading/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key("spec/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key("proc/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key("dir/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key("user/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key("system/key")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key()
        self.assertIsInstance(k, kdb.Key)
        self.assertFalse(k.isvalid())

        k = kdb.Key("wrongname")
        self.assertIsInstance(k, kdb.Key)
        self.assertFalse(k.isvalid())

        k = kdb.Key("user/foo")
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())

        k = kdb.Key(self.key)
        self.assertIsInstance(k, kdb.Key)
        self.assertTrue(k.isvalid())
Ejemplo n.º 10
0
    def test_meta(self):
        self.assertIsInstance(self.key.getmeta("owner"), kdb.Key)
        self.assertEqual(self.key.getmeta("owner").name, "owner")
        self.assertEqual(self.key.getmeta("owner").value, "myowner")
        self.assertEqual(self.key.getmeta("by").value, "manuel")

        self.assertFalse(self.key.hasmeta("doesnt_exist"))
        self.assertIsNone(self.key.getmeta("doesnt_exist"))
        self.assertTrue(bool(self.bkey.getmeta("binary")))
        self.assertIsNone(self.bkey.getmeta("owner"))

        k = kdb.Key("user/key1")
        k.setmeta("foo", "bar")
        self.assertEqual(k.getmeta("foo").value, "bar")

        self.assertEqual(sum(1 for _ in self.key.getmeta()), 2)
        self.assertEqual(sum(1 for _ in self.bkey.getmeta()), 1)
Ejemplo n.º 11
0
    def test_operator(self):
        self.assertEqual(len(self.ks), 4)
        self.assertEqual(len(kdb.KeySet(0)), 0)

        self.assertTrue(kdb.Key("user/key3") in self.ks)
        self.assertFalse(kdb.Key("user/foo") in self.ks)

        self.assertEqual(self.ks[0], kdb.Key("system/key1"))
        self.assertEqual(self.ks[-1], kdb.Key("user/key4"))
        with self.assertRaises(IndexError):
            self.assertIsNone(self.ks[100])
        with self.assertRaises(IndexError):
            self.assertIsNone(self.ks[-100])

        self.assertEqual(self.ks[1:3], [self.ks[1], self.ks[2]])

        self.assertEqual(self.ks["user/key3"], kdb.Key("user/key3"))
        with self.assertRaises(KeyError):
            self.assertIsNone(self.ks["user/doesnt_exist"])

        self.assertEqual(self.ks[kdb.Key("system/key2")],
                         kdb.Key("system/key2"))
        with self.assertRaises(KeyError):
            self.ks[kdb.Key("user/doesnt_exist")]
Ejemplo n.º 12
0
 def test_ctor(self):
     self.assertIsInstance(kdb.KDB(), kdb.KDB)
     error = kdb.Key()
     self.assertIsInstance(kdb.KDB(error), kdb.KDB)
Ejemplo n.º 13
0
    def test_operator(self):
        self.assertNotEqual(self.key, self.bkey)
        self.assertEqual(kdb.Key(self.key), self.key)
        self.assertEqual(
            self.key, kdb.Key("user/foo/bar", kdb.KEY_META, "owner",
                              "myowner"))
        self.assertEqual(kdb.Key(), kdb.Key())
        self.assertNotEqual(kdb.Key("user/key1"), kdb.Key("user/key2"))

        self.assertTrue(kdb.Key("user/key1") == kdb.Key("user/key1"))
        self.assertTrue(kdb.Key("user/key1") != kdb.Key("user/key2"))
        self.assertTrue(kdb.Key("user/key1") < kdb.Key("user/key2"))
        self.assertTrue(kdb.Key("user/key1") <= kdb.Key("user/key2"))
        self.assertTrue(kdb.Key("user/key2") > kdb.Key("user/key1"))
        self.assertTrue(kdb.Key("user/key2") >= kdb.Key("user/key1"))

        self.assertTrue(bool(self.key))
        self.assertTrue(bool(self.bkey))

        self.assertEqual(str(self.key), "user/foo/bar")
        self.assertEqual(str(self.bkey), "system/bkey")
Ejemplo n.º 14
0
 def test_iterator(self):
     self.assertEqual(sum(1 for _ in self.ks), 4)
     self.assertEqual(sum(1 for _ in reversed(self.ks)), 4)
     self.assertEqual(sum(1 for _ in kdb.KeySet(0)), 0)
Ejemplo n.º 15
0
 def setUp(self):
     self.ks = kdb.KeySet(100, kdb.Key("system/key1"),
                          kdb.Key("system/key2"), kdb.Key("user/key3"),
                          kdb.Key("user/key4"), kdb.KS_END,
                          kdb.Key("user/lost"))
Ejemplo n.º 16
0
    def test_functions(self):
        self.assertEqual(self.ks.lookup("user/key3"), kdb.Key("user/key3"))
        self.assertEqual(self.ks.lookup(kdb.Key("system/key2")),
                         kdb.Key("system/key2"))
        self.assertEqual(self.ks.lookup(0), kdb.Key("system/key1"))
        self.assertEqual(self.ks.lookup(-1), kdb.Key("user/key4"))

        ks = kdb.KeySet(0)
        ks.append(kdb.Key("user/foo"))
        ks.append(kdb.Key("user/bar"))
        self.assertEqual(len(ks), 2)

        ' test clear '
        ks = kdb.KeySet(0)
        ks.append(kdb.Key("user/test1"))
        ks.append(kdb.Key("user/test2"))
        ks.append(kdb.Key("user/test3"))
        ks.clear()
        self.assertEqual(len(ks), 0)

        ' test pop '
        ks = kdb.KeySet(0)
        ks.append(kdb.Key("user/test1"))
        ks.append(kdb.Key("user/test2"))
        ks.append(kdb.Key("user/test3"))
        self.assertEqual(ks.pop(), kdb.Key("user/test3"))
        self.assertEqual(len(ks), 2)

        ' test cut '
        ks = kdb.KeySet(0)
        ks.append(kdb.Key("user/level11"))
        ks.append(kdb.Key("user/level13"))
        ks.append(kdb.Key("user/level13/level21"))
        ks.append(kdb.Key("user/level13/level22/level31"))
        ks.append(kdb.Key("user/level13/level23"))
        ks.append(kdb.Key("user/level15"))

        cutresult = ks.cut(kdb.Key("user/level13"))

        self.assertEqual(len(cutresult), 4)
        self.assertTrue(kdb.Key("user/level13") in cutresult)
        self.assertTrue(kdb.Key("user/level13/level21") in cutresult)
        self.assertTrue(kdb.Key("user/level13/level22/level31") in cutresult)
        self.assertTrue(kdb.Key("user/level13/level23") in cutresult)

        self.assertEqual(len(ks), 2)
        self.assertTrue(kdb.Key("user/level11") in ks)
        self.assertTrue(kdb.Key("user/level15") in ks)

        cutresult = ks.cut(kdb.Key("user/does/not/exist"))
        self.assertEqual(len(cutresult), 0)
        self.assertEqual(len(ks), 2)