Author: cito
Date: Thu Nov 19 06:33:52 2015
New Revision: 546

Log:
Add tests for the remaining module level functions

Modified:
   branches/4.x/   (props changed)
   branches/4.x/module/TEST_PyGreSQL_classic_connection.py
   branches/4.x/module/TEST_PyGreSQL_classic_functions.py

Modified: branches/4.x/module/TEST_PyGreSQL_classic_connection.py
==============================================================================
--- branches/4.x/module/TEST_PyGreSQL_classic_connection.py     Wed Nov 18 
14:15:34 2015        (r545)
+++ branches/4.x/module/TEST_PyGreSQL_classic_connection.py     Thu Nov 19 
06:33:52 2015        (r546)
@@ -240,15 +240,15 @@
         self.assertIsInstance(v, str)
         self.assertEqual(v, result)
 
+    @unittest.skipUnless(namedtuple, 'Named tuples not available')
     def testNamedresult(self):
-        if namedtuple:
-            q = "select 0 as alias0"
-            result = [(0,)]
-            r = self.c.query(q).namedresult()
-            self.assertEqual(r, result)
-            v = r[0]
-            self.assertEqual(v._fields, ('alias0',))
-            self.assertEqual(v.alias0, 0)
+        q = "select 0 as alias0"
+        result = [(0,)]
+        r = self.c.query(q).namedresult()
+        self.assertEqual(r, result)
+        v = r[0]
+        self.assertEqual(v._fields, ('alias0',))
+        self.assertEqual(v.alias0, 0)
 
     def testGet3Cols(self):
         q = "select 1,2,3"
@@ -775,5 +775,102 @@
             self.c.query('''drop function bilbo_notice();''')
 
 
+class TestConfigFunctions(unittest.TestCase):
+    """Test the functions for changing default settings.
+
+    To test the effect of most of these functions, we need a database
+    connection.  That's why they are covered in this test module.
+
+    """
+
+    def setUp(self):
+        self.c = connect()
+
+    def tearDown(self):
+        self.c.close()
+
+    def testGetDecimalPoint(self):
+        point = pg.get_decimal_point()
+        self.assertIsInstance(point, str)
+        self.assertEqual(point, '.')
+
+    def testSetDecimalPoint(self):
+        d = pg.Decimal
+        point = pg.get_decimal_point()
+        query = self.c.query
+        # check that money values can be interpreted correctly
+        # if and only if the decimal point is set appropriately
+        # for the current lc_monetary setting
+        query("set lc_monetary='en_US'")
+        pg.set_decimal_point('.')
+        r = query("select '34.25'::money").getresult()[0][0]
+        self.assertIsInstance(r, d)
+        self.assertEqual(r, d('34.25'))
+        pg.set_decimal_point(',')
+        r = query("select '34.25'::money").getresult()[0][0]
+        self.assertNotEqual(r, d('34.25'))
+        query("set lc_monetary='de_DE'")
+        pg.set_decimal_point(',')
+        r = query("select '34,25'::money").getresult()[0][0]
+        self.assertIsInstance(r, d)
+        self.assertEqual(r, d('34.25'))
+        pg.set_decimal_point('.')
+        r = query("select '34,25'::money").getresult()[0][0]
+        self.assertNotEqual(r, d('34.25'))
+        pg.set_decimal_point(point)
+
+    def testSetDecimal(self):
+        d = pg.Decimal
+        query = self.c.query
+        r = query("select 3425::numeric").getresult()[0][0]
+        self.assertIsInstance(r, d)
+        self.assertEqual(r, d('3425'))
+        pg.set_decimal(long)
+        r = query("select 3425::numeric").getresult()[0][0]
+        self.assertNotIsInstance(r, d)
+        self.assertIsInstance(r, long)
+        self.assertEqual(r, 3425L)
+        pg.set_decimal(d)
+
+    @unittest.skipUnless(namedtuple, 'Named tuples not available')
+    def testSetNamedresult(self):
+        query = self.c.query
+
+        r = query("select 1 as x, 2 as y").namedresult()[0]
+        self.assertIsInstance(r, tuple)
+        self.assertEqual(r, (1, 2))
+        self.assertIsNot(type(r), tuple)
+        self.assertEqual(r._fields, ('x', 'y'))
+        self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
+        self.assertEqual(r.__class__.__name__, 'Row')
+
+        _namedresult = pg._namedresult
+        self.assertTrue(callable(_namedresult))
+        pg.set_namedresult(_namedresult)
+
+        r = query("select 1 as x, 2 as y").namedresult()[0]
+        self.assertIsInstance(r, tuple)
+        self.assertEqual(r, (1, 2))
+        self.assertIsNot(type(r), tuple)
+        self.assertEqual(r._fields, ('x', 'y'))
+        self.assertEqual(r._asdict(), {'x': 1, 'y': 2})
+        self.assertEqual(r.__class__.__name__, 'Row')
+
+        def _listresult(q):
+            return map(list, q.getresult())
+
+        pg.set_namedresult(_listresult)
+
+        try:
+            r = query("select 1 as x, 2 as y").namedresult()[0]
+            self.assertIsInstance(r, list)
+            self.assertEqual(r, [1, 2])
+            self.assertIsNot(type(r), tuple)
+            self.assertFalse(hasattr(r, '_fields'))
+            self.assertNotEqual(r.__class__.__name__, 'Row')
+        finally:
+            pg.set_namedresult(_namedresult)
+
+
 if __name__ == '__main__':
     unittest.main()

