Test encoding/decoding of arrays (particularly multidimensional).
(self)
| 789 | await stmt.fetchval(sample) |
| 790 | |
| 791 | async def test_arrays(self): |
| 792 | """Test encoding/decoding of arrays (particularly multidimensional).""" |
| 793 | cases = [ |
| 794 | ( |
| 795 | r"SELECT '[1:3][-1:0]={{1,2},{4,5},{6,7}}'::int[]", |
| 796 | [[1, 2], [4, 5], [6, 7]] |
| 797 | ), |
| 798 | ( |
| 799 | r"SELECT '{{{{{{1}}}}}}'::int[]", |
| 800 | [[[[[[1]]]]]] |
| 801 | ), |
| 802 | ( |
| 803 | r"SELECT '{1, 2, NULL}'::int[]::anyarray", |
| 804 | [1, 2, None] |
| 805 | ), |
| 806 | ( |
| 807 | r"SELECT '{}'::int[]", |
| 808 | [] |
| 809 | ), |
| 810 | ] |
| 811 | |
| 812 | for sql, expected in cases: |
| 813 | with self.subTest(sql=sql): |
| 814 | res = await self.con.fetchval(sql) |
| 815 | self.assertEqual(res, expected) |
| 816 | |
| 817 | with self.assertRaises(asyncpg.ProgramLimitExceededError): |
| 818 | await self.con.fetchval("SELECT '{{{{{{{1}}}}}}}'::int[]") |
| 819 | |
| 820 | cases = [ |
| 821 | [None], |
| 822 | [1, 2, 3, 4, 5, 6], |
| 823 | [[1, 2], [4, 5], [6, 7]], |
| 824 | [[[1], [2]], [[4], [5]], [[None], [7]]], |
| 825 | [[[[[[1]]]]]], |
| 826 | [[[[[[None]]]]]] |
| 827 | ] |
| 828 | |
| 829 | st = await self.con.prepare( |
| 830 | "SELECT $1::int[]" |
| 831 | ) |
| 832 | |
| 833 | for case in cases: |
| 834 | with self.subTest(case=case): |
| 835 | result = await st.fetchval(case) |
| 836 | err_msg = ( |
| 837 | "failed to return array data as-is; " |
| 838 | "gave {!r}, received {!r}".format( |
| 839 | case, result)) |
| 840 | |
| 841 | self.assertEqual(result, case, err_msg) |
| 842 | |
| 843 | # A sized iterable is fine as array input. |
| 844 | class Iterable: |
| 845 | def __iter__(self): |
| 846 | return iter([1, 2, 3]) |
| 847 | |
| 848 | def __len__(self): |
nothing calls this directly
no test coverage detected