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

Method test_custom_codec_on_enum_array

tests/test_codecs.py:1388–1414  ·  view source on GitHub ↗

Test encoding/decoding using a custom codec on an enum array. Bug: https://github.com/MagicStack/asyncpg/issues/590

(self)

Source from the content-addressed store, hash-verified

1386 await self.con.execute('DROP TYPE custom_codec_t')
1387
1388 async def test_custom_codec_on_enum_array(self):
1389 """Test encoding/decoding using a custom codec on an enum array.
1390
1391 Bug: https://github.com/MagicStack/asyncpg/issues/590
1392 """
1393 await self.con.execute('''
1394 CREATE TYPE custom_codec_t AS ENUM ('foo', 'bar', 'baz')
1395 ''')
1396
1397 try:
1398 await self.con.set_type_codec(
1399 'custom_codec_t',
1400 encoder=lambda v: str(v).lstrip('enum :'),
1401 decoder=lambda v: 'enum: ' + str(v))
1402
1403 v = await self.con.fetchval(
1404 "SELECT ARRAY['foo', 'bar']::custom_codec_t[]")
1405 self.assertEqual(v, ['enum: foo', 'enum: bar'])
1406
1407 v = await self.con.fetchval(
1408 'SELECT ARRAY[$1]::custom_codec_t[]', 'foo')
1409 self.assertEqual(v, ['enum: foo'])
1410
1411 v = await self.con.fetchval("SELECT 'foo'::custom_codec_t")
1412 self.assertEqual(v, 'enum: foo')
1413 finally:
1414 await self.con.execute('DROP TYPE custom_codec_t')
1415
1416 async def test_custom_codec_override_binary(self):
1417 """Test overriding core codecs."""

Callers

nothing calls this directly

Calls 3

set_type_codecMethod · 0.80
executeMethod · 0.45
fetchvalMethod · 0.45

Tested by

no test coverage detected