MCPcopy
hub / github.com/MagicStack/asyncpg / test_custom_codec_override_tuple

Method test_custom_codec_override_tuple

tests/test_codecs.py:1487–1545  ·  view source on GitHub ↗

Test overriding core codecs.

(self)

Source from the content-addressed store, hash-verified

1485 await conn.close()
1486
1487 async def test_custom_codec_override_tuple(self):
1488 """Test overriding core codecs."""
1489 cases = [
1490 ('date', (3,), '2000-01-04'),
1491 ('date', (2**31 - 1,), 'infinity'),
1492 ('date', (-2**31,), '-infinity'),
1493 ('time', (60 * 10**6,), '00:01:00'),
1494 ('timetz', (60 * 10**6, 12600), '00:01:00-03:30'),
1495 ('timestamp', (60 * 10**6,), '2000-01-01 00:01:00'),
1496 ('timestamp', (2**63 - 1,), 'infinity'),
1497 ('timestamp', (-2**63,), '-infinity'),
1498 ('timestamptz', (60 * 10**6,), '1999-12-31 19:01:00',
1499 "tab.v AT TIME ZONE 'EST'"),
1500 ('timestamptz', (2**63 - 1,), 'infinity'),
1501 ('timestamptz', (-2**63,), '-infinity'),
1502 ('interval', (2, 3, 1), '2 mons 3 days 00:00:00.000001')
1503 ]
1504
1505 conn = await self.connect()
1506
1507 def _encoder(value):
1508 return tuple(value)
1509
1510 def _decoder(value):
1511 return tuple(value)
1512
1513 try:
1514 for (typename, data, expected_result, *extra) in cases:
1515 with self.subTest(type=typename):
1516 await self.con.execute(
1517 'CREATE TABLE tab (v {})'.format(typename))
1518
1519 try:
1520 await conn.set_type_codec(
1521 typename, encoder=_encoder, decoder=_decoder,
1522 schema='pg_catalog', format='tuple'
1523 )
1524
1525 await conn.execute(
1526 'INSERT INTO tab VALUES ($1)', data)
1527
1528 res = await conn.fetchval('SELECT tab.v FROM tab')
1529 self.assertEqual(res, data)
1530
1531 await conn.reset_type_codec(
1532 typename, schema='pg_catalog')
1533
1534 if extra:
1535 val = extra[0]
1536 else:
1537 val = 'tab.v'
1538
1539 res = await conn.fetchval(
1540 'SELECT ({val})::text FROM tab'.format(val=val))
1541 self.assertEqual(res, expected_result)
1542 finally:
1543 await self.con.execute('DROP TABLE tab')
1544 finally:

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected