source: trunk/module/TEST_PyGreSQL_classic_functions.py @ 553

Last change on this file since 553 was 553, checked in by cito, 4 years ago

Require at least Python 2.6 for the trunk (5.x)

Support for even older Python versions is maintained in the 4.x branch.
The goal for 5.x is to be a single-source code for both Python 2 and 3,
and this is only possible by dropping support for Python 2.5 and older.
For instance, the new except .. as syntax works only since Python 2.6.
Otherwise we would need to use 2to3 and things would be very ugly.
Note that Python 2.6 is now 7 years old. We may want to drop Python 2.6
as well at some point if it turns out to be a burden.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1#! /usr/bin/python
2# -*- coding: utf-8 -*-
3
4"""Test the classic PyGreSQL interface.
5
6Sub-tests for the module functions and constants.
7
8Contributed by Christoph Zwerschke.
9
10These tests do not need a database to test against.
11
12"""
13
14
15try:
16    import unittest2 as unittest  # for Python < 2.7
17except ImportError:
18    import unittest
19
20import pg  # the module under test
21
22
23class TestAuxiliaryFunctions(unittest.TestCase):
24    """Test the auxiliary functions external to the connection class."""
25
26    def testIsQuoted(self):
27        f = pg._is_quoted
28        self.assertTrue(f('A'))
29        self.assertTrue(f('0'))
30        self.assertTrue(f('#'))
31        self.assertTrue(f('*'))
32        self.assertTrue(f('.'))
33        self.assertTrue(f(' '))
34        self.assertTrue(f('a b'))
35        self.assertTrue(f('a+b'))
36        self.assertTrue(f('a*b'))
37        self.assertTrue(f('a.b'))
38        self.assertTrue(f('0ab'))
39        self.assertTrue(f('aBc'))
40        self.assertTrue(f('ABC'))
41        self.assertTrue(f('"a"'))
42        self.assertTrue(not f('a'))
43        self.assertTrue(not f('a0'))
44        self.assertTrue(not f('_'))
45        self.assertTrue(not f('_a'))
46        self.assertTrue(not f('_0'))
47        self.assertTrue(not f('_a_0_'))
48        self.assertTrue(not f('ab'))
49        self.assertTrue(not f('ab0'))
50        self.assertTrue(not f('abc'))
51        self.assertTrue(not f('abc'))
52        if 'À'.isalpha():
53            self.assertTrue(not f('À'))
54            self.assertTrue(f('Ä'))
55            self.assertTrue(not f('kÀse'))
56            self.assertTrue(f('KÀse'))
57            self.assertTrue(not f('emmentaler_kÀse'))
58            self.assertTrue(f('emmentaler kÀse'))
59            self.assertTrue(f('EmmentalerKÀse'))
60            self.assertTrue(f('Emmentaler KÀse'))
61
62    def testIsUnquoted(self):
63        f = pg._is_unquoted
64        self.assertTrue(f('A'))
65        self.assertTrue(not f('0'))
66        self.assertTrue(not f('#'))
67        self.assertTrue(not f('*'))
68        self.assertTrue(not f('.'))
69        self.assertTrue(not f(' '))
70        self.assertTrue(not f('a b'))
71        self.assertTrue(not f('a+b'))
72        self.assertTrue(not f('a*b'))
73        self.assertTrue(not f('a.b'))
74        self.assertTrue(not f('0ab'))
75        self.assertTrue(f('aBc'))
76        self.assertTrue(f('ABC'))
77        self.assertTrue(not f('"a"'))
78        self.assertTrue(f('a0'))
79        self.assertTrue(f('_'))
80        self.assertTrue(f('_a'))
81        self.assertTrue(f('_0'))
82        self.assertTrue(f('_a_0_'))
83        self.assertTrue(f('ab'))
84        self.assertTrue(f('ab0'))
85        self.assertTrue(f('abc'))
86        if 'À'.isalpha():
87            self.assertTrue(f('À'))
88            self.assertTrue(f('Ä'))
89            self.assertTrue(f('kÀse'))
90            self.assertTrue(f('KÀse'))
91            self.assertTrue(f('emmentaler_kÀse'))
92            self.assertTrue(not f('emmentaler kÀse'))
93            self.assertTrue(f('EmmentalerKÀse'))
94            self.assertTrue(not f('Emmentaler KÀse'))
95
96    def testSplitFirstPart(self):
97        f = pg._split_first_part
98        self.assertEqual(f('a.b'), ['a', 'b'])
99        self.assertEqual(f('a.b.c'), ['a', 'b.c'])
100        self.assertEqual(f('"a.b".c'), ['a.b', 'c'])
101        self.assertEqual(f('a."b.c"'), ['a', '"b.c"'])
102        self.assertEqual(f('A.b.c'), ['a', 'b.c'])
103        self.assertEqual(f('Ab.c'), ['ab', 'c'])
104        self.assertEqual(f('aB.c'), ['ab', 'c'])
105        self.assertEqual(f('AB.c'), ['ab', 'c'])
106        self.assertEqual(f('A b.c'), ['A b', 'c'])
107        self.assertEqual(f('a B.c'), ['a B', 'c'])
108        self.assertEqual(f('"A".b.c'), ['A', 'b.c'])
109        self.assertEqual(f('"A""B".c'), ['A"B', 'c'])
110        self.assertEqual(f('a.b.c.d.e.f.g'), ['a', 'b.c.d.e.f.g'])
111        self.assertEqual(f('"a.b.c.d.e.f".g'), ['a.b.c.d.e.f', 'g'])
112        self.assertEqual(f('a.B.c.D.e.F.g'), ['a', 'B.c.D.e.F.g'])
113        self.assertEqual(f('A.b.C.d.E.f.G'), ['a', 'b.C.d.E.f.G'])
114
115    def testSplitParts(self):
116        f = pg._split_parts
117        self.assertEqual(f('a.b'), ['a', 'b'])
118        self.assertEqual(f('a.b.c'), ['a', 'b', 'c'])
119        self.assertEqual(f('"a.b".c'), ['a.b', 'c'])
120        self.assertEqual(f('a."b.c"'), ['a', 'b.c'])
121        self.assertEqual(f('A.b.c'), ['a', 'b', 'c'])
122        self.assertEqual(f('Ab.c'), ['ab', 'c'])
123        self.assertEqual(f('aB.c'), ['ab', 'c'])
124        self.assertEqual(f('AB.c'), ['ab', 'c'])
125        self.assertEqual(f('A b.c'), ['A b', 'c'])
126        self.assertEqual(f('a B.c'), ['a B', 'c'])
127        self.assertEqual(f('"A".b.c'), ['A', 'b', 'c'])
128        self.assertEqual(f('"A""B".c'), ['A"B', 'c'])
129        self.assertEqual(f('a.b.c.d.e.f.g'),
130            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
131        self.assertEqual(f('"a.b.c.d.e.f".g'),
132            ['a.b.c.d.e.f', 'g'])
133        self.assertEqual(f('a.B.c.D.e.F.g'),
134            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
135        self.assertEqual(f('A.b.C.d.E.f.G'),
136            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
137
138    def testJoinParts(self):
139        f = pg._join_parts
140        self.assertEqual(f(('a',)), 'a')
141        self.assertEqual(f(('a', 'b')), 'a.b')
142        self.assertEqual(f(('a', 'b', 'c')), 'a.b.c')
143        self.assertEqual(f(('a', 'b', 'c', 'd', 'e', 'f', 'g')),
144            'a.b.c.d.e.f.g')
145        self.assertEqual(f(('A', 'b')), '"A".b')
146        self.assertEqual(f(('a', 'B')), 'a."B"')
147        self.assertEqual(f(('a b', 'c')), '"a b".c')
148        self.assertEqual(f(('a', 'b c')), 'a."b c"')
149        self.assertEqual(f(('a_b', 'c')), 'a_b.c')
150        self.assertEqual(f(('a', 'b_c')), 'a.b_c')
151        self.assertEqual(f(('0', 'a')), '"0".a')
152        self.assertEqual(f(('0_', 'a')), '"0_".a')
153        self.assertEqual(f(('_0', 'a')), '_0.a')
154        self.assertEqual(f(('_a', 'b')), '_a.b')
155        self.assertEqual(f(('a', 'B', '0', 'c0', 'C0',
156            'd e', 'f_g', 'h.i', 'jklm', 'nopq')),
157            'a."B"."0".c0."C0"."d e".f_g."h.i".jklm.nopq')
158
159    def testOidKey(self):
160        f = pg._oid_key
161        self.assertEqual(f('a'), 'oid(a)')
162        self.assertEqual(f('a.b'), 'oid(a.b)')
163
164
165class TestHasConnect(unittest.TestCase):
166    """Test existence of basic pg module functions."""
167
168    def testhasPgError(self):
169        self.assertTrue(issubclass(pg.Error, Exception))
170
171    def testhasPgWarning(self):
172        self.assertTrue(issubclass(pg.Warning, Exception))
173
174    def testhasPgInterfaceError(self):
175        self.assertTrue(issubclass(pg.InterfaceError, pg.Error))
176
177    def testhasPgDatabaseError(self):
178        self.assertTrue(issubclass(pg.DatabaseError, pg.Error))
179
180    def testhasPgInternalError(self):
181        self.assertTrue(issubclass(pg.InternalError, pg.DatabaseError))
182
183    def testhasPgOperationalError(self):
184        self.assertTrue(issubclass(pg.OperationalError, pg.DatabaseError))
185
186    def testhasPgProgrammingError(self):
187        self.assertTrue(issubclass(pg.ProgrammingError, pg.DatabaseError))
188
189    def testhasPgIntegrityError(self):
190        self.assertTrue(issubclass(pg.IntegrityError, pg.DatabaseError))
191
192    def testhasPgDataError(self):
193        self.assertTrue(issubclass(pg.DataError, pg.DatabaseError))
194
195    def testhasPgNotSupportedError(self):
196        self.assertTrue(issubclass(pg.NotSupportedError, pg.DatabaseError))
197
198    def testhasConnect(self):
199        self.assertTrue(callable(pg.connect))
200
201    def testhasEscapeString(self):
202        self.assertTrue(callable(pg.escape_string))
203
204    def testhasEscapeBytea(self):
205        self.assertTrue(callable(pg.escape_bytea))
206
207    def testhasUnescapeBytea(self):
208        self.assertTrue(callable(pg.unescape_bytea))
209
210    def testDefHost(self):
211        d0 = pg.get_defhost()
212        d1 = 'pgtesthost'
213        pg.set_defhost(d1)
214        self.assertEqual(pg.get_defhost(), d1)
215        pg.set_defhost(d0)
216        self.assertEqual(pg.get_defhost(), d0)
217
218    def testDefPort(self):
219        d0 = pg.get_defport()
220        d1 = 1234
221        pg.set_defport(d1)
222        self.assertEqual(pg.get_defport(), d1)
223        if d0 is None:
224            d0 = -1
225        pg.set_defport(d0)
226        if d0 == -1:
227            d0 = None
228        self.assertEqual(pg.get_defport(), d0)
229
230    def testDefOpt(self):
231        d0 = pg.get_defopt()
232        d1 = '-h pgtesthost -p 1234'
233        pg.set_defopt(d1)
234        self.assertEqual(pg.get_defopt(), d1)
235        pg.set_defopt(d0)
236        self.assertEqual(pg.get_defopt(), d0)
237
238    def testDefTty(self):
239        d0 = pg.get_deftty()
240        d1 = 'pgtesttty'
241        pg.set_deftty(d1)
242        self.assertEqual(pg.get_deftty(), d1)
243        pg.set_deftty(d0)
244        self.assertEqual(pg.get_deftty(), d0)
245
246    def testDefBase(self):
247        d0 = pg.get_defbase()
248        d1 = 'pgtestdb'
249        pg.set_defbase(d1)
250        self.assertEqual(pg.get_defbase(), d1)
251        pg.set_defbase(d0)
252        self.assertEqual(pg.get_defbase(), d0)
253
254
255class TestEscapeFunctions(unittest.TestCase):
256    """"Test pg escape and unescape functions."""
257
258    def testEscapeString(self):
259        f = pg.escape_string
260        self.assertEqual(f('plain'), 'plain')
261        self.assertEqual(f("that's k\xe4se"), "that''s k\xe4se")
262        self.assertEqual(f(r"It's fine to have a \ inside."),
263            r"It''s fine to have a \\ inside.")
264
265    def testEscapeBytea(self):
266        f = pg.escape_bytea
267        self.assertEqual(f('plain'), 'plain')
268        self.assertEqual(f("that's k\xe4se"), "that''s k\\\\344se")
269        self.assertEqual(f('O\x00ps\xff!'), r'O\\000ps\\377!')
270
271    def testUnescapeBytea(self):
272        f = pg.unescape_bytea
273        self.assertEqual(f('plain'), 'plain')
274        self.assertEqual(f("that's k\\344se"), "that's k\xe4se")
275        self.assertEqual(f(r'O\000ps\377!'), 'O\x00ps\xff!')
276
277
278class TestConfigFunctions(unittest.TestCase):
279    """Test the functions for changing default settings.
280
281    The effect of most of these cannot be tested here, because that
282    needs a database connection.  So we merely test their existence here.
283
284    """
285
286    def testGetDecimalPoint(self):
287        r = pg.get_decimal_point()
288        self.assertIsInstance(r, str)
289        self.assertEqual(r, '.')
290
291    def testSetDecimalPoint(self):
292        point = pg.get_decimal_point()
293        pg.set_decimal_point('*')
294        r = pg.get_decimal_point()
295        self.assertIsInstance(r, str)
296        self.assertEqual(r, '*')
297        pg.set_decimal_point(point)
298
299    def testSetDecimal(self):
300        decimal_class = pg.Decimal
301        pg.set_decimal(long)
302        pg.set_decimal(decimal_class)
303
304    def testSetNamedresult(self):
305        pg.set_namedresult(tuple)
306
307
308class TestModuleConstants(unittest.TestCase):
309    """Test the existence of the documented module constants."""
310
311    def testVersion(self):
312        v = pg.version
313        self.assertIsInstance(v, str)
314        v = v.split('.')
315        self.assertTrue(2 <= len(v) <= 3)
316        for w in v:
317            self.assertTrue(1 <= len(w) <= 2)
318            self.assertTrue(w.isdigit())
319
320
321if __name__ == '__main__':
322    unittest.main()
Note: See TracBrowser for help on using the repository browser.