Add dn.insert() and update unittest

--
John Dennis <jden...@redhat.com>

Looking to carve out IT costs?
www.redhat.com/carveoutcosts/
From 3f4ea9affb47fc9cdbc9436b7e74437c3de6f344 Mon Sep 17 00:00:00 2001
From: John Dennis <jden...@redhat.com>
Date: Wed, 3 Aug 2011 19:14:51 -0400
Subject: [PATCH 34/34] ticket 1568 - DN objects should support the insert
 method
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Add dn.insert() and update unittest
---
 ipalib/dn.py                 |   30 +++++++++++++++++++++++++++---
 tests/test_ipalib/test_dn.py |    9 +++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/ipalib/dn.py b/ipalib/dn.py
index 1311b6a..0eac711 100644
--- a/ipalib/dn.py
+++ b/ipalib/dn.py
@@ -1004,9 +1004,19 @@ class DN(object):
     dn[:]
 
     # Set the 2nd and 3rd RDN using slices (all are equivalent)
-    dn[1:3] = ('cn', 'Bob), ('dc', 'redhat.com')
-    dn[1:3] = [['cn', 'Bob], ['dc', 'redhat.com']]
-    dn[1:3] = RDN('cn', 'Bob), RDN('dc', 'redhat.com')
+    dn[1:3] = ('cn', 'Bob'), ('dc', 'redhat.com')
+    dn[1:3] = [['cn', 'Bob'], ['dc', 'redhat.com']]
+    dn[1:3] = RDN('cn', 'Bob'), RDN('dc', 'redhat.com')
+
+    DN objects support the insert operation.
+
+    dn.insert(i,x) is exactly equivalent to dn[i:i] = [x], thus the following
+    are all equivalent:
+
+    dn.insert(i, ('cn','Bob'))
+    dn.insert(i, ['cn','Bob'])
+    dn.insert(i, RDN(('cn','Bob')))
+    dn[i:i] = [('cn','Bob')]
 
     DN objects support equality testing and comparision. See RDN for the
     definition of the comparision method.
@@ -1214,6 +1224,20 @@ class DN(object):
 
         return self
 
+    def insert(self, i, x):
+        '''
+        x must be a 2-value tuple or list promotable to an RDN object,
+        or a RDN object.
+
+        dn.insert(i, x) is the same as s[i:i] = [x]
+
+        When a negative index is passed as the first parameter to the
+        insert() method, the list length is added, as for slice
+        indices. If it is still negative, it is truncated to zero, as
+        for slice indices.
+        '''
+        self.rdns.insert(i, self._rdn_from_value(x))
+
     # The implementation of startswith, endswith, tailmatch, adjust_indices
     # was based on the Python's stringobject.c implementation
 
diff --git a/tests/test_ipalib/test_dn.py b/tests/test_ipalib/test_dn.py
index c647460..f4aa0aa 100644
--- a/tests/test_ipalib/test_dn.py
+++ b/tests/test_ipalib/test_dn.py
@@ -870,6 +870,15 @@ class TestDN(unittest.TestCase):
             slice_rdn = RDN(dn_slice[i])
             self.assertEqual(slice_rdn, query_rdn)
 
+        # insert
+        dn = DN(self.rdn2)
+        dn.insert(0, self.rdn1)
+        self.assertEqual(dn, self.dn3)
+
+        dn = DN(self.rdn1)
+        dn.insert(1, (self.attr2, self.value2))
+        self.assertEqual(dn, self.dn3)
+
         # Slices
         # Assign via RDN
         rdn_args = make_rdn_args(dn_low, dn_high, 'tuple',
-- 
1.7.4.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to