File: unicode_tests.py

package info (click to toggle)
python-pgsql 2.4.0-5sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 1,291
  • sloc: ansic: 5,913; python: 4,334; sh: 73; makefile: 71
file content (187 lines) | stat: -rw-r--r-- 7,056 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
#-*- coding: ISO-8859-1 -*-
#ident "@(#) $Id: unicode_tests.py,v 1.3 2003/07/12 17:19:06 ghaering Exp $"
#-----------------------------------------------------------------------+
# Name:		unicode_tests.py					|
#									|
# Synopsys:								|
#									|
# Description:	Tests for the Unicode support in pyPgSQL.		|
#=======================================================================|
# Copyright 2002 by Gerhard Hring.					|
# All rights reserved.							|
#									|
# Permission to use, copy, modify, and distribute this software and its	|
# documentation for any purpose and without fee is hereby granted, pro-	|
# vided that the above copyright notice appear in all copies and that	|
# both that copyright notice and this permission notice appear in sup-	|
# porting documentation, and that the copyright owner's name not be	|
# used in advertising or publicity pertaining to distribution of the	|
# software without specific, written prior permission.			|
#									|
# THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,	|
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN	|
# NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR	|
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS	|
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE	|
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE	|
# USE OR PERFORMANCE OF THIS SOFTWARE.					|
#=======================================================================|
# Revision History:							|
#									|
# Date      Ini Description						|
# --------- --- ------------------------------------------------------- |
# 27OCT2002 gh  Corrected coypright statement.				|
# 27OCT2002 gh  Initial release by Gerhard Hring.			|
#-----------------------------------------------------------------------+
import types
import unittest

from pyPgSQL import PgSQL

# Note: This test suite requires
# - that a database 'pypgsql' exists with encoding set to UNICODE:
#   $ createdb pypgsql -E UNICODE
# - that this database has support for PL/PgSQL:
#   $ createlang plpgsql pypgsql

dbname = "pypgsql"

class UnicodeDatabaseTestCase(unittest.TestCase):
    def testInsertWithUtf8(self):
	conn = PgSQL.connect(database=dbname, client_encoding=("utf-8",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to unicode")

	l1_text = unicode("sterreich", "latin1")
	
	cursor = conn.cursor()
	cursor.execute("create table test (name varchar(20))")
	cursor.execute(u"insert into test(name) values (%s)", l1_text)
	cursor.execute("select name from test")
	name = cursor.fetchone().name
	self.failUnless(name == l1_text, "latin1 text from insert and select don't match")

    def testInsertWithLatin1(self):
	conn = PgSQL.connect(database=dbname, client_encoding=("latin1",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to latin1")

	l1_text = unicode("sterreich", "latin1")
	
	cursor = conn.cursor()
	cursor.execute("create table test (name varchar(20))")
	cursor.execute(u"insert into test(name) values (%s)", (l1_text,))
	cursor.execute("select name from test")
	name = cursor.fetchone().name
	self.failUnless(name == l1_text, "latin1 text from insert and select don't match")

    def testCallProcUtf8(self):
	text1 = unicode("sterreich-", "latin1")
	text2 = unicode("Ungarn", "ascii")
	
	conn = PgSQL.connect(database=dbname, client_encoding=("utf-8",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to unicode")
	cursor.execute(u"""
	CREATE FUNCTION concat_text (TEXT, TEXT) RETURNS TEXT AS '
	BEGIN
	    RETURN $1 || $2;
	END;
	'LANGUAGE 'plpgsql';
	""")
	cursor.callproc(u"concat_text", text1, text2)
	result = cursor.fetchone()[0]
	self.failUnless(result == text1 + text2, "procedure didn't return the two strings concatenated")

    def testCallProcLatin1(self):
	text1 = unicode("sterreich-", "latin1")
	text2 = unicode("Ungarn", "ascii")
	
	conn = PgSQL.connect(database=dbname, client_encoding=("latin1",))
	cursor = conn.cursor()
	cursor.execute("set client_encoding to latin1")
	cursor.execute(u"""
	CREATE FUNCTION concat_text (TEXT, TEXT) RETURNS TEXT AS '
	BEGIN
	    RETURN $1 || $2;
	END;
	'LANGUAGE 'plpgsql';
	""")
	cursor.callproc(u"concat_text", text1, text2)
	result = cursor.fetchone()[0]
	self.failUnless(unicode(result, "latin1") == text1 + text2, "procedure didn't return the two strings concatenated")


    def testCallProcLatin1UnicodeResults(self):
	text1 = unicode("sterreich-", "latin1")
	text2 = unicode("Ungarn", "ascii")
	
	conn = PgSQL.connect(database=dbname, client_encoding=("latin1",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to latin1")
	cursor.execute(u"""
	CREATE FUNCTION concat_text (TEXT, TEXT) RETURNS TEXT AS '
	BEGIN
	    RETURN $1 || $2;
	END;
	'LANGUAGE 'plpgsql';
	""")
	cursor.callproc(u"concat_text", text1, text2)
	result = cursor.fetchone()[0]
	self.failUnless(result == text1 + text2, "procedure didn't return the two strings concatenated")


    def __testUnicodeConversion(self, item):
	text = unicode("sterreich", "latin1")

	conn = PgSQL.connect(database=dbname, client_encoding=("utf-8",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to unicode")
	cursor.execute("create table test (name varchar(20))")
	cursor.execute("insert into test (name) values (%s)", text)
	if type(item) is types.DictType:
	    cursor.execute("select name from test where name=%(name)s", item)
	else:
	    cursor.execute("select name from test where name=%s", item)
	result = cursor.fetchall()
	self.failUnless(len(result) == 1, "couldn't find the inserted string")

    def testTupleUnicodeConversion(self):
	self.__testUnicodeConversion((unicode("sterreich", "latin1"),))
	
    def testListUnicodeConversion(self):
	self.__testUnicodeConversion([unicode("sterreich", "latin1")])

    def testDictUnicodeConversion(self):
	self.__testUnicodeConversion({"name": unicode("sterreich", "latin1")})

    def testPgResultSetUnicodeConversion(self):
	text = unicode("sterreich", "latin1")

	conn = PgSQL.connect(database=dbname, client_encoding=("utf-8",), unicode_results=1)
	cursor = conn.cursor()
	cursor.execute("set client_encoding to unicode")
	cursor.execute("create table test (name varchar(20))")
	cursor.execute("insert into test (name) values (%s)", text)
	cursor.execute("select name from test")
	result = cursor.fetchone()

	cursor.execute("select name from test where name=%s", result[0])
	result2 = cursor.fetchall()
	self.failUnless(len(result2) == 1)

	cursor.execute("select name from test where name=%(name)s", result)
	result2 = cursor.fetchall()
	self.failUnless(len(result2) == 1, "couldn't find the inserted string")

def suite():
    unicode_tests = unittest.makeSuite(UnicodeDatabaseTestCase, "test")
    return unicode_tests

def main():
    runner = unittest.TextTestRunner()
    runner.run(suite())
  
if __name__ == "__main__":
    main()