s4:ldb Add hooks to get/set the flags on a ldb_message_element
authorAndrew Bartlett <abartlet@samba.org>
Wed, 26 Aug 2009 05:01:12 +0000 (15:01 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 26 Aug 2009 05:07:50 +0000 (15:07 +1000)
Also add tests to prove that we got this correct, and correct the
existing tests which used the wrong constants.

Andrew Bartlett

source4/lib/ldb/pyldb.c
source4/lib/ldb/tests/python/api.py

index 67e1d5c9e090e84da7c129e726b0f6ff50320321..94415c8c31de11feb110781cc8ece5d77338357c 100644 (file)
@@ -1493,8 +1493,30 @@ static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObjec
                                                                 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
 }
 
                                                                 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
 }
 
+static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
+{
+       struct ldb_message_element *el;
+
+       el = PyLdbMessageElement_AsMessageElement(self);
+       return PyInt_FromLong(el->flags);
+}
+
+static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args)
+{
+       int flags;
+       struct ldb_message_element *el;
+       if (!PyArg_ParseTuple(args, "i", &flags))
+               return NULL;
+
+       el = PyLdbMessageElement_AsMessageElement(self);
+       el->flags = flags;
+       Py_RETURN_NONE;
+}
+
 static PyMethodDef py_ldb_msg_element_methods[] = {
        { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
 static PyMethodDef py_ldb_msg_element_methods[] = {
        { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
+       { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL },
+       { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
        { NULL },
 };
 
        { NULL },
 };
 
@@ -2326,6 +2348,10 @@ void initldb(void)
        PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
        PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
 
        PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
        PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
 
+       PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
+       PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
+       PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE));
+
        PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
        PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
        PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
        PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
        PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
        PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
index d946bf06a54c647f4d256fd9d8836a7ba86f04b8..9bcb2251905561f92b5af8a387027e007d56538b 100755 (executable)
@@ -195,10 +195,13 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modifydelete")
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modifydelete")
-            m["bla"] = ldb.MessageElement([], ldb.CHANGETYPE_DELETE, "bla")
+            m["bla"] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, "bla")
+            self.assertEquals(ldb.FLAG_MOD_DELETE, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(1, len(rm))
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(1, len(rm))
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(0, len(rm))
         finally:
             l.delete(ldb.Dn(l, "dc=modifydelete"))
 
         finally:
             l.delete(ldb.Dn(l, "dc=modifydelete"))
 
@@ -211,7 +214,8 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=add")
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=add")
-            m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_ADD, "bla")
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
@@ -219,7 +223,7 @@ class SimpleLdb(unittest.TestCase):
         finally:
             l.delete(ldb.Dn(l, "dc=add"))
 
         finally:
             l.delete(ldb.Dn(l, "dc=add"))
 
-    def test_modify_modify(self):
+    def test_modify_replace(self):
         l = ldb.Ldb(filename())
         m = ldb.Message()
         m.dn = ldb.Dn(l, "dc=modify2")
         l = ldb.Ldb(filename())
         m = ldb.Message()
         m.dn = ldb.Dn(l, "dc=modify2")
@@ -228,14 +232,44 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modify2")
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modify2")
-            m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_MODIFY, "bla")
+            m["bla"] = ldb.MessageElement(["789"], ldb.FLAG_MOD_REPLACE, "bla")
+            self.assertEquals(ldb.FLAG_MOD_REPLACE, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
-            self.assertEquals(["1234"], list(rm["bla"]))
+            self.assertEquals(["789"], list(rm["bla"]))
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(1, len(rm))
         finally:
             l.delete(ldb.Dn(l, "dc=modify2"))
 
         finally:
             l.delete(ldb.Dn(l, "dc=modify2"))
 
+    def test_modify_flags_change(self):
+        l = ldb.Ldb(filename())
+        m = ldb.Message()
+        m.dn = ldb.Dn(l, "dc=add")
+        m["bla"] = ["1234"]
+        l.add(m)
+        try:
+            m = ldb.Message()
+            m.dn = ldb.Dn(l, "dc=add")
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
+            l.modify(m)
+            rm = l.search(m.dn)[0]
+            self.assertEquals(2, len(rm))
+            self.assertEquals(["1234", "456"], list(rm["bla"]))
+            
+            #Now create another modify, but switch the flags before we do it
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            m["bla"].set_flags(ldb.FLAG_MOD_DELETE)
+            l.modify(m)
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(1, len(rm))
+            self.assertEquals(["1234"], list(rm["bla"]))
+            
+        finally:
+            l.delete(ldb.Dn(l, "dc=add"))
+
     def test_transaction_commit(self):
         l = ldb.Ldb(filename())
         l.transaction_start()
     def test_transaction_commit(self):
         l = ldb.Ldb(filename())
         l.transaction_start()