source: trunk/tests/test_classic_functions.py @ 770

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

Add methods for getting a table as a list or dict

Also added documentation and 100% test coverage.

The get_attnames() method now always returns a read-only ordered dictionary,
even under Python 2.6 or 3.0. So you can sure the columns will be returned
in the right order if you iterate over it, and that you don't accidentally
modify the dictionary (since it is cached).

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 7.5 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
13try:
14    import unittest2 as unittest  # for Python < 2.7
15except ImportError:
16    import unittest
17
18import re
19
20import pg  # the module under test
21
22try:
23    long
24except NameError:  # Python >= 3.0
25    long = int
26
27try:
28    unicode
29except NameError:  # Python >= 3.0
30    unicode = str
31
32
33class TestHasConnect(unittest.TestCase):
34    """Test existence of basic pg module functions."""
35
36    def testhasPgError(self):
37        self.assertTrue(issubclass(pg.Error, Exception))
38
39    def testhasPgWarning(self):
40        self.assertTrue(issubclass(pg.Warning, Exception))
41
42    def testhasPgInterfaceError(self):
43        self.assertTrue(issubclass(pg.InterfaceError, pg.Error))
44
45    def testhasPgDatabaseError(self):
46        self.assertTrue(issubclass(pg.DatabaseError, pg.Error))
47
48    def testhasPgInternalError(self):
49        self.assertTrue(issubclass(pg.InternalError, pg.DatabaseError))
50
51    def testhasPgOperationalError(self):
52        self.assertTrue(issubclass(pg.OperationalError, pg.DatabaseError))
53
54    def testhasPgProgrammingError(self):
55        self.assertTrue(issubclass(pg.ProgrammingError, pg.DatabaseError))
56
57    def testhasPgIntegrityError(self):
58        self.assertTrue(issubclass(pg.IntegrityError, pg.DatabaseError))
59
60    def testhasPgDataError(self):
61        self.assertTrue(issubclass(pg.DataError, pg.DatabaseError))
62
63    def testhasPgNotSupportedError(self):
64        self.assertTrue(issubclass(pg.NotSupportedError, pg.DatabaseError))
65
66    def testhasConnect(self):
67        self.assertTrue(callable(pg.connect))
68
69    def testhasEscapeString(self):
70        self.assertTrue(callable(pg.escape_string))
71
72    def testhasEscapeBytea(self):
73        self.assertTrue(callable(pg.escape_bytea))
74
75    def testhasUnescapeBytea(self):
76        self.assertTrue(callable(pg.unescape_bytea))
77
78    def testDefHost(self):
79        d0 = pg.get_defhost()
80        d1 = 'pgtesthost'
81        pg.set_defhost(d1)
82        self.assertEqual(pg.get_defhost(), d1)
83        pg.set_defhost(d0)
84        self.assertEqual(pg.get_defhost(), d0)
85
86    def testDefPort(self):
87        d0 = pg.get_defport()
88        d1 = 1234
89        pg.set_defport(d1)
90        self.assertEqual(pg.get_defport(), d1)
91        if d0 is None:
92            d0 = -1
93        pg.set_defport(d0)
94        if d0 == -1:
95            d0 = None
96        self.assertEqual(pg.get_defport(), d0)
97
98    def testDefOpt(self):
99        d0 = pg.get_defopt()
100        d1 = '-h pgtesthost -p 1234'
101        pg.set_defopt(d1)
102        self.assertEqual(pg.get_defopt(), d1)
103        pg.set_defopt(d0)
104        self.assertEqual(pg.get_defopt(), d0)
105
106    def testDefBase(self):
107        d0 = pg.get_defbase()
108        d1 = 'pgtestdb'
109        pg.set_defbase(d1)
110        self.assertEqual(pg.get_defbase(), d1)
111        pg.set_defbase(d0)
112        self.assertEqual(pg.get_defbase(), d0)
113
114
115class TestEscapeFunctions(unittest.TestCase):
116    """Test pg escape and unescape functions.
117
118    The libpq interface memorizes some parameters of the last opened
119    connection that influence the result of these functions.
120    Therefore we cannot do rigid tests of these functions here.
121    We leave this for the test module that runs with a database.
122
123    """
124
125    def testEscapeString(self):
126        f = pg.escape_string
127        r = f(b'plain')
128        self.assertIsInstance(r, bytes)
129        self.assertEqual(r, b'plain')
130        r = f(u'plain')
131        self.assertIsInstance(r, unicode)
132        self.assertEqual(r, u'plain')
133        r = f("that's cheese")
134        self.assertIsInstance(r, str)
135        self.assertEqual(r, "that''s cheese")
136
137    def testEscapeBytea(self):
138        f = pg.escape_bytea
139        r = f(b'plain')
140        self.assertIsInstance(r, bytes)
141        self.assertEqual(r, b'plain')
142        r = f(u'plain')
143        self.assertIsInstance(r, unicode)
144        self.assertEqual(r, u'plain')
145        r = f("that's cheese")
146        self.assertIsInstance(r, str)
147        self.assertEqual(r, "that''s cheese")
148
149    def testUnescapeBytea(self):
150        f = pg.unescape_bytea
151        r = f(b'plain')
152        self.assertIsInstance(r, bytes)
153        self.assertEqual(r, b'plain')
154        r = f(u'plain')
155        self.assertIsInstance(r, bytes)
156        self.assertEqual(r, b'plain')
157        r = f(b"das is' k\\303\\244se")
158        self.assertIsInstance(r, bytes)
159        self.assertEqual(r, u"das is' kÀse".encode('utf-8'))
160        r = f(u"das is' k\\303\\244se")
161        self.assertIsInstance(r, bytes)
162        self.assertEqual(r, u"das is' kÀse".encode('utf-8'))
163        r = f(b'O\\000ps\\377!')
164        self.assertEqual(r, b'O\x00ps\xff!')
165        r = f(u'O\\000ps\\377!')
166        self.assertEqual(r, b'O\x00ps\xff!')
167
168
169class TestConfigFunctions(unittest.TestCase):
170    """Test the functions for changing default settings.
171
172    The effect of most of these cannot be tested here, because that
173    needs a database connection.  So we merely test their existence here.
174
175    """
176
177    def testGetDecimalPoint(self):
178        r = pg.get_decimal_point()
179        self.assertIsInstance(r, str)
180        self.assertEqual(r, '.')
181
182    def testSetDecimalPoint(self):
183        point = pg.get_decimal_point()
184        pg.set_decimal_point('*')
185        r = pg.get_decimal_point()
186        pg.set_decimal_point(point)
187        self.assertIsInstance(r, str)
188        self.assertEqual(r, '*')
189        r = pg.get_decimal_point()
190        self.assertIsInstance(r, str)
191        self.assertEqual(r, point)
192
193    def testGetDecimal(self):
194        r = pg.get_decimal()
195        self.assertIs(r, pg.Decimal)
196
197    def testSetDecimal(self):
198        decimal_class = pg.Decimal
199        pg.set_decimal(int)
200        r = pg.get_decimal()
201        pg.set_decimal(decimal_class)
202        self.assertIs(r, int)
203        r = pg.get_decimal()
204        self.assertIs(r, decimal_class)
205
206    def testGetBool(self):
207        r = pg.get_bool()
208        self.assertIsInstance(r, bool)
209        self.assertIs(r, False)
210
211    def testSetBool(self):
212        use_bool = pg.get_bool()
213        pg.set_bool(True)
214        r = pg.get_bool()
215        pg.set_bool(use_bool)
216        self.assertIsInstance(r, bool)
217        self.assertIs(r, True)
218        pg.set_bool(False)
219        r = pg.get_bool()
220        pg.set_bool(use_bool)
221        self.assertIsInstance(r, bool)
222        self.assertIs(r, False)
223        r = pg.get_bool()
224        self.assertIsInstance(r, bool)
225        self.assertIs(r, use_bool)
226
227    def testGetNamedresult(self):
228        r = pg.get_namedresult()
229        self.assertTrue(callable(r))
230        self.assertIs(r, pg._namedresult)
231
232    def testSetNamedresult(self):
233        namedresult = pg.get_namedresult()
234        f = lambda q: q.getresult()
235        pg.set_namedresult(f)
236        r = pg.get_namedresult()
237        pg.set_namedresult(namedresult)
238        self.assertIs(r, f)
239        r = pg.get_namedresult()
240        self.assertIs(r, namedresult)
241
242
243class TestModuleConstants(unittest.TestCase):
244    """Test the existence of the documented module constants."""
245
246    def testVersion(self):
247        v = pg.version
248        self.assertIsInstance(v, str)
249        # make sure the version conforms to PEP440
250        re_version = r"""^
251            (\d[\.\d]*(?<= \d))
252            ((?:[abc]|rc)\d+)?
253            (?:(\.post\d+))?
254            (?:(\.dev\d+))?
255            (?:(\+(?![.])[a-zA-Z0-9\.]*[a-zA-Z0-9]))?
256            $"""
257        match = re.match(re_version, v, re.X)
258        self.assertIsNotNone(match)
259
260
261if __name__ == '__main__':
262    unittest.main()
Note: See TracBrowser for help on using the repository browser.