Changeset 546 for branches/4.x


Ignore:
Timestamp:
Nov 19, 2015, 6:33:52 AM (4 years ago)
Author:
cito
Message:

Add tests for the remaining module level functions

Location:
branches/4.x
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.x

    • Property svn:ignore
      •  

        old new  
         1.idea
        12BETA
  • branches/4.x/module/TEST_PyGreSQL_classic_connection.py

    r545 r546  
    241241        self.assertEqual(v, result)
    242242
     243    @unittest.skipUnless(namedtuple, 'Named tuples not available')
    243244    def testNamedresult(self):
    244         if namedtuple:
    245             q = "select 0 as alias0"
    246             result = [(0,)]
    247             r = self.c.query(q).namedresult()
    248             self.assertEqual(r, result)
    249             v = r[0]
    250             self.assertEqual(v._fields, ('alias0',))
    251             self.assertEqual(v.alias0, 0)
     245        q = "select 0 as alias0"
     246        result = [(0,)]
     247        r = self.c.query(q).namedresult()
     248        self.assertEqual(r, result)
     249        v = r[0]
     250        self.assertEqual(v._fields, ('alias0',))
     251        self.assertEqual(v.alias0, 0)
    252252
    253253    def testGet3Cols(self):
     
    776776
    777777
     778class TestConfigFunctions(unittest.TestCase):
     779    """Test the functions for changing default settings.
     780
     781    To test the effect of most of these functions, we need a database
     782    connection.  That's why they are covered in this test module.
     783
     784    """
     785
     786    def setUp(self):
     787        self.c = connect()
     788
     789    def tearDown(self):
     790        self.c.close()
     791
     792    def testGetDecimalPoint(self):
     793        point = pg.get_decimal_point()
     794        self.assertIsInstance(point, str)
     795        self.assertEqual(point, '.')
     796
     797    def testSetDecimalPoint(self):
     798        d = pg.Decimal
     799        point = pg.get_decimal_point()
     800        query = self.c.query
     801        # check that money values can be interpreted correctly
     802        # if and only if the decimal point is set appropriately
     803        # for the current lc_monetary setting
     804        query("set lc_monetary='en_US'")
     805        pg.set_decimal_point('.')
     806        r = query("select '34.25'::money").getresult()[0][0]
     807        self.assertIsInstance(r, d)
     808        self.assertEqual(r, d('34.25'))
     809        pg.set_decimal_point(',')
     810        r = query("select '34.25'::money").getresult()[0][0]
     811        self.assertNotEqual(r, d('34.25'))
     812        query("set lc_monetary='de_DE'")
     813        pg.set_decimal_point(',')
     814        r = query("select '34,25'::money").getresult()[0][0]
     815        self.assertIsInstance(r, d)
     816        self.assertEqual(r, d('34.25'))
     817        pg.set_decimal_point('.')
     818        r = query("select '34,25'::money").getresult()[0][0]
     819        self.assertNotEqual(r, d('34.25'))
     820        pg.set_decimal_point(point)
     821
     822    def testSetDecimal(self):
     823        d = pg.Decimal
     824        query = self.c.query
     825        r = query("select 3425::numeric").getresult()[0][0]
     826        self.assertIsInstance(r, d)
     827        self.assertEqual(r, d('3425'))
     828        pg.set_decimal(long)
     829        r = query("select 3425::numeric").getresult()[0][0]
     830        self.assertNotIsInstance(r, d)
     831        self.assertIsInstance(r, long)
     832        self.assertEqual(r, 3425L)
     833        pg.set_decimal(d)
     834
     835    @unittest.skipUnless(namedtuple, 'Named tuples not available')
     836    def testSetNamedresult(self):
     837        query = self.c.query
     838
     839        r = query("select 1 as x, 2 as y").namedresult()[0]
     840        self.assertIsInstance(r, tuple)
     841        self.assertEqual(r, (1, 2))
     842        self.assertIsNot(type(r), tuple)
     843        self.assertEqual(r._fields, ('x', 'y'))
     844        self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
     845        self.assertEqual(r.__class__.__name__, 'Row')
     846
     847        _namedresult = pg._namedresult
     848        self.assertTrue(callable(_namedresult))
     849        pg.set_namedresult(_namedresult)
     850
     851        r = query("select 1 as x, 2 as y").namedresult()[0]
     852        self.assertIsInstance(r, tuple)
     853        self.assertEqual(r, (1, 2))
     854        self.assertIsNot(type(r), tuple)
     855        self.assertEqual(r._fields, ('x', 'y'))
     856        self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
     857        self.assertEqual(r.__class__.__name__, 'Row')
     858
     859        def _listresult(q):
     860            return map(list, q.getresult())
     861
     862        pg.set_namedresult(_listresult)
     863
     864        try:
     865            r = query("select 1 as x, 2 as y").namedresult()[0]
     866            self.assertIsInstance(r, list)
     867            self.assertEqual(r, [1, 2])
     868            self.assertIsNot(type(r), tuple)
     869            self.assertFalse(hasattr(r, '_fields'))
     870            self.assertNotEqual(r.__class__.__name__, 'Row')
     871        finally:
     872            pg.set_namedresult(_namedresult)
     873
     874
    778875if __name__ == '__main__':
    779876    unittest.main()
  • branches/4.x/module/TEST_PyGreSQL_classic_functions.py

    r541 r546  
    276276
    277277
     278class TestConfigFunctions(unittest.TestCase):
     279    """Test the functions for changing default settings.
     280
     281    The effect of most of these cannot be tested here, because that
     282    needs a database connection.  So we merely test their existence here.
     283
     284    """
     285
     286    def testGetDecimalPoint(self):
     287        r = pg.get_decimal_point()
     288        self.assertIsInstance(r, str)
     289        self.assertEqual(r, '.')
     290
     291    def testSetDecimalPoint(self):
     292        point = pg.get_decimal_point()
     293        pg.set_decimal_point('*')
     294        r = pg.get_decimal_point()
     295        self.assertIsInstance(r, str)
     296        self.assertEqual(r, '*')
     297        pg.set_decimal_point(point)
     298
     299    def testSetDecimal(self):
     300        decimal_class = pg.Decimal
     301        pg.set_decimal(long)
     302        pg.set_decimal(decimal_class)
     303
     304    def testSetNamedresult(self):
     305        pg.set_namedresult(tuple)
     306
     307
     308class TestModuleConstants(unittest.TestCase):
     309    """Test the existence of the documented module constants."""
     310
     311    def testVersion(self):
     312        v = pg.version
     313        self.assertIsInstance(v, str)
     314        v = v.split('.')
     315        self.assertTrue(2 <= len(v) <= 3)
     316        for w in v:
     317            self.assertTrue(1 <= len(w) <= 2)
     318            self.assertTrue(w.isdigit())
     319
     320    def testLargeObjectIntConstants(self):
     321        names = 'INV_READ INV_WRITE SEEK_SET SEEK_CUR SEEK_END'.split()
     322        for name in names:
     323            try:
     324                value = getattr(pg, name)
     325            except AttributeError:
     326                self.fail('Module constant %s is missing' % name)
     327            self.assertIsInstance(value, int)
     328
     329
    278330if __name__ == '__main__':
    279331    unittest.main()
Note: See TracChangeset for help on using the changeset viewer.