Test encoding/decoding of composite types.
(self)
| 895 | 1) |
| 896 | |
| 897 | async def test_composites(self): |
| 898 | """Test encoding/decoding of composite types.""" |
| 899 | await self.con.execute(''' |
| 900 | CREATE TYPE test_composite AS ( |
| 901 | a int, |
| 902 | b text, |
| 903 | c int[] |
| 904 | ) |
| 905 | ''') |
| 906 | |
| 907 | st = await self.con.prepare(''' |
| 908 | SELECT ROW(NULL, 1234, '5678', ROW(42, '42')) |
| 909 | ''') |
| 910 | |
| 911 | res = await st.fetchval() |
| 912 | |
| 913 | self.assertEqual(res, (None, 1234, '5678', (42, '42'))) |
| 914 | |
| 915 | with self.assertRaisesRegex( |
| 916 | asyncpg.UnsupportedClientFeatureError, |
| 917 | 'query argument \\$1: input of anonymous ' |
| 918 | 'composite types is not supported', |
| 919 | ): |
| 920 | await self.con.fetchval("SELECT (1, 'foo') = $1", (1, 'foo')) |
| 921 | |
| 922 | try: |
| 923 | st = await self.con.prepare(''' |
| 924 | SELECT ROW( |
| 925 | NULL, |
| 926 | '5678', |
| 927 | ARRAY[9, NULL, 11]::int[] |
| 928 | )::test_composite AS test |
| 929 | ''') |
| 930 | |
| 931 | res = await st.fetch() |
| 932 | res = res[0]['test'] |
| 933 | |
| 934 | self.assertIsNone(res['a']) |
| 935 | self.assertEqual(res['b'], '5678') |
| 936 | self.assertEqual(res['c'], [9, None, 11]) |
| 937 | |
| 938 | self.assertIsNone(res[0]) |
| 939 | self.assertEqual(res[1], '5678') |
| 940 | self.assertEqual(res[2], [9, None, 11]) |
| 941 | |
| 942 | at = st.get_attributes() |
| 943 | self.assertEqual(len(at), 1) |
| 944 | self.assertEqual(at[0].name, 'test') |
| 945 | self.assertEqual(at[0].type.name, 'test_composite') |
| 946 | self.assertEqual(at[0].type.kind, 'composite') |
| 947 | |
| 948 | res = await self.con.fetchval(''' |
| 949 | SELECT $1::test_composite |
| 950 | ''', res) |
| 951 | |
| 952 | # composite input as a mapping |
| 953 | res = await self.con.fetchval(''' |
| 954 | SELECT $1::test_composite |
nothing calls this directly
no test coverage detected