source: branches/4.x/module/tests/test_classic_functions.py @ 646

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

Backport some more tests from trunk to 4.x

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