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

Method test_extra_codec_alias

tests/test_codecs.py:1159–1230  ·  view source on GitHub ↗

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

(self)

Source from the content-addressed store, hash-verified

1157 await self.con.fetch("SELECT $1::int4multirange", 1)
1158
1159 async def test_extra_codec_alias(self):
1160 """Test encoding/decoding of a builtin non-pg_catalog codec."""
1161 await self.con.execute('''
1162 CREATE DOMAIN my_dec_t AS decimal;
1163 CREATE EXTENSION IF NOT EXISTS hstore;
1164 CREATE TYPE rec_t AS ( i my_dec_t, h hstore );
1165 ''')
1166
1167 try:
1168 await self.con.set_builtin_type_codec(
1169 'hstore', codec_name='pg_contrib.hstore')
1170
1171 cases = [
1172 {'ham': 'spam', 'nada': None},
1173 {}
1174 ]
1175
1176 st = await self.con.prepare('''
1177 SELECT $1::hstore AS result
1178 ''')
1179
1180 for case in cases:
1181 res = await st.fetchval(case)
1182 self.assertEqual(res, case)
1183
1184 res = await self.con.fetchval('''
1185 SELECT $1::hstore AS result
1186 ''', (('foo', '2'), ('bar', '3')))
1187
1188 self.assertEqual(res, {'foo': '2', 'bar': '3'})
1189
1190 with self.assertRaisesRegex(asyncpg.DataError,
1191 'null value not allowed'):
1192 await self.con.fetchval('''
1193 SELECT $1::hstore AS result
1194 ''', {None: '1'})
1195
1196 await self.con.set_builtin_type_codec(
1197 'my_dec_t', codec_name='decimal')
1198
1199 res = await self.con.fetchval('''
1200 SELECT $1::my_dec_t AS result
1201 ''', 44)
1202
1203 self.assertEqual(res, 44)
1204
1205 # Both my_dec_t and hstore are decoded in binary
1206 res = await self.con.fetchval('''
1207 SELECT ($1::my_dec_t, 'a=>1'::hstore)::rec_t AS result
1208 ''', 44)
1209
1210 self.assertEqual(res, (44, {'a': '1'}))
1211
1212 # Now, declare only the text format for my_dec_t
1213 await self.con.reset_type_codec('my_dec_t')
1214 await self.con.set_builtin_type_codec(
1215 'my_dec_t', codec_name='decimal', format='text')
1216

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected