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

Method test_composites

tests/test_codecs.py:943–1020  ·  view source on GitHub ↗

Test encoding/decoding of composite types.

(self)

Source from the content-addressed store, hash-verified

941 1)
942
943 async def test_composites(self):
944 """Test encoding/decoding of composite types."""
945 await self.con.execute('''
946 CREATE TYPE test_composite AS (
947 a int,
948 b text,
949 c int[]
950 )
951 ''')
952
953 st = await self.con.prepare('''
954 SELECT ROW(NULL, 1234, '5678', ROW(42, '42'))
955 ''')
956
957 res = await st.fetchval()
958
959 self.assertEqual(res, (None, 1234, '5678', (42, '42')))
960
961 with self.assertRaisesRegex(
962 asyncpg.UnsupportedClientFeatureError,
963 'query argument \\$1: input of anonymous '
964 'composite types is not supported',
965 ):
966 await self.con.fetchval("SELECT (1, 'foo') = $1", (1, 'foo'))
967
968 try:
969 st = await self.con.prepare('''
970 SELECT ROW(
971 NULL,
972 '5678',
973 ARRAY[9, NULL, 11]::int[]
974 )::test_composite AS test
975 ''')
976
977 res = await st.fetch()
978 res = res[0]['test']
979
980 self.assertIsNone(res['a'])
981 self.assertEqual(res['b'], '5678')
982 self.assertEqual(res['c'], [9, None, 11])
983
984 self.assertIsNone(res[0])
985 self.assertEqual(res[1], '5678')
986 self.assertEqual(res[2], [9, None, 11])
987
988 at = st.get_attributes()
989 self.assertEqual(len(at), 1)
990 self.assertEqual(at[0].name, 'test')
991 self.assertEqual(at[0].type.name, 'test_composite')
992 self.assertEqual(at[0].type.kind, 'composite')
993
994 res = await self.con.fetchval('''
995 SELECT $1::test_composite
996 ''', res)
997
998 # composite input as a mapping
999 res = await self.con.fetchval('''
1000 SELECT $1::test_composite

Callers

nothing calls this directly

Calls 5

get_attributesMethod · 0.80
executeMethod · 0.45
prepareMethod · 0.45
fetchvalMethod · 0.45
fetchMethod · 0.45

Tested by

no test coverage detected