Changeset 542 for branches/4.x


Ignore:
Timestamp:
Nov 18, 2015, 12:56:24 PM (4 years ago)
Author:
cito
Message:

Improve type checks in the tests for classic pg

Location:
branches/4.x/module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.x/module/TEST_PyGreSQL_classic_connection.py

    r541 r542  
    107107    def testAttributePort(self):
    108108        def_port = 5432
     109        self.assertIsInstance(self.connection.port, int)
    109110        self.assertEqual(self.connection.port, def_port)
    110111
    111112    def testAttributeProtocolVersion(self):
    112113        protocol_version = self.connection.protocol_version
    113         self.assertTrue(isinstance(protocol_version, int))
     114        self.assertIsInstance(protocol_version, int)
    114115        self.assertTrue(2 <= protocol_version < 4)
    115116
    116117    def testAttributeServerVersion(self):
    117118        server_version = self.connection.server_version
    118         self.assertTrue(isinstance(server_version, int))
     119        self.assertIsInstance(server_version, int)
    119120        self.assertTrue(70400 <= server_version < 100000)
    120121
    121122    def testAttributeStatus(self):
    122123        status_ok = 1
     124        self.assertIsInstance(self.connection.status, int)
    123125        self.assertEqual(self.connection.status, status_ok)
    124126
    125127    def testAttributeTty(self):
    126128        def_tty = ''
     129        self.assertIsInstance(self.connection.tty, str)
    127130        self.assertEqual(self.connection.tty, def_tty)
    128131
     
    131134        user = self.connection.user
    132135        self.assertTrue(user)
     136        self.assertIsInstance(user, str)
    133137        self.assertNotEqual(user, no_user)
    134138
     
    192196        result = [(0,)]
    193197        r = self.c.query(q).getresult()
    194         self.assertEqual(r, result)
     198        self.assertIsInstance(r, list)
     199        v = r[0]
     200        self.assertIsInstance(v, tuple)
     201        self.assertIsInstance(v[0], int)
     202        self.assertEqual(r, result)
     203
     204    def testGetresultLong(self):
     205        q = "select 1234567890123456790"
     206        result = 1234567890123456790L
     207        v = self.c.query(q).getresult()[0][0]
     208        self.assertIsInstance(v, long)
     209        self.assertEqual(v, result)
     210
     211    def testGetresultString(self):
     212        result = 'Hello, world!'
     213        q = "select '%s'" % result
     214        v = self.c.query(q).getresult()[0][0]
     215        self.assertIsInstance(v, str)
     216        self.assertEqual(v, result)
    195217
    196218    def testDictresult(self):
     
    198220        result = [{'alias0': 0}]
    199221        r = self.c.query(q).dictresult()
    200         self.assertEqual(r, result)
     222        self.assertIsInstance(r, list)
     223        v = r[0]
     224        self.assertIsInstance(v, dict)
     225        self.assertIsInstance(v['alias0'], int)
     226        self.assertEqual(r, result)
     227
     228    def testDictresultLong(self):
     229        q = "select 1234567890123456790 as longjohnsilver"
     230        result = 1234567890123456790L
     231        v = self.c.query(q).dictresult()[0]['longjohnsilver']
     232        self.assertIsInstance(v, long)
     233        self.assertEqual(v, result)
     234
     235    def testDictresultString(self):
     236        result = 'Hello, world!'
     237        q = "select '%s' as greeting" % result
     238        v = self.c.query(q).dictresult()[0]['greeting']
     239        self.assertIsInstance(v, str)
     240        self.assertEqual(v, result)
    201241
    202242    def testNamedresult(self):
     
    307347        q = "select 0 as z, 0 as a, 0 as x, 0 as y"
    308348        r = self.c.query(q).fieldname(2)
    309         result = "x"
    310         self.assertEqual(r, result)
     349        self.assertEqual(r, 'x')
     350        r = self.c.query(q).fieldname(3)
     351        self.assertEqual(r, 'y')
    311352
    312353    def testFieldnum(self):
     354        q = "select 1 as x"
     355        self.assertRaises(ValueError, self.c.query(q).fieldnum, 'y')
     356        q = "select 1 as x"
     357        r = self.c.query(q).fieldnum('x')
     358        self.assertIsInstance(r, int)
     359        self.assertEqual(r, 0)
    313360        q = "select 0 as z, 0 as a, 0 as x, 0 as y"
    314         r = self.c.query(q).fieldnum("x")
    315         result = 2
    316         self.assertEqual(r, result)
     361        r = self.c.query(q).fieldnum('x')
     362        self.assertIsInstance(r, int)
     363        self.assertEqual(r, 2)
     364        r = self.c.query(q).fieldnum('y')
     365        self.assertIsInstance(r, int)
     366        self.assertEqual(r, 3)
    317367
    318368    def testNtuples(self):
     369        q = "select 1 where false"
     370        r = self.c.query(q).ntuples()
     371        self.assertIsInstance(r, int)
     372        self.assertEqual(r, 0)
    319373        q = ("select 1 as a, 2 as b, 3 as c, 4 as d"
    320374            " union select 5 as a, 6 as b, 7 as c, 8 as d")
    321375        r = self.c.query(q).ntuples()
    322         result = 2
    323         self.assertEqual(r, result)
     376        self.assertIsInstance(r, int)
     377        self.assertEqual(r, 2)
     378        q = ("select 1 union select 2 union select 3"
     379            " union select 4 union select 5 union select 6")
     380        r = self.c.query(q).ntuples()
     381        self.assertIsInstance(r, int)
     382        self.assertEqual(r, 6)
    324383
    325384    def testQuery(self):
     
    331390        q = "insert into test_table values (1)"
    332391        r = query(q)
    333         self.assertTrue(isinstance(r, int)), r
     392        self.assertIsInstance(r, int)
    334393        q = "insert into test_table select 2"
    335394        r = query(q)
    336         self.assertTrue(isinstance(r, int))
     395        self.assertIsInstance(r, int)
    337396        oid = r
    338397        q = "select oid from test_table where n=2"
     
    342401        self.assertEqual(len(r), 1)
    343402        r = r[0]
     403        self.assertIsInstance(r, int)
    344404        self.assertEqual(r, oid)
    345405        q = "insert into test_table select 3 union select 4 union select 5"
    346406        r = query(q)
    347         self.assertTrue(isinstance(r, str))
     407        self.assertIsInstance(r, str)
    348408        self.assertEqual(r, '3')
    349409        q = "update test_table set n=4 where n<5"
    350410        r = query(q)
    351         self.assertTrue(isinstance(r, str))
     411        self.assertIsInstance(r, str)
    352412        self.assertEqual(r, '4')
    353413        q = "delete from test_table"
    354414        r = query(q)
    355         self.assertTrue(isinstance(r, str))
     415        self.assertIsInstance(r, str)
    356416        self.assertEqual(r, '5')
    357417        query("drop table test_table")
     
    385445            self.c.query("notify test_notify")
    386446            r = self.c.getnotify()
    387             self.assertTrue(isinstance(r, tuple))
     447            self.assertIsInstance(r, tuple)
    388448            self.assertEqual(len(r), 3)
    389             self.assertTrue(isinstance(r[0], str))
    390             self.assertTrue(isinstance(r[1], int))
    391             self.assertTrue(isinstance(r[2], str))
     449            self.assertIsInstance(r[0], str)
     450            self.assertIsInstance(r[1], int)
     451            self.assertIsInstance(r[2], str)
    392452            self.assertEqual(r[0], 'test_notify')
    393453            self.assertEqual(r[2], '')
     
    401461                self.assertTrue(isinstance(r, tuple))
    402462                self.assertEqual(len(r), 3)
    403                 self.assertTrue(isinstance(r[0], str))
    404                 self.assertTrue(isinstance(r[1], int))
    405                 self.assertTrue(isinstance(r[2], str))
     463                self.assertIsInstance(r[0], str)
     464                self.assertIsInstance(r[1], int)
     465                self.assertIsInstance(r[2], str)
    406466                self.assertEqual(r[0], 'test_notify')
    407467                self.assertEqual(r[2], 'test_payload')
  • branches/4.x/module/TEST_PyGreSQL_classic_dbwrapper.py

    r541 r542  
    148148        def_port = 5432
    149149        port = self.db.port
     150        self.assertIsInstance(port, int)
    150151        self.assertEqual(port, def_port)
    151152        self.assertEqual(port, self.db.db.port)
     
    153154    def testAttributeProtocolVersion(self):
    154155        protocol_version = self.db.protocol_version
    155         self.assertTrue(isinstance(protocol_version, int))
     156        self.assertIsInstance(protocol_version, int)
    156157        self.assertTrue(2 <= protocol_version < 4)
    157158        self.assertEqual(protocol_version, self.db.db.protocol_version)
     
    159160    def testAttributeServerVersion(self):
    160161        server_version = self.db.server_version
    161         self.assertTrue(isinstance(server_version, int))
     162        self.assertIsInstance(server_version, int)
    162163        self.assertTrue(70400 <= server_version < 100000)
    163164        self.assertEqual(server_version, self.db.db.server_version)
     
    166167        status_ok = 1
    167168        status = self.db.status
     169        self.assertIsInstance(status, int)
    168170        self.assertEqual(status, status_ok)
    169171        self.assertEqual(status, self.db.db.status)
     
    172174        def_tty = ''
    173175        tty = self.db.tty
     176        self.assertIsInstance(tty, str)
    174177        self.assertEqual(tty, def_tty)
    175178        self.assertEqual(tty, self.db.db.tty)
     
    179182        user = self.db.user
    180183        self.assertTrue(user)
     184        self.assertIsInstance(user, str)
    181185        self.assertNotEqual(user, no_user)
    182186        self.assertEqual(user, self.db.db.user)
Note: See TracChangeset for help on using the changeset viewer.