def test_unicode(self): curs = self.conn.cursor() curs.execute("SHOW server_encoding") server_encoding = curs.fetchone()[0] if server_encoding != "UTF8": return self.skipTest( "Unicode test skipped since server encoding is %s" % server_encoding) data = _u(b"""some data with \t chars to escape into, 'quotes', \xe2\x82\xac euro sign and \\ a backslash too. """) _unichr = chr if six.PY3 else unichr data += _u(b"").join( map(_unichr, [u for u in range(1, 65536) if not 0xD800 <= u <= 0xDFFF ])) # surrogate area self.conn.set_client_encoding('UNICODE') extensions.register_type(extensions.UNICODE, self.conn) curs.execute("SELECT %s::text;", (data, )) res = curs.fetchone()[0] self.assertEqual(res, data) self.assert_(not self.conn.notices)
def test_notices_utf8(self): conn = self.conn cur = conn.cursor() if self.conn.server_version >= 90300: cur.execute("set client_min_messages=debug1") cur.execute(_u(b("create temp table мыыchatty (id serial primary key);"))) self.assertEqual("CREATE TABLE", cur.statusmessage) self.assert_(conn.notices) self.assert_(_u(b('мыыchatty')) in conn.notices[0])
def test_xid_unicode(self): cnn = self.connect() x1 = cnn.xid(10, _u(b'uni'), _u(b'code')) cnn.tpc_begin(x1) cnn.tpc_prepare() cnn.reset() xid = [xid for xid in cnn.tpc_recover() if xid.database == dbname][0] self.assertEqual(10, xid.format_id) self.assertEqual('uni', xid.gtrid) self.assertEqual('code', xid.bqual)
def test_read_text(self): lo = self.conn.lobject() snowman = _u(b'\xe2\x98\x83') length = lo.write(_u(b"some data ") + snowman) lo.close() lo = self.conn.lobject(lo.oid, "rt") x = lo.read(4) self.assertEqual(type(x), type(_u(b''))) self.assertEqual(x, _u(b"some")) self.assertEqual(lo.read(), _u(b" data ") + snowman)
def test_xid_unicode(self): cnn = self.connect() x1 = cnn.xid(10, _u(b'uni'), _u(b'code')) cnn.tpc_begin(x1) cnn.tpc_prepare() cnn.reset() xid = [ xid for xid in cnn.tpc_recover() if xid.database == dbname ][0] self.assertEqual(10, xid.format_id) self.assertEqual('uni', xid.gtrid) self.assertEqual('code', xid.bqual)
def test_encoding_name(self): self.conn.set_client_encoding("EUC_JP") # conn.encoding is 'EUCJP' now. cur = self.conn.cursor() extensions.register_type(extensions.UNICODE, cur) cur.execute("select 'foo'::text;") self.assertEqual(cur.fetchone()[0], _u(b'foo'))
def test_mogrify_unicode(self): conn = self.conn cur = conn.cursor() # test consistency between execute and mogrify. # unicode query containing only ascii data cur.execute(_u(b"SELECT 'foo';")) self.assertEqual('foo', cur.fetchone()[0]) self.assertEqual(b"SELECT 'foo';", cur.mogrify(_u(b"SELECT 'foo';"))) conn.set_client_encoding('UTF8') snowman = _u(b'\xe2\x98\x83') # unicode query with non-ascii data cur.execute(_u(b"SELECT '%s';") % snowman) self.assertEqual(snowman.encode('utf8'), b(cur.fetchone()[0])) self.assertEqual(("SELECT '%s';" % snowman).encode('utf8'), cur.mogrify(_u(b"SELECT '%s';") % snowman).replace(b("E'"), b("'"))) # unicode args cur.execute("SELECT %s;", (snowman,)) self.assertEqual(snowman.encode("utf-8"), b(cur.fetchone()[0])) self.assertEqual(("SELECT '%s';" % snowman).encode('utf8'), cur.mogrify("SELECT %s;", (snowman,)).replace(b("E'"), b("'"))) # unicode query and args cur.execute(_u(b"SELECT %s;"), (snowman,)) self.assertEqual(snowman.encode("utf-8"), b(cur.fetchone()[0])) self.assertEqual(("SELECT '%s';" % snowman).encode('utf8'), cur.mogrify(_u(b"SELECT %s;"), (snowman,)).replace(b("E'"), b("'")))
def test_unicode(self): curs = self.conn.cursor() curs.execute("SHOW server_encoding") server_encoding = curs.fetchone()[0] if server_encoding != "UTF8": return self.skipTest( "Unicode test skipped since server encoding is %s" % server_encoding) data = _u(b"""some data with \t chars to escape into, 'quotes', \xe2\x82\xac euro sign and \\ a backslash too. """) _unichr = chr if six.PY3 else unichr data += _u(b"").join(map(_unichr, [ u for u in range(1,65536) if not 0xD800 <= u <= 0xDFFF ])) # surrogate area self.conn.set_client_encoding('UNICODE') extensions.register_type(extensions.UNICODE, self.conn) curs.execute("SELECT %s::text;", (data,)) res = curs.fetchone()[0] self.assertEqual(res, data) self.assert_(not self.conn.notices)
def test_xid_unicode_unparsed(self): # We don't expect people shooting snowmen as transaction ids, # so if something explodes in an encode error I don't mind. # Let's just check uniconde is accepted as type. cnn = self.connect() cnn.set_client_encoding('utf8') cnn.tpc_begin(_u(b"transaction-id")) cnn.tpc_prepare() cnn.reset() xid = [xid for xid in cnn.tpc_recover() if xid.database == dbname][0] self.assertEqual(None, xid.format_id) self.assertEqual('transaction-id', xid.gtrid) self.assertEqual(None, xid.bqual)
def test_xid_unicode_unparsed(self): # We don't expect people shooting snowmen as transaction ids, # so if something explodes in an encode error I don't mind. # Let's just check uniconde is accepted as type. cnn = self.connect() cnn.set_client_encoding('utf8') cnn.tpc_begin(_u(b"transaction-id")) cnn.tpc_prepare() cnn.reset() xid = [ xid for xid in cnn.tpc_recover() if xid.database == dbname ][0] self.assertEqual(None, xid.format_id) self.assertEqual('transaction-id', xid.gtrid) self.assertEqual(None, xid.bqual)
def _test_create_exception(self, encoding, value): conn = self.conn if encoding is not None: conn.set_client_encoding(encoding) cur = conn.cursor() query = _u(b"SLECT '%s';") % value try: cur.execute(query) except Exception as e: if hasattr(self, 'assertIsInstance'): self.assertIsInstance(e, psycopg2.ProgrammingError) self.assertIn(query, e.args[0]) else: self.assert_(isinstance(e, psycopg2.ProgrammingError)) self.assert_(query in e.args[0]) else: assert False, 'expected exception'
def test_create_exception_other(self): self._test_create_exception('EUC_JP', _u(b'\xe6\x83\x85\xe5\xa0\xb1'))
def test_create_exception_None(self): self._test_create_exception(None, _u(b'\xe2\x98\x83'))
def test_create_exception_utf8(self): self._test_create_exception('utf-8', _u(b'\xe2\x98\x83'))
def testUnicode(self): s = _u(b"Quote'this\\! ''ok?''") self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s, "wrong unicode quoting: " + s)
def testUnicode(self): s = _u(b"Quote'this\\! ''ok?''") self.failUnless( self.execute("SELECT %s AS foo", (s, )) == s, "wrong unicode quoting: " + s)