def testBinaryEmptyString(self): # test to make sure an empty Binary is converted to an empty string if sys.version_info[0] < 3: b = psycopg2.Binary('') self.assertEqual(str(b), "''::bytea") else: b = psycopg2.Binary(bytes([])) self.assertEqual(str(b), "''::bytea")
def testBinary(self): if sys.version_info[0] < 3: s = ''.join([chr(x) for x in range(256)]) b = psycopg2.Binary(s) buf = self.execute("SELECT %s::bytea AS foo", (b, )) self.assertEqual(s, str(buf)) else: s = bytes(range(256)) b = psycopg2.Binary(s) buf = self.execute("SELECT %s::bytea AS foo", (b, )) self.assertEqual(s, buf.tobytes())
def testBinaryRoundTrip(self): # test to make sure buffers returned by psycopg2 are # understood by execute: if sys.version_info[0] < 3: s = ''.join([chr(x) for x in range(256)]) buf = self.execute("SELECT %s::bytea AS foo", (psycopg2.Binary(s), )) buf2 = self.execute("SELECT %s::bytea AS foo", (buf, )) self.assertEqual(s, str(buf2)) else: s = bytes(range(256)) buf = self.execute("SELECT %s::bytea AS foo", (psycopg2.Binary(s), )) buf2 = self.execute("SELECT %s::bytea AS foo", (buf, )) self.assertEqual(s, buf2.tobytes())
def testByteaHexCheckFalsePositive(self): # the check \x -> x to detect bad bytea decode # may be fooled if the first char is really an 'x' o1 = psycopg2.Binary(b('x')) o2 = self.execute("SELECT %s::bytea AS foo", (o1, )) if sys.version_info[:2] < (3, 3): self.assertEqual(b('x'), o2[0]) else: self.assertEqual(ord('x'), o2[0])
def test_binary(self): data = b"""some data with \000\013 binary stuff into, 'quotes' and \\ a backslash too. """ if sys.version_info[0] < 3: data += "".join(map(chr, range(256))) else: data += bytes(range(256)) curs = self.conn.cursor() curs.execute("SELECT %s::bytea;", (psycopg2.Binary(data), )) if sys.version_info[0] < 3: res = str(curs.fetchone()[0]) else: res = curs.fetchone()[0].tobytes() if res[0] in (b'x', ord(b'x')) and self.conn.server_version >= 90000: return self.skipTest("bytea broken with server >= 9.0, libpq < 9") self.assertEqual(res, data) self.assert_(not self.conn.notices)
def testBinaryNone(self): b = psycopg2.Binary(None) buf = self.execute("SELECT %s::bytea AS foo", (b, )) self.assertEqual(buf, None)