MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / test_custom_codec_override_text

Method test_custom_codec_override_text

tests/test_codecs.py:1486–1531  ·  view source on GitHub ↗

Test overriding core codecs.

(self)

Source from the content-addressed store, hash-verified

1484 await conn.close()
1485
1486 async def test_custom_codec_override_text(self):
1487 """Test overriding core codecs."""
1488 import json
1489
1490 conn = await self.connect()
1491 try:
1492 def _encoder(value):
1493 return json.dumps(value)
1494
1495 def _decoder(value):
1496 return json.loads(value)
1497
1498 await conn.set_type_codec(
1499 'json', encoder=_encoder, decoder=_decoder,
1500 schema='pg_catalog', format='text'
1501 )
1502
1503 data = {'foo': 'bar', 'spam': 1}
1504 res = await conn.fetchval('SELECT $1::json', data)
1505 self.assertEqual(data, res)
1506
1507 res = await conn.fetchval('SELECT $1::json[]', [data])
1508 self.assertEqual([data], res)
1509
1510 await conn.execute('CREATE DOMAIN my_json AS json')
1511
1512 res = await conn.fetchval('SELECT $1::my_json', data)
1513 self.assertEqual(data, res)
1514
1515 def _encoder(value):
1516 return value
1517
1518 def _decoder(value):
1519 return value
1520
1521 await conn.set_type_codec(
1522 'uuid', encoder=_encoder, decoder=_decoder,
1523 schema='pg_catalog', format='text'
1524 )
1525
1526 data = '14058ad9-0118-4b7e-ac15-01bc13e2ccd1'
1527 res = await conn.fetchval('SELECT $1::uuid', data)
1528 self.assertEqual(res, data)
1529 finally:
1530 await conn.execute('DROP DOMAIN IF EXISTS my_json')
1531 await conn.close()
1532
1533 async def test_custom_codec_override_tuple(self):
1534 """Test overriding core codecs."""

Callers

nothing calls this directly

Calls 5

set_type_codecMethod · 0.80
connectMethod · 0.45
fetchvalMethod · 0.45
executeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected