diff options
author | Petr Viktorin <pviktori@redhat.com> | 2015-03-11 17:01:53 +0100 |
---|---|---|
committer | Petr Viktorin <pviktori@redhat.com> | 2015-03-11 17:32:32 +0100 |
commit | 34ad28d225771541e3a294bf3782be0839575148 (patch) | |
tree | 94c374ab3fb50ca6ca91c3ab4bd95a3ba7f1599a /lib/ldb/tests/python | |
parent | d479cbfe08b143f65c674a005f2acc9257a30a47 (diff) | |
download | samba-py3compat-34ad28d.tar.gz samba-py3compat-34ad28d.tar.xz samba-py3compat-34ad28d.zip |
[TMP] Port ldb to Python 3py3compat-34ad28d
Diffstat (limited to 'lib/ldb/tests/python')
-rwxr-xr-x | lib/ldb/tests/python/api.py | 210 |
1 files changed, 113 insertions, 97 deletions
diff --git a/lib/ldb/tests/python/api.py b/lib/ldb/tests/python/api.py index d101de864f..faed913f3a 100755 --- a/lib/ldb/tests/python/api.py +++ b/lib/ldb/tests/python/api.py @@ -3,6 +3,7 @@ # Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org> import os +import sys from unittest import TestCase import ldb @@ -54,7 +55,7 @@ class SimpleLdb(TestCase): def test_set_create_perms(self): x = ldb.Ldb() - x.set_create_perms(0600) + x.set_create_perms(0o600) def test_modules_none(self): x = ldb.Ldb() @@ -103,7 +104,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo1") - m["b"] = ["a"] + m["b"] = [b"a"] l.add(m) self.assertRaises(ldb.LdbError, lambda: l.delete(m.dn, ["search_options:1:2"])) l.delete(m.dn) @@ -115,7 +116,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(name) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo3") - m["b"] = ["a"] + m["b"] = [b"a"] l.add(m) try: self.assertTrue(ldb.Dn(l, "dc=foo3") in l) @@ -142,7 +143,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo4") - m["bla"] = "bla" + m["bla"] = b"bla" self.assertEquals(len(l.search()), 0) l.add(m) try: @@ -154,14 +155,14 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo4") - m["bla"] = "bla" + m["bla"] = b"bla" self.assertEquals(len(l.search()), 0) self.assertRaises(ldb.LdbError, lambda: l.add(m,["search_options:1:2"])) def test_add_dict(self): l = ldb.Ldb(filename()) m = {"dn": ldb.Dn(l, "dc=foo5"), - "bla": "bla"} + "bla": b"bla"} self.assertEquals(len(l.search()), 0) l.add(m) try: @@ -171,7 +172,7 @@ class SimpleLdb(TestCase): def test_add_dict_string_dn(self): l = ldb.Ldb(filename()) - m = {"dn": "dc=foo6", "bla": "bla"} + m = {"dn": "dc=foo6", "bla": b"bla"} self.assertEquals(len(l.search()), 0) l.add(m) try: @@ -183,7 +184,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo7") - m["bla"] = "bla" + m["bla"] = b"bla" self.assertEquals(len(l.search()), 0) l.add(m) try: @@ -196,7 +197,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo8") - m["bla"] = "bla" + m["bla"] = b"bla" self.assertEquals(len(l.search()), 0) l.add(m) self.assertEquals(len(l.search()), 1) @@ -210,10 +211,10 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=modifydelete") - m["bla"] = ["1234"] + m["bla"] = [b"1234"] l.add(m) rm = l.search(m.dn)[0] - self.assertEquals(["1234"], list(rm["bla"])) + self.assertEquals([b"1234"], list(rm["bla"])) try: m = ldb.Message() m.dn = ldb.Dn(l, "dc=modifydelete") @@ -231,17 +232,17 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=add") - m["bla"] = ["1234"] + m["bla"] = [b"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") + m["bla"] = ldb.MessageElement([b"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"])) + self.assertEquals([b"1234", b"456"], list(rm["bla"])) finally: l.delete(ldb.Dn(l, "dc=add")) @@ -249,17 +250,17 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=modify2") - m["bla"] = ["1234", "456"] + m["bla"] = [b"1234", b"456"] l.add(m) try: m = ldb.Message() m.dn = ldb.Dn(l, "dc=modify2") - m["bla"] = ldb.MessageElement(["789"], ldb.FLAG_MOD_REPLACE, "bla") + m["bla"] = ldb.MessageElement([b"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)) - self.assertEquals(["789"], list(rm["bla"])) + self.assertEquals([b"789"], list(rm["bla"])) rm = l.search(m.dn, attrs=["bla"])[0] self.assertEquals(1, len(rm)) finally: @@ -269,25 +270,25 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=add") - m["bla"] = ["1234"] + m["bla"] = [b"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") + m["bla"] = ldb.MessageElement([b"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"])) + self.assertEquals([b"1234", b"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"] = ldb.MessageElement([b"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"])) + self.assertEquals([b"1234"], list(rm["bla"])) finally: l.delete(ldb.Dn(l, "dc=add")) @@ -295,7 +296,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) l.transaction_start() m = ldb.Message(ldb.Dn(l, "dc=foo9")) - m["foo"] = ["bar"] + m["foo"] = [b"bar"] l.add(m) l.transaction_commit() l.delete(m.dn) @@ -304,7 +305,7 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) l.transaction_start() m = ldb.Message(ldb.Dn(l, "dc=foo10")) - m["foo"] = ["bar"] + m["foo"] = [b"bar"] l.add(m) l.transaction_cancel() self.assertEquals(0, len(l.search(ldb.Dn(l, "dc=foo10")))) @@ -319,14 +320,14 @@ class SimpleLdb(TestCase): """Testing we do not get trapped in the \0 byte in a property string.""" l = ldb.Ldb(filename()) l.add({ - "dn" : "dc=somedn", - "objectclass" : "user", - "cN" : "LDAPtestUSER", - "givenname" : "ldap", - "displayname" : "foo\0bar", + "dn": "dc=somedn", + "objectclass": b"user", + "cN": b"LDAPtestUSER", + "givenname": b"ldap", + "displayname": b"foo\0bar", }) res = l.search(expression="(dn=dc=somedn)") - self.assertEquals("foo\0bar", res[0]["displayname"][0]) + self.assertEquals(b"foo\0bar", res[0]["displayname"][0]) def test_no_crash_broken_expr(self): l = ldb.Ldb(filename()) @@ -423,7 +424,7 @@ class DnTests(TestCase): def test_parse_ldif(self): msgs = self.ldb.parse_ldif("dn: foo=bar\n") - msg = msgs.next() + msg = next(msgs) self.assertEquals("foo=bar", str(msg[1].dn)) self.assertTrue(isinstance(msg[1], ldb.Message)) ldif = self.ldb.write_ldif(msg[1], ldb.CHANGETYPE_NONE) @@ -431,9 +432,9 @@ class DnTests(TestCase): def test_parse_ldif_more(self): msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar") - msg = msgs.next() + msg = next(msgs) self.assertEquals("foo=bar", str(msg[1].dn)) - msg = msgs.next() + msg = next(msgs) self.assertEquals("bar=bar", str(msg[1].dn)) def test_canonical_string(self): @@ -474,8 +475,14 @@ class LdbMsgTests(TestCase): def test_repr(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "dc=foo29") - self.msg["dc"] = "foo" - self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg)) + self.msg["dc"] = b"foo" + if sys.version_info > (3, 0): + self.assertIn(repr(self.msg), [ + "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement([b'foo'])})", + "Message({'dc': MessageElement([b'foo']), 'dn': Dn('dc=foo29')})", + ]) + else: + self.assertEquals("Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", repr(self.msg)) def test_len(self): self.assertEquals(0, len(self.msg)) @@ -487,37 +494,37 @@ class LdbMsgTests(TestCase): del self.msg["foo"] def test_add(self): - self.msg.add(ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")) + self.msg.add(ldb.MessageElement([b"456"], ldb.FLAG_MOD_ADD, "bla")) def test_elements_empty(self): self.assertEquals([], self.msg.elements()) def test_elements(self): - el = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla") + el = ldb.MessageElement([b"456"], ldb.FLAG_MOD_ADD, "bla") self.msg.add(el) self.assertEquals([el], self.msg.elements()) def test_add_value(self): self.assertEquals(0, len(self.msg)) - self.msg["foo"] = ["foo"] + self.msg["foo"] = [b"foo"] self.assertEquals(1, len(self.msg)) def test_add_value_multiple(self): self.assertEquals(0, len(self.msg)) - self.msg["foo"] = ["foo", "bla"] + self.msg["foo"] = [b"foo", b"bla"] self.assertEquals(1, len(self.msg)) - self.assertEquals(["foo", "bla"], list(self.msg["foo"])) + self.assertEquals([b"foo", b"bla"], list(self.msg["foo"])) def test_set_value(self): - self.msg["foo"] = ["fool"] - self.assertEquals(["fool"], list(self.msg["foo"])) - self.msg["foo"] = ["bar"] - self.assertEquals(["bar"], list(self.msg["foo"])) + self.msg["foo"] = [b"fool"] + self.assertEquals([b"fool"], list(self.msg["foo"])) + self.msg["foo"] = [b"bar"] + self.assertEquals([b"bar"], list(self.msg["foo"])) def test_keys(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") - self.msg["foo"] = ["bla"] - self.msg["bar"] = ["bla"] + self.msg["foo"] = [b"bla"] + self.msg["bar"] = [b"bla"] self.assertEquals(["dn", "foo", "bar"], self.msg.keys()) def test_dn(self): @@ -533,9 +540,9 @@ class LdbMsgTests(TestCase): self.assertRaises(TypeError, self.msg.get, 42) def test_get_other(self): - self.msg["foo"] = ["bar"] - self.assertEquals("bar", self.msg.get("foo")[0]) - self.assertEquals("bar", self.msg.get("foo", idx=0)) + self.msg["foo"] = [b"bar"] + self.assertEquals(b"bar", self.msg.get("foo")[0]) + self.assertEquals(b"bar", self.msg.get("foo", idx=0)) self.assertEquals(None, self.msg.get("foo", idx=1)) self.assertEquals("", self.msg.get("foo", default='', idx=1)) @@ -549,8 +556,8 @@ class LdbMsgTests(TestCase): def test_msg_diff(self): l = ldb.Ldb() msgs = l.parse_ldif("dn: foo=bar\nfoo: bar\nbaz: do\n\ndn: foo=bar\nfoo: bar\nbaz: dont\n") - msg1 = msgs.next()[1] - msg2 = msgs.next()[1] + msg1 = next(msgs)[1] + msg2 = next(msgs)[1] msgdiff = l.msg_diff(msg1, msg2) self.assertEquals("foo=bar", msgdiff.get("dn").__str__()) self.assertRaises(KeyError, lambda: msgdiff["foo"]) @@ -568,16 +575,16 @@ class LdbMsgTests(TestCase): msg2 = ldb.Message() msg2.dn = ldb.Dn(db, "foo=bar") self.assertEquals(msg1, msg2) - msg1['foo'] = 'bar' - msg2['foo'] = 'bar' + msg1['foo'] = b'bar' + msg2['foo'] = b'bar' self.assertEquals(msg1, msg2) - msg2['foo'] = 'blie' + msg2['foo'] = b'blie' self.assertNotEquals(msg1, msg2) - msg2['foo'] = 'blie' + msg2['foo'] = b'blie' def test_from_dict(self): rec = {"dn": "dc=fromdict", - "a1": ["a1-val1", "a1-val1"]} + "a1": [b"a1-val1", b"a1-val1"]} l = ldb.Ldb() # check different types of input Flags for flags in [ldb.FLAG_MOD_ADD, ldb.FLAG_MOD_REPLACE, ldb.FLAG_MOD_DELETE]: @@ -594,63 +601,72 @@ class LdbMsgTests(TestCase): def test_copy_add_message_element(self): m = ldb.Message() - m["1"] = ldb.MessageElement(["val 111"], ldb.FLAG_MOD_ADD, "1") - m["2"] = ldb.MessageElement(["val 222"], ldb.FLAG_MOD_ADD, "2") + m["1"] = ldb.MessageElement([b"val 111"], ldb.FLAG_MOD_ADD, "1") + m["2"] = ldb.MessageElement([b"val 222"], ldb.FLAG_MOD_ADD, "2") mto = ldb.Message() mto["1"] = m["1"] mto["2"] = m["2"] - self.assertEqual(mto["1"], m["1"]) - self.assertEqual(mto["2"], m["2"]) + self.assertEquals(mto["1"], m["1"]) + self.assertEquals(mto["2"], m["2"]) mto = ldb.Message() mto.add(m["1"]) mto.add(m["2"]) - self.assertEqual(mto["1"], m["1"]) - self.assertEqual(mto["2"], m["2"]) + self.assertEquals(mto["1"], m["1"]) + self.assertEquals(mto["2"], m["2"]) class MessageElementTests(TestCase): def test_cmp_element(self): - x = ldb.MessageElement(["foo"]) - y = ldb.MessageElement(["foo"]) - z = ldb.MessageElement(["bzr"]) + x = ldb.MessageElement([b"foo"]) + y = ldb.MessageElement([b"foo"]) + z = ldb.MessageElement([b"bzr"]) self.assertEquals(x, y) self.assertNotEquals(x, z) def test_create_iterable(self): - x = ldb.MessageElement(["foo"]) - self.assertEquals(["foo"], list(x)) + x = ldb.MessageElement([b"foo"]) + self.assertEquals([b"foo"], list(x)) def test_repr(self): - x = ldb.MessageElement(["foo"]) - self.assertEquals("MessageElement(['foo'])", repr(x)) - x = ldb.MessageElement(["foo", "bla"]) + x = ldb.MessageElement([b"foo"]) + if sys.version_info > (3, 0): + self.assertEquals("MessageElement([b'foo'])", repr(x)) + else: + self.assertEquals("MessageElement(['foo'])", repr(x)) + x = ldb.MessageElement([b"foo", b"bla"]) self.assertEquals(2, len(x)) - self.assertEquals("MessageElement(['foo','bla'])", repr(x)) + if sys.version_info > (3, 0): + self.assertEquals("MessageElement([b'foo',b'bla'])", repr(x)) + else: + self.assertEquals("MessageElement(['foo','bla'])", repr(x)) def test_get_item(self): - x = ldb.MessageElement(["foo", "bar"]) - self.assertEquals("foo", x[0]) - self.assertEquals("bar", x[1]) - self.assertEquals("bar", x[-1]) + x = ldb.MessageElement([b"foo", b"bar"]) + self.assertEquals(b"foo", x[0]) + self.assertEquals(b"bar", x[1]) + self.assertEquals(b"bar", x[-1]) self.assertRaises(IndexError, lambda: x[45]) def test_len(self): - x = ldb.MessageElement(["foo", "bar"]) + x = ldb.MessageElement([b"foo", b"bar"]) self.assertEquals(2, len(x)) def test_eq(self): - x = ldb.MessageElement(["foo", "bar"]) - y = ldb.MessageElement(["foo", "bar"]) + x = ldb.MessageElement([b"foo", b"bar"]) + y = ldb.MessageElement([b"foo", b"bar"]) self.assertEquals(y, x) - x = ldb.MessageElement(["foo"]) + x = ldb.MessageElement([b"foo"]) self.assertNotEquals(y, x) - y = ldb.MessageElement(["foo"]) + y = ldb.MessageElement([b"foo"]) self.assertEquals(y, x) def test_extended(self): - el = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla") - self.assertEquals("MessageElement(['456'])", repr(el)) + el = ldb.MessageElement([b"456"], ldb.FLAG_MOD_ADD, "bla") + if sys.version_info > (3, 0): + self.assertEquals("MessageElement([b'456'])", repr(el)) + else: + self.assertEquals("MessageElement(['456'])", repr(el)) class ModuleTests(TestCase): @@ -680,7 +696,7 @@ class ModuleTests(TestCase): if os.path.exists(name): os.unlink(name) l = ldb.Ldb(name) - l.add({"dn": "@MODULES", "@LIST": "bla"}) + l.add({"dn": "@MODULES", "@LIST": b"bla"}) self.assertEquals([], ops) l = ldb.Ldb(name) self.assertEquals(["init"], ops) @@ -694,19 +710,19 @@ class LdbResultTests(TestCase): if os.path.exists(name): os.unlink(name) self.l = ldb.Ldb(name) - self.l.add({"dn": "DC=SAMBA,DC=ORG", "name": "samba.org"}) - self.l.add({"dn": "OU=ADMIN,DC=SAMBA,DC=ORG", "name": "Admins"}) - self.l.add({"dn": "OU=USERS,DC=SAMBA,DC=ORG", "name": "Users"}) - self.l.add({"dn": "OU=OU1,DC=SAMBA,DC=ORG", "name": "OU #1"}) - self.l.add({"dn": "OU=OU2,DC=SAMBA,DC=ORG", "name": "OU #2"}) - self.l.add({"dn": "OU=OU3,DC=SAMBA,DC=ORG", "name": "OU #3"}) - self.l.add({"dn": "OU=OU4,DC=SAMBA,DC=ORG", "name": "OU #4"}) - self.l.add({"dn": "OU=OU5,DC=SAMBA,DC=ORG", "name": "OU #5"}) - self.l.add({"dn": "OU=OU6,DC=SAMBA,DC=ORG", "name": "OU #6"}) - self.l.add({"dn": "OU=OU7,DC=SAMBA,DC=ORG", "name": "OU #7"}) - self.l.add({"dn": "OU=OU8,DC=SAMBA,DC=ORG", "name": "OU #8"}) - self.l.add({"dn": "OU=OU9,DC=SAMBA,DC=ORG", "name": "OU #9"}) - self.l.add({"dn": "OU=OU10,DC=SAMBA,DC=ORG", "name": "OU #10"}) + self.l.add({"dn": "DC=SAMBA,DC=ORG", "name": b"samba.org"}) + self.l.add({"dn": "OU=ADMIN,DC=SAMBA,DC=ORG", "name": b"Admins"}) + self.l.add({"dn": "OU=USERS,DC=SAMBA,DC=ORG", "name": b"Users"}) + self.l.add({"dn": "OU=OU1,DC=SAMBA,DC=ORG", "name": b"OU #1"}) + self.l.add({"dn": "OU=OU2,DC=SAMBA,DC=ORG", "name": b"OU #2"}) + self.l.add({"dn": "OU=OU3,DC=SAMBA,DC=ORG", "name": b"OU #3"}) + self.l.add({"dn": "OU=OU4,DC=SAMBA,DC=ORG", "name": b"OU #4"}) + self.l.add({"dn": "OU=OU5,DC=SAMBA,DC=ORG", "name": b"OU #5"}) + self.l.add({"dn": "OU=OU6,DC=SAMBA,DC=ORG", "name": b"OU #6"}) + self.l.add({"dn": "OU=OU7,DC=SAMBA,DC=ORG", "name": b"OU #7"}) + self.l.add({"dn": "OU=OU8,DC=SAMBA,DC=ORG", "name": b"OU #8"}) + self.l.add({"dn": "OU=OU9,DC=SAMBA,DC=ORG", "name": b"OU #9"}) + self.l.add({"dn": "OU=OU10,DC=SAMBA,DC=ORG", "name": b"OU #10"}) def tearDown(self): super(LdbResultTests, self).tearDown() @@ -739,7 +755,7 @@ class LdbResultTests(TestCase): def test_iter_msgs_count(self): self.assertTrue(self.l.search().count > 0) # 13 objects has been added to the DC=SAMBA, DC=ORG - self.assertEqual(self.l.search(base="DC=SAMBA,DC=ORG").count, 13) + self.assertEquals(self.l.search(base="DC=SAMBA,DC=ORG").count, 13) def test_iter_controls(self): res = self.l.search().controls |