From 01565fa6c56fbeb6d4bd4b948ba06bb000f5e8a5 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Dec 2010 13:47:29 +0100 Subject: [PATCH] Added tests to verify a couple of incomplete encodings. --- tests/test_quote.py | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/test_quote.py b/tests/test_quote.py index 83662c72..1e86803c 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -1,9 +1,10 @@ #!/usr/bin/env python +import sys from testutils import unittest +from testconfig import dsn import psycopg2 import psycopg2.extensions -from testconfig import dsn class QuotingTestCase(unittest.TestCase): r"""Checks the correct quoting of strings and binary objects. @@ -78,6 +79,55 @@ class QuotingTestCase(unittest.TestCase): self.assertEqual(res, data) self.assert_(not self.conn.notices) + def test_latin1(self): + self.conn.set_client_encoding('LATIN1') + curs = self.conn.cursor() + if sys.version_info[0] < 3: + data = ''.join(map(chr, range(32, 127) + range(160, 256))) + else: + data = bytes(range(32, 127) + range(160, 256)).decode('latin1') + + # as string + curs.execute("SELECT %s::text;", (data,)) + res = curs.fetchone()[0] + self.assertEqual(res, data) + self.assert_(not self.conn.notices) + + # as unicode + if sys.version_info[0] < 3: + psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, self.conn) + data = data.decode('latin1') + + curs.execute("SELECT %s::text;", (data,)) + res = curs.fetchone()[0] + self.assertEqual(res, data) + self.assert_(not self.conn.notices) + + def test_koi8(self): + self.conn.set_client_encoding('KOI8') + curs = self.conn.cursor() + if sys.version_info[0] < 3: + data = ''.join(map(chr, range(32, 127) + range(128, 256))) + else: + data = bytes(range(32, 127) + range(128, 256)).decode('koi8_r') + + # as string + curs.execute("SELECT %s::text;", (data,)) + res = curs.fetchone()[0] + self.assertEqual(res, data) + self.assert_(not self.conn.notices) + + # as unicode + if sys.version_info[0] < 3: + psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, self.conn) + data = data.decode('koi8_r') + + curs.execute("SELECT %s::text;", (data,)) + res = curs.fetchone()[0] + self.assertEqual(res, data) + self.assert_(not self.conn.notices) + + def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__)