source: trunk/module/tests/test_classic_functions.py @ 680

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

Remove the deprecated tty parameter and attribute

This parameter has been ignored by PostgreSQL since version 7.4.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 13.1 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 re
21
22import pg  # the module under test
23
24try:
25    long
26except NameError:  # Python >= 3.0
27    long = int
28
29try:
30    unicode
31except NameError:  # Python >= 3.0
32    unicode = str
33
34
35class TestAuxiliaryFunctions(unittest.TestCase):
36    """Test the auxiliary functions external to the connection class."""
37
38    def testIsQuoted(self):
39        f = pg._is_quoted
40        self.assertTrue(f('A'))
41        self.assertTrue(f('0'))
42        self.assertTrue(f('#'))
43        self.assertTrue(f('*'))
44        self.assertTrue(f('.'))
45        self.assertTrue(f(' '))
46        self.assertTrue(f('a b'))
47        self.assertTrue(f('a+b'))
48        self.assertTrue(f('a*b'))
49        self.assertTrue(f('a.b'))
50        self.assertTrue(f('0ab'))
51        self.assertTrue(f('aBc'))
52        self.assertTrue(f('ABC'))
53        self.assertTrue(f('"a"'))
54        self.assertTrue(not f('a'))
55        self.assertTrue(not f('a0'))
56        self.assertTrue(not f('_'))
57        self.assertTrue(not f('_a'))
58        self.assertTrue(not f('_0'))
59        self.assertTrue(not f('_a_0_'))
60        self.assertTrue(not f('ab'))
61        self.assertTrue(not f('ab0'))
62        self.assertTrue(not f('abc'))
63        self.assertTrue(not f('abc'))
64        if 'À'.isalpha():
65            self.assertTrue(not f('À'))
66            self.assertTrue(f('Ä'))
67            self.assertTrue(not f('kÀse'))
68            self.assertTrue(f('KÀse'))
69            self.assertTrue(not f('emmentaler_kÀse'))
70            self.assertTrue(f('emmentaler kÀse'))
71            self.assertTrue(f('EmmentalerKÀse'))
72            self.assertTrue(f('Emmentaler KÀse'))
73
74    def testIsUnquoted(self):
75        f = pg._is_unquoted
76        self.assertTrue(f('A'))
77        self.assertTrue(not f('0'))
78        self.assertTrue(not f('#'))
79        self.assertTrue(not f('*'))
80        self.assertTrue(not f('.'))
81        self.assertTrue(not f(' '))
82        self.assertTrue(not f('a b'))
83        self.assertTrue(not f('a+b'))
84        self.assertTrue(not f('a*b'))
85        self.assertTrue(not f('a.b'))
86        self.assertTrue(not f('0ab'))
87        self.assertTrue(f('aBc'))
88        self.assertTrue(f('ABC'))
89        self.assertTrue(not f('"a"'))
90        self.assertTrue(f('a0'))
91        self.assertTrue(f('_'))
92        self.assertTrue(f('_a'))
93        self.assertTrue(f('_0'))
94        self.assertTrue(f('_a_0_'))
95        self.assertTrue(f('ab'))
96        self.assertTrue(f('ab0'))
97        self.assertTrue(f('abc'))
98        if 'À'.isalpha():
99            self.assertTrue(f('À'))
100            self.assertTrue(f('Ä'))
101            self.assertTrue(f('kÀse'))
102            self.assertTrue(f('KÀse'))
103            self.assertTrue(f('emmentaler_kÀse'))
104            self.assertTrue(not f('emmentaler kÀse'))
105            self.assertTrue(f('EmmentalerKÀse'))
106            self.assertTrue(not f('Emmentaler KÀse'))
107
108    def testSplitFirstPart(self):
109        f = pg._split_first_part
110        self.assertEqual(f('a.b'), ['a', 'b'])
111        self.assertEqual(f('a.b.c'), ['a', 'b.c'])
112        self.assertEqual(f('"a.b".c'), ['a.b', 'c'])
113        self.assertEqual(f('a."b.c"'), ['a', '"b.c"'])
114        self.assertEqual(f('A.b.c'), ['a', 'b.c'])
115        self.assertEqual(f('Ab.c'), ['ab', 'c'])
116        self.assertEqual(f('aB.c'), ['ab', 'c'])
117        self.assertEqual(f('AB.c'), ['ab', 'c'])
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('a.b.c.d.e.f.g'), ['a', 'b.c.d.e.f.g'])
123        self.assertEqual(f('"a.b.c.d.e.f".g'), ['a.b.c.d.e.f', 'g'])
124        self.assertEqual(f('a.B.c.D.e.F.g'), ['a', 'B.c.D.e.F.g'])
125        self.assertEqual(f('A.b.C.d.E.f.G'), ['a', 'b.C.d.E.f.G'])
126
127    def testSplitParts(self):
128        f = pg._split_parts
129        self.assertEqual(f('a.b'), ['a', 'b'])
130        self.assertEqual(f('a.b.c'), ['a', 'b', 'c'])
131        self.assertEqual(f('"a.b".c'), ['a.b', 'c'])
132        self.assertEqual(f('a."b.c"'), ['a', 'b.c'])
133        self.assertEqual(f('A.b.c'), ['a', 'b', 'c'])
134        self.assertEqual(f('Ab.c'), ['ab', 'c'])
135        self.assertEqual(f('aB.c'), ['ab', 'c'])
136        self.assertEqual(f('AB.c'), ['ab', 'c'])
137        self.assertEqual(f('A b.c'), ['A b', 'c'])
138        self.assertEqual(f('a B.c'), ['a B', 'c'])
139        self.assertEqual(f('"A".b.c'), ['A', 'b', 'c'])
140        self.assertEqual(f('"A""B".c'), ['A"B', 'c'])
141        self.assertEqual(f('a.b.c.d.e.f.g'),
142            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
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.c.D.e.F.g'),
146            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
147        self.assertEqual(f('A.b.C.d.E.f.G'),
148            ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
149
150    def testJoinParts(self):
151        f = pg._join_parts
152        self.assertEqual(f(('a',)), 'a')
153        self.assertEqual(f(('a', 'b')), 'a.b')
154        self.assertEqual(f(('a', 'b', 'c')), 'a.b.c')
155        self.assertEqual(f(('a', 'b', 'c', 'd', 'e', 'f', 'g')),
156            'a.b.c.d.e.f.g')
157        self.assertEqual(f(('A', 'b')), '"A".b')
158        self.assertEqual(f(('a', 'B')), 'a."B"')
159        self.assertEqual(f(('a b', 'c')), '"a b".c')
160        self.assertEqual(f(('a', 'b c')), 'a."b c"')
161        self.assertEqual(f(('a_b', 'c')), 'a_b.c')
162        self.assertEqual(f(('a', 'b_c')), 'a.b_c')
163        self.assertEqual(f(('0', 'a')), '"0".a')
164        self.assertEqual(f(('0_', 'a')), '"0_".a')
165        self.assertEqual(f(('_0', 'a')), '_0.a')
166        self.assertEqual(f(('_a', 'b')), '_a.b')
167        self.assertEqual(f(('a', 'B', '0', 'c0', 'C0',
168            'd e', 'f_g', 'h.i', 'jklm', 'nopq')),
169            'a."B"."0".c0."C0"."d e".f_g."h.i".jklm.nopq')
170
171    def testOidKey(self):
172        f = pg._oid_key
173        self.assertEqual(f('a'), 'oid(a)')
174        self.assertEqual(f('a.b'), 'oid(a.b)')
175
176
177class TestHasConnect(unittest.TestCase):
178    """Test existence of basic pg module functions."""
179
180    def testhasPgError(self):
181        self.assertTrue(issubclass(pg.Error, Exception))
182
183    def testhasPgWarning(self):
184        self.assertTrue(issubclass(pg.Warning, Exception))
185
186    def testhasPgInterfaceError(self):
187        self.assertTrue(issubclass(pg.InterfaceError, pg.Error))
188
189    def testhasPgDatabaseError(self):
190        self.assertTrue(issubclass(pg.DatabaseError, pg.Error))
191
192    def testhasPgInternalError(self):
193        self.assertTrue(issubclass(pg.InternalError, pg.DatabaseError))
194
195    def testhasPgOperationalError(self):
196        self.assertTrue(issubclass(pg.OperationalError, pg.DatabaseError))
197
198    def testhasPgProgrammingError(self):
199        self.assertTrue(issubclass(pg.ProgrammingError, pg.DatabaseError))
200
201    def testhasPgIntegrityError(self):
202        self.assertTrue(issubclass(pg.IntegrityError, pg.DatabaseError))
203
204    def testhasPgDataError(self):
205        self.assertTrue(issubclass(pg.DataError, pg.DatabaseError))
206
207    def testhasPgNotSupportedError(self):
208        self.assertTrue(issubclass(pg.NotSupportedError, pg.DatabaseError))
209
210    def testhasConnect(self):
211        self.assertTrue(callable(pg.connect))
212
213    def testhasEscapeString(self):
214        self.assertTrue(callable(pg.escape_string))
215
216    def testhasEscapeBytea(self):
217        self.assertTrue(callable(pg.escape_bytea))
218
219    def testhasUnescapeBytea(self):
220        self.assertTrue(callable(pg.unescape_bytea))
221
222    def testDefHost(self):
223        d0 = pg.get_defhost()
224        d1 = 'pgtesthost'
225        pg.set_defhost(d1)
226        self.assertEqual(pg.get_defhost(), d1)
227        pg.set_defhost(d0)
228        self.assertEqual(pg.get_defhost(), d0)
229
230    def testDefPort(self):
231        d0 = pg.get_defport()
232        d1 = 1234
233        pg.set_defport(d1)
234        self.assertEqual(pg.get_defport(), d1)
235        if d0 is None:
236            d0 = -1
237        pg.set_defport(d0)
238        if d0 == -1:
239            d0 = None
240        self.assertEqual(pg.get_defport(), d0)
241
242    def testDefOpt(self):
243        d0 = pg.get_defopt()
244        d1 = '-h pgtesthost -p 1234'
245        pg.set_defopt(d1)
246        self.assertEqual(pg.get_defopt(), d1)
247        pg.set_defopt(d0)
248        self.assertEqual(pg.get_defopt(), d0)
249
250    def testDefBase(self):
251        d0 = pg.get_defbase()
252        d1 = 'pgtestdb'
253        pg.set_defbase(d1)
254        self.assertEqual(pg.get_defbase(), d1)
255        pg.set_defbase(d0)
256        self.assertEqual(pg.get_defbase(), d0)
257
258
259class TestEscapeFunctions(unittest.TestCase):
260    """Test pg escape and unescape functions.
261
262    The libpq interface memorizes some parameters of the last opened
263    connection that influence the result of these functions.
264    Therefore we cannot do rigid tests of these functions here.
265    We leave this for the test module that runs with a database.
266
267    """
268
269    def testEscapeString(self):
270        f = pg.escape_string
271        r = f(b'plain')
272        self.assertIsInstance(r, bytes)
273        self.assertEqual(r, b'plain')
274        r = f(u'plain')
275        self.assertIsInstance(r, unicode)
276        self.assertEqual(r, u'plain')
277        r = f("that's cheese")
278        self.assertIsInstance(r, str)
279        self.assertEqual(r, "that''s cheese")
280
281    def testEscapeBytea(self):
282        f = pg.escape_bytea
283        r = f(b'plain')
284        self.assertIsInstance(r, bytes)
285        self.assertEqual(r, b'plain')
286        r = f(u'plain')
287        self.assertIsInstance(r, unicode)
288        self.assertEqual(r, u'plain')
289        r = f("that's cheese")
290        self.assertIsInstance(r, str)
291        self.assertEqual(r, "that''s cheese")
292
293    def testUnescapeBytea(self):
294        f = pg.unescape_bytea
295        r = f(b'plain')
296        self.assertIsInstance(r, bytes)
297        self.assertEqual(r, b'plain')
298        r = f(u'plain')
299        self.assertIsInstance(r, bytes)
300        self.assertEqual(r, b'plain')
301        r = f(b"das is' k\\303\\244se")
302        self.assertIsInstance(r, bytes)
303        self.assertEqual(r, u"das is' kÀse".encode('utf-8'))
304        r = f(u"das is' k\\303\\244se")
305        self.assertIsInstance(r, bytes)
306        self.assertEqual(r, u"das is' kÀse".encode('utf-8'))
307        r = f(b'O\\000ps\\377!')
308        self.assertEqual(r, b'O\x00ps\xff!')
309        r = f(u'O\\000ps\\377!')
310        self.assertEqual(r, b'O\x00ps\xff!')
311
312
313class TestConfigFunctions(unittest.TestCase):
314    """Test the functions for changing default settings.
315
316    The effect of most of these cannot be tested here, because that
317    needs a database connection.  So we merely test their existence here.
318
319    """
320
321    def testGetDecimalPoint(self):
322        r = pg.get_decimal_point()
323        self.assertIsInstance(r, str)
324        self.assertEqual(r, '.')
325
326    def testSetDecimalPoint(self):
327        point = pg.get_decimal_point()
328        pg.set_decimal_point('*')
329        r = pg.get_decimal_point()
330        pg.set_decimal_point(point)
331        self.assertIsInstance(r, str)
332        self.assertEqual(r, '*')
333        r = pg.get_decimal_point()
334        self.assertIsInstance(r, str)
335        self.assertEqual(r, point)
336
337    def testGetDecimal(self):
338        r = pg.get_decimal()
339        self.assertIs(r, pg.Decimal)
340
341    def testSetDecimal(self):
342        decimal_class = pg.Decimal
343        pg.set_decimal(int)
344        r = pg.get_decimal()
345        pg.set_decimal(decimal_class)
346        self.assertIs(r, int)
347        r = pg.get_decimal()
348        self.assertIs(r, decimal_class)
349
350    def testGetBool(self):
351        r = pg.get_bool()
352        self.assertIsInstance(r, bool)
353        self.assertIs(r, False)
354
355    def testSetBool(self):
356        use_bool = pg.get_bool()
357        pg.set_bool(True)
358        r = pg.get_bool()
359        pg.set_bool(use_bool)
360        self.assertIsInstance(r, bool)
361        self.assertIs(r, True)
362        pg.set_bool(False)
363        r = pg.get_bool()
364        pg.set_bool(use_bool)
365        self.assertIsInstance(r, bool)
366        self.assertIs(r, False)
367        r = pg.get_bool()
368        self.assertIsInstance(r, bool)
369        self.assertIs(r, use_bool)
370
371    def testGetNamedresult(self):
372        r = pg.get_namedresult()
373        self.assertTrue(callable(r))
374        self.assertIs(r, pg._namedresult)
375
376    def testSetNamedresult(self):
377        namedresult = pg.get_namedresult()
378        f = lambda q: q.getresult()
379        pg.set_namedresult(f)
380        r = pg.get_namedresult()
381        pg.set_namedresult(namedresult)
382        self.assertIs(r, f)
383        r = pg.get_namedresult()
384        self.assertIs(r, namedresult)
385
386
387class TestModuleConstants(unittest.TestCase):
388    """Test the existence of the documented module constants."""
389
390    def testVersion(self):
391        v = pg.version
392        self.assertIsInstance(v, str)
393        # make sure the version conforms to PEP440
394        re_version = r"""^
395            (\d[\.\d]*(?<= \d))
396            ((?:[abc]|rc)\d+)?
397            (?:(\.post\d+))?
398            (?:(\.dev\d+))?
399            (?:(\+(?![.])[a-zA-Z0-9\.]*[a-zA-Z0-9]))?
400            $"""
401        match = re.match(re_version, v, re.X)
402        self.assertIsNotNone(match)
403
404
405if __name__ == '__main__':
406    unittest.main()
Note: See TracBrowser for help on using the repository browser.