source: branches/4.x/module/TEST_PyGreSQL_classic_functions.py @ 569

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

Don't break named tuple result function in test

After running the test, set the function back to its default, so that
other tests will not break if you use unittest to run multiple test
modules at once, since in that case they use the same interpreter.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 11.0 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    def testEscapeString(self):
261        f = pg.escape_string
262        self.assertEqual(f('plain'), 'plain')
263        self.assertEqual(f("that's k\xe4se"), "that''s k\xe4se")
264        self.assertEqual(f(r"It's fine to have a \ inside."),
265            r"It''s fine to have a \\ inside.")
266
267    def testEscapeBytea(self):
268        f = pg.escape_bytea
269        self.assertEqual(f('plain'), 'plain')
270        self.assertEqual(f("that's k\xe4se"), "that''s k\\\\344se")
271        self.assertEqual(f('O\x00ps\xff!'), r'O\\000ps\\377!')
272
273    def testUnescapeBytea(self):
274        f = pg.unescape_bytea
275        self.assertEqual(f('plain'), 'plain')
276        self.assertEqual(f("that's k\\344se"), "that's k\xe4se")
277        self.assertEqual(f(r'O\000ps\377!'), 'O\x00ps\xff!')
278
279
280class TestConfigFunctions(unittest.TestCase):
281    """Test the functions for changing default settings.
282
283    The effect of most of these cannot be tested here, because that
284    needs a database connection.  So we merely test their existence here.
285
286    """
287
288    def testGetDecimalPoint(self):
289        r = pg.get_decimal_point()
290        self.assertIsInstance(r, str)
291        self.assertEqual(r, '.')
292
293    def testSetDecimalPoint(self):
294        point = pg.get_decimal_point()
295        pg.set_decimal_point('*')
296        r = pg.get_decimal_point()
297        self.assertIsInstance(r, str)
298        self.assertEqual(r, '*')
299        pg.set_decimal_point(point)
300
301    def testSetDecimal(self):
302        decimal_class = pg.Decimal
303        pg.set_decimal(long)
304        pg.set_decimal(decimal_class)
305
306    def testSetNamedresult(self):
307        pg.set_namedresult(lambda q: q.getresult())
308        pg.set_namedresult(pg._namedresult)
309
310
311class TestModuleConstants(unittest.TestCase):
312    """Test the existence of the documented module constants."""
313
314    def testVersion(self):
315        v = pg.version
316        self.assertIsInstance(v, str)
317        # make sure the version conforms to PEP440
318        re_version = r"""^
319            (\d[\.\d]*(?<= \d))
320            ((?:[abc]|rc)\d+)?
321            (?:(\.post\d+))?
322            (?:(\.dev\d+))?
323            (?:(\+(?![.])[a-zA-Z0-9\.]*[a-zA-Z0-9]))?
324            $"""
325        match = re.match(re_version, v, re.X)
326        self.assertIsNotNone(match)
327
328
329if __name__ == '__main__':
330    unittest.main()
Note: See TracBrowser for help on using the repository browser.