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

Method test_extra_codec_alias

tests/test_codecs.py:1113–1184  ·  view source on GitHub ↗

Test encoding/decoding of a builtin non-pg_catalog codec.

(self)

Source from the content-addressed store, hash-verified

1111 await self.con.fetch("SELECT $1::int4multirange", 1)
1112
1113 async def test_extra_codec_alias(self):
1114 """Test encoding/decoding of a builtin non-pg_catalog codec."""
1115 await self.con.execute('''
1116 CREATE DOMAIN my_dec_t AS decimal;
1117 CREATE EXTENSION IF NOT EXISTS hstore;
1118 CREATE TYPE rec_t AS ( i my_dec_t, h hstore );
1119 ''')
1120
1121 try:
1122 await self.con.set_builtin_type_codec(
1123 'hstore', codec_name='pg_contrib.hstore')
1124
1125 cases = [
1126 {'ham': 'spam', 'nada': None},
1127 {}
1128 ]
1129
1130 st = await self.con.prepare('''
1131 SELECT $1::hstore AS result
1132 ''')
1133
1134 for case in cases:
1135 res = await st.fetchval(case)
1136 self.assertEqual(res, case)
1137
1138 res = await self.con.fetchval('''
1139 SELECT $1::hstore AS result
1140 ''', (('foo', '2'), ('bar', '3')))
1141
1142 self.assertEqual(res, {'foo': '2', 'bar': '3'})
1143
1144 with self.assertRaisesRegex(asyncpg.DataError,
1145 'null value not allowed'):
1146 await self.con.fetchval('''
1147 SELECT $1::hstore AS result
1148 ''', {None: '1'})
1149
1150 await self.con.set_builtin_type_codec(
1151 'my_dec_t', codec_name='decimal')
1152
1153 res = await self.con.fetchval('''
1154 SELECT $1::my_dec_t AS result
1155 ''', 44)
1156
1157 self.assertEqual(res, 44)
1158
1159 # Both my_dec_t and hstore are decoded in binary
1160 res = await self.con.fetchval('''
1161 SELECT ($1::my_dec_t, 'a=>1'::hstore)::rec_t AS result
1162 ''', 44)
1163
1164 self.assertEqual(res, (44, {'a': '1'}))
1165
1166 # Now, declare only the text format for my_dec_t
1167 await self.con.reset_type_codec('my_dec_t')
1168 await self.con.set_builtin_type_codec(
1169 'my_dec_t', codec_name='decimal', format='text')
1170

Callers

nothing calls this directly

Calls 5

prepareMethod · 0.80
reset_type_codecMethod · 0.80
executeMethod · 0.45
fetchvalMethod · 0.45

Tested by

no test coverage detected