Test encoding/decoding of arrays (particularly multidimensional).
(self)
| 835 | await stmt.fetchval(sample) |
| 836 | |
| 837 | async def test_arrays(self): |
| 838 | """Test encoding/decoding of arrays (particularly multidimensional).""" |
| 839 | cases = [ |
| 840 | ( |
| 841 | r"SELECT '[1:3][-1:0]={{1,2},{4,5},{6,7}}'::int[]", |
| 842 | [[1, 2], [4, 5], [6, 7]] |
| 843 | ), |
| 844 | ( |
| 845 | r"SELECT '{{{{{{1}}}}}}'::int[]", |
| 846 | [[[[[[1]]]]]] |
| 847 | ), |
| 848 | ( |
| 849 | r"SELECT '{1, 2, NULL}'::int[]::anyarray", |
| 850 | [1, 2, None] |
| 851 | ), |
| 852 | ( |
| 853 | r"SELECT '{}'::int[]", |
| 854 | [] |
| 855 | ), |
| 856 | ] |
| 857 | |
| 858 | for sql, expected in cases: |
| 859 | with self.subTest(sql=sql): |
| 860 | res = await self.con.fetchval(sql) |
| 861 | self.assertEqual(res, expected) |
| 862 | |
| 863 | with self.assertRaises(asyncpg.ProgramLimitExceededError): |
| 864 | await self.con.fetchval("SELECT '{{{{{{{1}}}}}}}'::int[]") |
| 865 | |
| 866 | cases = [ |
| 867 | [None], |
| 868 | [1, 2, 3, 4, 5, 6], |
| 869 | [[1, 2], [4, 5], [6, 7]], |
| 870 | [[[1], [2]], [[4], [5]], [[None], [7]]], |
| 871 | [[[[[[1]]]]]], |
| 872 | [[[[[[None]]]]]] |
| 873 | ] |
| 874 | |
| 875 | st = await self.con.prepare( |
| 876 | "SELECT $1::int[]" |
| 877 | ) |
| 878 | |
| 879 | for case in cases: |
| 880 | with self.subTest(case=case): |
| 881 | result = await st.fetchval(case) |
| 882 | err_msg = ( |
| 883 | "failed to return array data as-is; " |
| 884 | "gave {!r}, received {!r}".format( |
| 885 | case, result)) |
| 886 | |
| 887 | self.assertEqual(result, case, err_msg) |
| 888 | |
| 889 | # A sized iterable is fine as array input. |
| 890 | class Iterable: |
| 891 | def __iter__(self): |
| 892 | return iter([1, 2, 3]) |
| 893 | |
| 894 | def __len__(self): |
nothing calls this directly
no test coverage detected