Ignore:
Timestamp:
Jan 21, 2016, 1:49:28 PM (4 years ago)
Author:
cito
Message:

Add support for JSON and JSONB to pg and pgdb

This adds all necessary functions to make PyGreSQL automatically
convert between JSON columns and Python objects representing them.

The documentation has also been updated, see there for the details.

Also, tuples automatically bind to ROW expressions in pgdb now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_classic_functions.py

    r770 r774  
    1616    import unittest
    1717
     18import json
    1819import re
    1920
     
    182183    def testSetDecimalPoint(self):
    183184        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, '*')
     185        try:
     186            pg.set_decimal_point('*')
     187            r = pg.get_decimal_point()
     188            self.assertIsInstance(r, str)
     189            self.assertEqual(r, '*')
     190        finally:
     191            pg.set_decimal_point(point)
    189192        r = pg.get_decimal_point()
    190193        self.assertIsInstance(r, str)
     
    197200    def testSetDecimal(self):
    198201        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)
     202        try:
     203            pg.set_decimal(int)
     204            r = pg.get_decimal()
     205            self.assertIs(r, int)
     206        finally:
     207            pg.set_decimal(decimal_class)
    203208        r = pg.get_decimal()
    204209        self.assertIs(r, decimal_class)
     
    211216    def testSetBool(self):
    212217        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)
     218        try:
     219            pg.set_bool(True)
     220            r = pg.get_bool()
     221            pg.set_bool(use_bool)
     222            self.assertIsInstance(r, bool)
     223            self.assertIs(r, True)
     224            pg.set_bool(False)
     225            r = pg.get_bool()
     226            self.assertIsInstance(r, bool)
     227            self.assertIs(r, False)
     228        finally:
     229            pg.set_bool(use_bool)
    223230        r = pg.get_bool()
    224231        self.assertIsInstance(r, bool)
     
    232239    def testSetNamedresult(self):
    233240        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)
     241        try:
     242            pg.set_namedresult(None)
     243            r = pg.get_namedresult()
     244            self.assertIsNone(r)
     245            f = lambda q: q.getresult()
     246            pg.set_namedresult(f)
     247            r = pg.get_namedresult()
     248            self.assertIs(r, f)
     249            self.assertRaises(TypeError, pg.set_namedresult, 'invalid')
     250        finally:
     251            pg.set_namedresult(namedresult)
    239252        r = pg.get_namedresult()
    240253        self.assertIs(r, namedresult)
     254
     255    def testGetJsondecode(self):
     256        r = pg.get_jsondecode()
     257        self.assertTrue(callable(r))
     258        self.assertIs(r, json.loads)
     259
     260    def testSetJsondecode(self):
     261        jsondecode = pg.get_jsondecode()
     262        try:
     263            pg.set_jsondecode(None)
     264            r = pg.get_jsondecode()
     265            self.assertIsNone(r)
     266            pg.set_jsondecode(str)
     267            r = pg.get_jsondecode()
     268            self.assertIs(r, str)
     269            self.assertRaises(TypeError, pg.set_jsondecode, 'invalid')
     270        finally:
     271            pg.set_jsondecode(jsondecode)
     272        r = pg.get_jsondecode()
     273        self.assertIs(r, jsondecode)
    241274
    242275
Note: See TracChangeset for help on using the changeset viewer.