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

Method test_custom_codec_text

tests/test_codecs.py:1186–1238  ·  view source on GitHub ↗

Test encoding/decoding using a custom codec in text mode.

(self)

Source from the content-addressed store, hash-verified

1184 ''')
1185
1186 async def test_custom_codec_text(self):
1187 """Test encoding/decoding using a custom codec in text mode."""
1188 await self.con.execute('''
1189 CREATE EXTENSION IF NOT EXISTS hstore
1190 ''')
1191
1192 def hstore_decoder(data):
1193 result = {}
1194 items = data.split(',')
1195 for item in items:
1196 k, _, v = item.partition('=>')
1197 result[k.strip('"')] = v.strip('"')
1198
1199 return result
1200
1201 def hstore_encoder(obj):
1202 return ','.join('{}=>{}'.format(k, v) for k, v in obj.items())
1203
1204 try:
1205 await self.con.set_type_codec('hstore', encoder=hstore_encoder,
1206 decoder=hstore_decoder)
1207
1208 st = await self.con.prepare('''
1209 SELECT $1::hstore AS result
1210 ''')
1211
1212 res = await st.fetchrow({'ham': 'spam'})
1213 res = res['result']
1214
1215 self.assertEqual(res, {'ham': 'spam'})
1216
1217 pt = st.get_parameters()
1218 self.assertTrue(isinstance(pt, tuple))
1219 self.assertEqual(len(pt), 1)
1220 self.assertEqual(pt[0].name, 'hstore')
1221 self.assertEqual(pt[0].kind, 'scalar')
1222 self.assertEqual(pt[0].schema, 'public')
1223
1224 at = st.get_attributes()
1225 self.assertTrue(isinstance(at, tuple))
1226 self.assertEqual(len(at), 1)
1227 self.assertEqual(at[0].name, 'result')
1228 self.assertEqual(at[0].type, pt[0])
1229
1230 err = 'cannot use custom codec on type public._hstore'
1231 with self.assertRaisesRegex(asyncpg.InterfaceError, err):
1232 await self.con.set_type_codec('_hstore',
1233 encoder=hstore_encoder,
1234 decoder=hstore_decoder)
1235 finally:
1236 await self.con.execute('''
1237 DROP EXTENSION hstore
1238 ''')
1239
1240 async def test_custom_codec_binary(self):
1241 """Test encoding/decoding using a custom codec in binary mode."""

Callers

nothing calls this directly

Calls 6

set_type_codecMethod · 0.80
prepareMethod · 0.80
get_parametersMethod · 0.80
get_attributesMethod · 0.80
executeMethod · 0.45
fetchrowMethod · 0.45

Tested by

no test coverage detected