Test overriding core codecs.
(self)
| 1531 | await conn.close() |
| 1532 | |
| 1533 | async def test_custom_codec_override_tuple(self): |
| 1534 | """Test overriding core codecs.""" |
| 1535 | cases = [ |
| 1536 | ('date', (3,), '2000-01-04'), |
| 1537 | ('date', (2**31 - 1,), 'infinity'), |
| 1538 | ('date', (-2**31,), '-infinity'), |
| 1539 | ('time', (60 * 10**6,), '00:01:00'), |
| 1540 | ('timetz', (60 * 10**6, 12600), '00:01:00-03:30'), |
| 1541 | ('timestamp', (60 * 10**6,), '2000-01-01 00:01:00'), |
| 1542 | ('timestamp', (2**63 - 1,), 'infinity'), |
| 1543 | ('timestamp', (-2**63,), '-infinity'), |
| 1544 | ('timestamptz', (60 * 10**6,), '1999-12-31 19:01:00', |
| 1545 | "tab.v AT TIME ZONE 'EST'"), |
| 1546 | ('timestamptz', (2**63 - 1,), 'infinity'), |
| 1547 | ('timestamptz', (-2**63,), '-infinity'), |
| 1548 | ('interval', (2, 3, 1), '2 mons 3 days 00:00:00.000001') |
| 1549 | ] |
| 1550 | |
| 1551 | conn = await self.connect() |
| 1552 | |
| 1553 | def _encoder(value): |
| 1554 | return tuple(value) |
| 1555 | |
| 1556 | def _decoder(value): |
| 1557 | return tuple(value) |
| 1558 | |
| 1559 | try: |
| 1560 | for (typename, data, expected_result, *extra) in cases: |
| 1561 | with self.subTest(type=typename): |
| 1562 | await self.con.execute( |
| 1563 | 'CREATE TABLE tab (v {})'.format(typename)) |
| 1564 | |
| 1565 | try: |
| 1566 | await conn.set_type_codec( |
| 1567 | typename, encoder=_encoder, decoder=_decoder, |
| 1568 | schema='pg_catalog', format='tuple' |
| 1569 | ) |
| 1570 | |
| 1571 | await conn.execute( |
| 1572 | 'INSERT INTO tab VALUES ($1)', data) |
| 1573 | |
| 1574 | res = await conn.fetchval('SELECT tab.v FROM tab') |
| 1575 | self.assertEqual(res, data) |
| 1576 | |
| 1577 | await conn.reset_type_codec( |
| 1578 | typename, schema='pg_catalog') |
| 1579 | |
| 1580 | if extra: |
| 1581 | val = extra[0] |
| 1582 | else: |
| 1583 | val = 'tab.v' |
| 1584 | |
| 1585 | res = await conn.fetchval( |
| 1586 | 'SELECT ({val})::text FROM tab'.format(val=val)) |
| 1587 | self.assertEqual(res, expected_result) |
| 1588 | finally: |
| 1589 | await self.con.execute('DROP TABLE tab') |
| 1590 | finally: |
nothing calls this directly
no test coverage detected