Changeset 817 for trunk/tests


Ignore:
Timestamp:
Feb 4, 2016, 3:18:08 PM (3 years ago)
Author:
cito
Message:

Support the hstore data type

Added adaptation and typecasting of the hstore type as Python dictionaries.
For the typecasting, a fast parser has been added to the C extension.

Location:
trunk/tests
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_classic_dbwrapper.py

    r816 r817  
    36843684        self.assertEqual(r[1][0], dt[1])
    36853685
     3686    def testHstore(self):
     3687        try:
     3688            self.db.query("select 'k=>v'::hstore")
     3689        except pg.ProgrammingEror:
     3690            try:
     3691                self.db.query("create extension hstore")
     3692            except pg.ProgrammingError:
     3693                self.skipTest("hstore extension not enabled")
     3694        d = {'k': 'v', 'foo': 'bar', 'baz': 'whatever',
     3695            '1a': 'anything at all', '2=b': 'value = 2', '3>c': 'value > 3',
     3696            '4"c': 'value " 4', "5'c": "value ' 5", 'hello, world': '"hi!"',
     3697            'None': None, 'NULL': 'NULL', 'empty': ''}
     3698        q = "select $1::hstore"
     3699        r = self.db.query(q, (pg.Hstore(d),)).getresult()[0][0]
     3700        self.assertIsInstance(r, dict)
     3701        self.assertEqual(r, d)
     3702
    36863703    def testDbTypesInfo(self):
    36873704        dbtypes = self.db.dbtypes
  • trunk/tests/test_classic_functions.py

    r814 r817  
    615615                expected = replace_comma(expected)
    616616                self.assertEqual(f(string, cast, b';'), expected)
     617
     618
     619class TestParseHStore(unittest.TestCase):
     620    """Test the hstore parser."""
     621
     622    test_strings = [
     623        ('', {}),
     624        ('=>', ValueError),
     625        ('""=>', ValueError),
     626        ('=>""', ValueError),
     627        ('""=>""', {'': ''}),
     628        ('NULL=>NULL', {'NULL': None}),
     629        ('null=>null', {'null': None}),
     630        ('NULL=>"NULL"', {'NULL': 'NULL'}),
     631        ('null=>"null"', {'null': 'null'}),
     632        ('k', ValueError),
     633        ('k,', ValueError),
     634        ('k=', ValueError),
     635        ('k=>', ValueError),
     636        ('k=>v', {'k': 'v'}),
     637        ('k=>v,', ValueError),
     638        (' k => v ', {'k': 'v'}),
     639        ('   k   =>   v   ', {'k': 'v'}),
     640        ('" k " => " v "', {' k ': ' v '}),
     641        ('"k=>v', ValueError),
     642        ('k=>"v', ValueError),
     643        ('"1-a" => "anything at all"', {'1-a': 'anything at all'}),
     644        ('k => v, foo => bar, baz => whatever,'
     645                ' "1-a" => "anything at all"',
     646            {'k': 'v', 'foo': 'bar', 'baz': 'whatever',
     647            '1-a': 'anything at all'}),
     648        ('"Hello, World!"=>"Hi!"', {'Hello, World!': 'Hi!'}),
     649        ('"Hi!"=>"Hello, World!"', {'Hi!': 'Hello, World!'}),
     650        ('"k=>v"=>k\=\>v', {'k=>v': 'k=>v'}),
     651        ('k\=\>v=>"k=>v"', {'k=>v': 'k=>v'}),
     652        ('a\\,b=>a,b=>a', {'a,b': 'a', 'b': 'a'})]
     653
     654    def testParser(self):
     655        f = pg.cast_hstore
     656
     657        self.assertRaises(TypeError, f)
     658        self.assertRaises(TypeError, f, None)
     659        self.assertRaises(TypeError, f, 42)
     660        self.assertRaises(TypeError, f, '', None)
     661
     662        for string, expected in self.test_strings:
     663            if expected is ValueError:
     664                self.assertRaises(ValueError, f, string)
     665            else:
     666                self.assertEqual(f(string), expected)
    617667
    618668
  • trunk/tests/test_dbapi20.py

    r816 r817  
    590590        finally:
    591591            con.close()
     592
     593    def test_hstore(self):
     594        con = self._connect()
     595        try:
     596            cur = con.cursor()
     597            cur.execute("select 'k=>v'::hstore")
     598        except pgdb.ProgrammingError:
     599            try:
     600                cur.execute("create extension hstore")
     601            except pgdb.ProgrammingError:
     602                self.skipTest("hstore extension not enabled")
     603        finally:
     604            con.close()
     605        d = {'k': 'v', 'foo': 'bar', 'baz': 'whatever',
     606            '1a': 'anything at all', '2=b': 'value = 2', '3>c': 'value > 3',
     607            '4"c': 'value " 4', "5'c": "value ' 5", 'hello, world': '"hi!"',
     608            'None': None, 'NULL': 'NULL', 'empty': ''}
     609        con = self._connect()
     610        try:
     611            cur = con.cursor()
     612            cur.execute("select %s::hstore", (pgdb.Hstore(d),))
     613            result = cur.fetchone()[0]
     614        finally:
     615            con.close()
     616        self.assertIsInstance(result, dict)
     617        self.assertEqual(result, d)
    592618
    593619    def test_insert_array(self):
Note: See TracChangeset for help on using the changeset viewer.