From e95838591b7d5f66ffde248022136f23eed5bc33 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 29 Jul 2015 12:45:43 +0200 Subject: [PATCH] pyldb: Adapt tests to Python 3 Signed-off-by: Petr Viktorin Reviewed-by: Andrew Bartlett Reviewed-by: Stefan Metzmacher --- lib/ldb/tests/python/api.py | 405 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 330 insertions(+), 75 deletions(-) diff --git a/lib/ldb/tests/python/api.py b/lib/ldb/tests/python/api.py index f365027bb27..2d9537fc2eb 100755 --- a/lib/ldb/tests/python/api.py +++ b/lib/ldb/tests/python/api.py @@ -4,9 +4,12 @@ import os from unittest import TestCase +import sys import ldb +PY3 = sys.version_info > (3, 0) + def filename(): import tempfile @@ -32,9 +35,12 @@ class NoContextTests(TestCase): self.assertEqual(1195499412, ldb.string_to_time("20071119191012.0Z")) def test_binary_encode(self): - encoded = ldb.binary_encode('test\\x') + encoded = ldb.binary_encode(b'test\\x') decoded = ldb.binary_decode(encoded) - self.assertEqual(decoded, 'test\\x') + self.assertEqual(decoded, b'test\\x') + + encoded2 = ldb.binary_encode('test\\x') + self.assertEqual(encoded2, encoded) class SimpleLdb(TestCase): @@ -83,6 +89,7 @@ class SimpleLdb(TestCase): def test_search_attr_string(self): l = ldb.Ldb(filename()) self.assertRaises(TypeError, l.search, attrs="dc") + self.assertRaises(TypeError, l.search, attrs=b"dc") def test_opaque(self): l = ldb.Ldb(filename()) @@ -103,7 +110,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) @@ -142,6 +149,18 @@ class SimpleLdb(TestCase): l = ldb.Ldb(filename()) m = ldb.Message() m.dn = ldb.Dn(l, "dc=foo4") + m["bla"] = b"bla" + self.assertEqual(len(l.search()), 0) + l.add(m) + try: + self.assertEqual(len(l.search()), 1) + finally: + l.delete(ldb.Dn(l, "dc=foo4")) + + def test_add_text(self): + l = ldb.Ldb(filename()) + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=foo4") m["bla"] = "bla" self.assertEqual(len(l.search()), 0) l.add(m) @@ -154,13 +173,24 @@ 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.assertEqual(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": b"bla"} + self.assertEqual(len(l.search()), 0) + l.add(m) + try: + self.assertEqual(len(l.search()), 1) + finally: + l.delete(ldb.Dn(l, "dc=foo5")) + + def test_add_dict_text(self): + l = ldb.Ldb(filename()) + m = {"dn": ldb.Dn(l, "dc=foo5"), "bla": "bla"} self.assertEqual(len(l.search()), 0) l.add(m) @@ -171,7 +201,17 @@ 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.assertEqual(len(l.search()), 0) + l.add(m) + try: + self.assertEqual(len(l.search()), 1) + finally: + l.delete(ldb.Dn(l, "dc=foo6")) + + def test_add_dict_bytes_dn(self): + l = ldb.Ldb(filename()) + m = {"dn": b"dc=foo6", "bla": b"bla"} self.assertEqual(len(l.search()), 0) l.add(m) try: @@ -183,7 +223,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.assertEqual(len(l.search()), 0) l.add(m) try: @@ -196,7 +236,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.assertEqual(len(l.search()), 0) l.add(m) self.assertEqual(len(l.search()), 1) @@ -210,10 +250,31 @@ 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.assertEqual([b"1234"], list(rm["bla"])) + try: + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=modifydelete") + m["bla"] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, "bla") + self.assertEqual(ldb.FLAG_MOD_DELETE, m["bla"].flags()) + l.modify(m) + rm = l.search(m.dn)[0] + self.assertEqual(1, len(rm)) + rm = l.search(m.dn, attrs=["bla"]) + self.assertEqual(0, len(rm)) + finally: + l.delete(ldb.Dn(l, "dc=modifydelete")) + + def test_modify_delete_text(self): + l = ldb.Ldb(filename()) + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=modifydelete") + m.text["bla"] = ["1234"] l.add(m) rm = l.search(m.dn)[0] - self.assertEqual(["1234"], list(rm["bla"])) + self.assertEqual(["1234"], list(rm.text["bla"])) try: m = ldb.Message() m.dn = ldb.Dn(l, "dc=modifydelete") @@ -231,7 +292,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([b"456"], ldb.FLAG_MOD_ADD, "bla") + self.assertEqual(ldb.FLAG_MOD_ADD, m["bla"].flags()) + l.modify(m) + rm = l.search(m.dn)[0] + self.assertEqual(2, len(rm)) + self.assertEqual([b"1234", b"456"], list(rm["bla"])) + finally: + l.delete(ldb.Dn(l, "dc=add")) + + def test_modify_add_text(self): + l = ldb.Ldb(filename()) + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=add") + m.text["bla"] = ["1234"] l.add(m) try: m = ldb.Message() @@ -241,7 +320,7 @@ class SimpleLdb(TestCase): l.modify(m) rm = l.search(m.dn)[0] self.assertEqual(2, len(rm)) - self.assertEqual(["1234", "456"], list(rm["bla"])) + self.assertEqual(["1234", "456"], list(rm.text["bla"])) finally: l.delete(ldb.Dn(l, "dc=add")) @@ -249,7 +328,27 @@ 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([b"789"], ldb.FLAG_MOD_REPLACE, "bla") + self.assertEqual(ldb.FLAG_MOD_REPLACE, m["bla"].flags()) + l.modify(m) + rm = l.search(m.dn)[0] + self.assertEqual(2, len(rm)) + self.assertEqual([b"789"], list(rm["bla"])) + rm = l.search(m.dn, attrs=["bla"])[0] + self.assertEqual(1, len(rm)) + finally: + l.delete(ldb.Dn(l, "dc=modify2")) + + def test_modify_replace_text(self): + l = ldb.Ldb(filename()) + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=modify2") + m.text["bla"] = ["1234", "456"] l.add(m) try: m = ldb.Message() @@ -259,7 +358,7 @@ class SimpleLdb(TestCase): l.modify(m) rm = l.search(m.dn)[0] self.assertEqual(2, len(rm)) - self.assertEqual(["789"], list(rm["bla"])) + self.assertEqual(["789"], list(rm.text["bla"])) rm = l.search(m.dn, attrs=["bla"])[0] self.assertEqual(1, len(rm)) finally: @@ -269,7 +368,33 @@ 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([b"456"], ldb.FLAG_MOD_ADD, "bla") + self.assertEqual(ldb.FLAG_MOD_ADD, m["bla"].flags()) + l.modify(m) + rm = l.search(m.dn)[0] + self.assertEqual(2, len(rm)) + self.assertEqual([b"1234", b"456"], list(rm["bla"])) + + # Now create another modify, but switch the flags before we do it + 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.assertEqual(1, len(rm)) + self.assertEqual([b"1234"], list(rm["bla"])) + finally: + l.delete(ldb.Dn(l, "dc=add")) + + def test_modify_flags_change_text(self): + l = ldb.Ldb(filename()) + m = ldb.Message() + m.dn = ldb.Dn(l, "dc=add") + m.text["bla"] = ["1234"] l.add(m) try: m = ldb.Message() @@ -279,7 +404,7 @@ class SimpleLdb(TestCase): l.modify(m) rm = l.search(m.dn)[0] self.assertEqual(2, len(rm)) - self.assertEqual(["1234", "456"], list(rm["bla"])) + self.assertEqual(["1234", "456"], list(rm.text["bla"])) # Now create another modify, but switch the flags before we do it m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla") @@ -287,7 +412,7 @@ class SimpleLdb(TestCase): l.modify(m) rm = l.search(m.dn, attrs=["bla"])[0] self.assertEqual(1, len(rm)) - self.assertEqual(["1234"], list(rm["bla"])) + self.assertEqual(["1234"], list(rm.text["bla"])) finally: l.delete(ldb.Dn(l, "dc=add")) @@ -295,7 +420,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 +429,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.assertEqual(0, len(l.search(ldb.Dn(l, "dc=foo10")))) @@ -319,14 +444,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" : b"dc=somedn", + "objectclass" : b"user", + "cN" : b"LDAPtestUSER", + "givenname" : b"ldap", + "displayname" : b"foo\0bar", }) res = l.search(expression="(dn=dc=somedn)") - self.assertEqual("foo\0bar", res[0]["displayname"][0]) + self.assertEqual(b"foo\0bar", res[0]["displayname"][0]) def test_no_crash_broken_expr(self): l = ldb.Ldb(filename()) @@ -503,11 +628,23 @@ class LdbMsgTests(TestCase): def test_repr(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "dc=foo29") - self.msg["dc"] = "foo" - self.assertIn(repr(self.msg), [ - "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})", - "Message({'dc': MessageElement(['foo']), 'dn': Dn('dc=foo29')})", - ]) + self.msg["dc"] = b"foo" + if PY3: + self.assertIn(repr(self.msg), [ + "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement([b'foo'])})", + "Message({'dc': MessageElement([b'foo']), 'dn': Dn('dc=foo29')})", + ]) + self.assertIn(repr(self.msg.text), [ + "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement([b'foo'])}).text", + "Message({'dc': MessageElement([b'foo']), 'dn': Dn('dc=foo29')}).text", + ]) + else: + self.assertEquals( + repr(self.msg), + "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])})") + self.assertEquals( + repr(self.msg.text), + "Message({'dn': Dn('dc=foo29'), 'dc': MessageElement(['foo'])}).text") def test_len(self): self.assertEqual(0, len(self.msg)) @@ -519,38 +656,65 @@ class LdbMsgTests(TestCase): del self.msg["foo"] def test_add(self): + self.msg.add(ldb.MessageElement([b"456"], ldb.FLAG_MOD_ADD, "bla")) + + def test_add_text(self): self.msg.add(ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")) def test_elements_empty(self): self.assertEqual([], 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.assertEqual([el], self.msg.elements()) + self.assertEqual([el.text], self.msg.text.elements()) def test_add_value(self): self.assertEqual(0, len(self.msg)) + self.msg["foo"] = [b"foo"] + self.assertEqual(1, len(self.msg)) + + def test_add_value_text(self): + self.assertEqual(0, len(self.msg)) self.msg["foo"] = ["foo"] self.assertEqual(1, len(self.msg)) def test_add_value_multiple(self): self.assertEqual(0, len(self.msg)) + self.msg["foo"] = [b"foo", b"bla"] + self.assertEqual(1, len(self.msg)) + self.assertEqual([b"foo", b"bla"], list(self.msg["foo"])) + + def test_add_value_multiple_text(self): + self.assertEqual(0, len(self.msg)) self.msg["foo"] = ["foo", "bla"] self.assertEqual(1, len(self.msg)) - self.assertEqual(["foo", "bla"], list(self.msg["foo"])) + self.assertEqual(["foo", "bla"], list(self.msg.text["foo"])) def test_set_value(self): + self.msg["foo"] = [b"fool"] + self.assertEqual([b"fool"], list(self.msg["foo"])) + self.msg["foo"] = [b"bar"] + self.assertEqual([b"bar"], list(self.msg["foo"])) + + def test_set_value_text(self): self.msg["foo"] = ["fool"] - self.assertEqual(["fool"], list(self.msg["foo"])) + self.assertEqual(["fool"], list(self.msg.text["foo"])) self.msg["foo"] = ["bar"] - self.assertEqual(["bar"], list(self.msg["foo"])) + self.assertEqual(["bar"], list(self.msg.text["foo"])) def test_keys(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") + self.msg["foo"] = [b"bla"] + self.msg["bar"] = [b"bla"] + self.assertEqual(["dn", "foo", "bar"], self.msg.keys()) + + def test_keys_text(self): + self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") self.msg["foo"] = ["bla"] self.msg["bar"] = ["bla"] - self.assertEqual(["dn", "foo", "bar"], self.msg.keys()) + self.assertEqual(["dn", "foo", "bar"], self.msg.text.keys()) def test_dn(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") @@ -560,14 +724,32 @@ class LdbMsgTests(TestCase): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") self.assertEqual("@BASEINFO", self.msg.get("dn").__str__()) + def test_dn_text(self): + self.msg.text.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") + self.assertEqual("@BASEINFO", str(self.msg.dn)) + self.assertEqual("@BASEINFO", str(self.msg.text.dn)) + + def test_get_dn_text(self): + self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") + self.assertEqual("@BASEINFO", str(self.msg.get("dn"))) + self.assertEqual("@BASEINFO", str(self.msg.text.get("dn"))) + def test_get_invalid(self): self.msg.dn = ldb.Dn(ldb.Ldb(filename()), "@BASEINFO") self.assertRaises(TypeError, self.msg.get, 42) def test_get_other(self): + self.msg["foo"] = [b"bar"] + self.assertEqual(b"bar", self.msg.get("foo")[0]) + self.assertEqual(b"bar", self.msg.get("foo", idx=0)) + self.assertEqual(None, self.msg.get("foo", idx=1)) + self.assertEqual("", self.msg.get("foo", default='', idx=1)) + + def test_get_other_text(self): self.msg["foo"] = ["bar"] - self.assertEqual("bar", self.msg.get("foo")[0]) - self.assertEqual("bar", self.msg.get("foo", idx=0)) + self.assertEqual(["bar"], list(self.msg.text.get("foo"))) + self.assertEqual("bar", self.msg.text.get("foo")[0]) + self.assertEqual("bar", self.msg.text.get("foo", idx=0)) self.assertEqual(None, self.msg.get("foo", idx=1)) self.assertEqual("", self.msg.get("foo", default='', idx=1)) @@ -575,9 +757,16 @@ class LdbMsgTests(TestCase): self.assertEqual(None, self.msg.get("tatayoyo", idx=0)) self.assertEqual("anniecordie", self.msg.get("tatayoyo", "anniecordie")) + def test_get_default_text(self): + self.assertEqual(None, self.msg.text.get("tatayoyo", idx=0)) + self.assertEqual("anniecordie", self.msg.text.get("tatayoyo", "anniecordie")) + def test_get_unknown(self): self.assertEqual(None, self.msg.get("lalalala")) + def test_get_unknown_text(self): + self.assertEqual(None, self.msg.text.get("lalalala")) + 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") @@ -600,16 +789,16 @@ class LdbMsgTests(TestCase): msg2 = ldb.Message() msg2.dn = ldb.Dn(db, "foo=bar") self.assertEqual(msg1, msg2) - msg1['foo'] = 'bar' - msg2['foo'] = 'bar' + msg1['foo'] = b'bar' + msg2['foo'] = b'bar' self.assertEqual(msg1, msg2) - msg2['foo'] = 'blie' + msg2['foo'] = b'blie' self.assertNotEqual(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]: @@ -621,13 +810,30 @@ class LdbMsgTests(TestCase): self.assertRaises(TypeError, ldb.Message.from_dict, l, list(), ldb.FLAG_MOD_REPLACE) self.assertRaises(ValueError, ldb.Message.from_dict, l, rec, 0) # Message.from_dict expects dictionary with 'dn' + err_rec = {"a1": [b"a1-val1", b"a1-val1"]} + self.assertRaises(TypeError, ldb.Message.from_dict, l, err_rec, ldb.FLAG_MOD_REPLACE) + + def test_from_dict_text(self): + rec = {"dn": "dc=fromdict", + "a1": ["a1-val1", "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]: + m = ldb.Message.from_dict(l, rec, flags) + self.assertEqual(rec["a1"], list(m.text["a1"])) + self.assertEqual(flags, m.text["a1"].flags()) + # check input params + self.assertRaises(TypeError, ldb.Message.from_dict, dict(), rec, ldb.FLAG_MOD_REPLACE) + self.assertRaises(TypeError, ldb.Message.from_dict, l, list(), ldb.FLAG_MOD_REPLACE) + self.assertRaises(ValueError, ldb.Message.from_dict, l, rec, 0) + # Message.from_dict expects dictionary with 'dn' err_rec = {"a1": ["a1-val1", "a1-val1"]} self.assertRaises(TypeError, ldb.Message.from_dict, l, err_rec, ldb.FLAG_MOD_REPLACE) 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"] @@ -639,50 +845,99 @@ class LdbMsgTests(TestCase): self.assertEqual(mto["1"], m["1"]) self.assertEqual(mto["2"], m["2"]) + def test_copy_add_message_element_text(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") + mto = ldb.Message() + mto["1"] = m["1"] + mto["2"] = m["2"] + self.assertEqual(mto["1"], m.text["1"]) + self.assertEqual(mto["2"], m.text["2"]) + mto = ldb.Message() + mto.add(m["1"]) + mto.add(m["2"]) + self.assertEqual(mto.text["1"], m.text["1"]) + self.assertEqual(mto.text["2"], m.text["2"]) + self.assertEqual(mto["1"], m["1"]) + self.assertEqual(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.assertEqual(x, y) self.assertNotEqual(x, z) + def test_cmp_element_text(self): + x = ldb.MessageElement([b"foo"]) + y = ldb.MessageElement(["foo"]) + self.assertEqual(x, y) + def test_create_iterable(self): - x = ldb.MessageElement(["foo"]) - self.assertEqual(["foo"], list(x)) + x = ldb.MessageElement([b"foo"]) + self.assertEqual([b"foo"], list(x)) + self.assertEqual(["foo"], list(x.text)) def test_repr(self): - x = ldb.MessageElement(["foo"]) - self.assertEqual("MessageElement(['foo'])", repr(x)) - x = ldb.MessageElement(["foo", "bla"]) + x = ldb.MessageElement([b"foo"]) + if PY3: + self.assertEqual("MessageElement([b'foo'])", repr(x)) + self.assertEqual("MessageElement([b'foo']).text", repr(x.text)) + else: + self.assertEqual("MessageElement(['foo'])", repr(x)) + self.assertEqual("MessageElement(['foo']).text", repr(x.text)) + x = ldb.MessageElement([b"foo", b"bla"]) self.assertEqual(2, len(x)) - self.assertEqual("MessageElement(['foo','bla'])", repr(x)) + if PY3: + self.assertEqual("MessageElement([b'foo',b'bla'])", repr(x)) + self.assertEqual("MessageElement([b'foo',b'bla']).text", repr(x.text)) + else: + self.assertEqual("MessageElement(['foo','bla'])", repr(x)) + self.assertEqual("MessageElement(['foo','bla']).text", repr(x.text)) def test_get_item(self): + x = ldb.MessageElement([b"foo", b"bar"]) + self.assertEqual(b"foo", x[0]) + self.assertEqual(b"bar", x[1]) + self.assertEqual(b"bar", x[-1]) + self.assertRaises(IndexError, lambda: x[45]) + + def test_get_item_text(self): x = ldb.MessageElement(["foo", "bar"]) - self.assertEqual("foo", x[0]) - self.assertEqual("bar", x[1]) - self.assertEqual("bar", x[-1]) + self.assertEqual("foo", x.text[0]) + self.assertEqual("bar", x.text[1]) + self.assertEqual("bar", x.text[-1]) self.assertRaises(IndexError, lambda: x[45]) def test_len(self): - x = ldb.MessageElement(["foo", "bar"]) + x = ldb.MessageElement([b"foo", b"bar"]) self.assertEqual(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.assertEqual(y, x) - x = ldb.MessageElement(["foo"]) + x = ldb.MessageElement([b"foo"]) self.assertNotEqual(y, x) - y = ldb.MessageElement(["foo"]) + y = ldb.MessageElement([b"foo"]) self.assertEqual(y, x) def test_extended(self): - el = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla") - self.assertEqual("MessageElement(['456'])", repr(el)) + el = ldb.MessageElement([b"456"], ldb.FLAG_MOD_ADD, "bla") + if PY3: + self.assertEqual("MessageElement([b'456'])", repr(el)) + self.assertEqual("MessageElement([b'456']).text", repr(el.text)) + else: + self.assertEqual("MessageElement(['456'])", repr(el)) + self.assertEqual("MessageElement(['456']).text", repr(el.text)) + + def test_bad_text(self): + el = ldb.MessageElement(b'\xba\xdd') + self.assertRaises(UnicodeDecodeError, el.text.__getitem__, 0) class ModuleTests(TestCase): @@ -726,19 +981,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() -- 2.11.4.GIT