Modified: branches/4.x/module/TEST_PyGreSQL_classic_functions.py
==============================================================================
--- branches/4.x/module/TEST_PyGreSQL_classic_functions.py      Wed Nov 18 
14:15:34 2015        (r545)
+++ branches/4.x/module/TEST_PyGreSQL_classic_functions.py      Thu Nov 19 
06:33:52 2015        (r546)
@@ -275,5 +275,57 @@
         self.assertEqual(f(r'O\000ps\377!'), 'O\x00ps\xff!')
 
 
+class TestConfigFunctions(unittest.TestCase):
+    """Test the functions for changing default settings.
+
+    The effect of most of these cannot be tested here, because that
+    needs a database connection.  So we merely test their existence here.
+
+    """
+
+    def testGetDecimalPoint(self):
+        r = pg.get_decimal_point()
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, '.')
+
+    def testSetDecimalPoint(self):
+        point = pg.get_decimal_point()
+        pg.set_decimal_point('*')
+        r = pg.get_decimal_point()
+        self.assertIsInstance(r, str)
+        self.assertEqual(r, '*')
+        pg.set_decimal_point(point)
+
+    def testSetDecimal(self):
+        decimal_class = pg.Decimal
+        pg.set_decimal(long)
+        pg.set_decimal(decimal_class)
+
+    def testSetNamedresult(self):
+        pg.set_namedresult(tuple)
+
+
+class TestModuleConstants(unittest.TestCase):
+    """Test the existence of the documented module constants."""
+
+    def testVersion(self):
+        v = pg.version
+        self.assertIsInstance(v, str)
+        v = v.split('.')
+        self.assertTrue(2 <= len(v) <= 3)
+        for w in v:
+            self.assertTrue(1 <= len(w) <= 2)
+            self.assertTrue(w.isdigit())
+
+    def testLargeObjectIntConstants(self):
+        names = 'INV_READ INV_WRITE SEEK_SET SEEK_CUR SEEK_END'.split()
+        for name in names:
+            try:
+                value = getattr(pg, name)
+            except AttributeError:
+                self.fail('Module constant %s is missing' % name)
+            self.assertIsInstance(value, int)
+
+
 if __name__ == '__main__':
     unittest.main()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to