@@ -542,17 +542,19 @@ async def test_numeric(self):
542
542
"SELECT $1::numeric" , decimal .Decimal ('sNaN' ))
543
543
self .assertTrue (res .is_nan ())
544
544
545
- with self .assertRaisesRegex (ValueError , 'numeric type does not '
546
- 'support infinite values' ):
545
+ with self .assertRaisesRegex (asyncpg .DataError ,
546
+ 'numeric type does not '
547
+ 'support infinite values' ):
547
548
await self .con .fetchval (
548
549
"SELECT $1::numeric" , decimal .Decimal ('-Inf' ))
549
550
550
- with self .assertRaisesRegex (ValueError , 'numeric type does not '
551
- 'support infinite values' ):
551
+ with self .assertRaisesRegex (asyncpg .DataError ,
552
+ 'numeric type does not '
553
+ 'support infinite values' ):
552
554
await self .con .fetchval (
553
555
"SELECT $1::numeric" , decimal .Decimal ('+Inf' ))
554
556
555
- with self .assertRaises ( decimal . InvalidOperation ):
557
+ with self .assertRaisesRegex ( asyncpg . DataError , 'invalid' ):
556
558
await self .con .fetchval (
557
559
"SELECT $1::numeric" , 'invalid' )
558
560
@@ -578,91 +580,95 @@ async def test_unhandled_type_fallback(self):
578
580
579
581
async def test_invalid_input (self ):
580
582
cases = [
581
- ('bytea' , TypeError , 'a bytes-like object is required' , [
583
+ ('bytea' , 'a bytes-like object is required' , [
582
584
1 ,
583
585
'aaa'
584
586
]),
585
- ('bool' , TypeError , 'a boolean is required' , [
587
+ ('bool' , 'a boolean is required' , [
586
588
1 ,
587
589
]),
588
- ('int2' , TypeError , 'an integer is required' , [
590
+ ('int2' , 'an integer is required' , [
589
591
'2' ,
590
592
'aa' ,
591
593
]),
592
- ('smallint' , OverflowError , 'int16 value out of range' , [
594
+ ('smallint' , ' value out of int16 range' , [
593
595
2 ** 256 , # check for the same exception for any big numbers
594
596
decimal .Decimal ("2000000000000000000000000000000" ),
595
597
0xffff ,
596
598
0xffffffff ,
597
599
32768 ,
598
600
- 32769
599
601
]),
600
- ('float4' , ValueError , 'float value too large ' , [
602
+ ('float4' , ' value out of float32 range ' , [
601
603
4.1 * 10 ** 40 ,
602
604
- 4.1 * 10 ** 40 ,
603
605
]),
604
- ('int4' , TypeError , 'an integer is required' , [
606
+ ('int4' , 'an integer is required' , [
605
607
'2' ,
606
608
'aa' ,
607
609
]),
608
- ('int' , OverflowError , 'int32 value out of range' , [
610
+ ('int' , ' value out of int32 range' , [
609
611
2 ** 256 , # check for the same exception for any big numbers
610
612
decimal .Decimal ("2000000000000000000000000000000" ),
611
613
0xffffffff ,
612
614
2 ** 31 ,
613
615
- 2 ** 31 - 1 ,
614
616
]),
615
- ('int8' , TypeError , 'an integer is required' , [
617
+ ('int8' , 'an integer is required' , [
616
618
'2' ,
617
619
'aa' ,
618
620
]),
619
- ('bigint' , OverflowError , 'int64 value out of range' , [
621
+ ('bigint' , ' value out of int64 range' , [
620
622
2 ** 256 , # check for the same exception for any big numbers
621
623
decimal .Decimal ("2000000000000000000000000000000" ),
622
624
0xffffffffffffffff ,
623
625
2 ** 63 ,
624
626
- 2 ** 63 - 1 ,
625
627
]),
626
- ('text' , TypeError , 'expected str, got bytes' , [
628
+ ('text' , 'expected str, got bytes' , [
627
629
b'foo'
628
630
]),
629
- ('text' , TypeError , 'expected str, got list' , [
631
+ ('text' , 'expected str, got list' , [
630
632
[1 ]
631
633
]),
632
- ('tid' , TypeError , 'list or tuple expected' , [
634
+ ('tid' , 'list or tuple expected' , [
633
635
b'foo'
634
636
]),
635
- ('tid' , ValueError , 'invalid number of elements in tid tuple' , [
637
+ ('tid' , 'invalid number of elements in tid tuple' , [
636
638
[],
637
639
(),
638
640
[1 , 2 , 3 ],
639
641
(4 ,),
640
642
]),
641
- ('tid' , OverflowError , 'tuple id block value out of range' , [
643
+ ('tid' , 'tuple id block value out of uint32 range' , [
642
644
(- 1 , 0 ),
643
645
(2 ** 256 , 0 ),
644
646
(0xffffffff + 1 , 0 ),
645
647
(2 ** 32 , 0 ),
646
648
]),
647
- ('tid' , OverflowError , 'tuple id offset value out of range' , [
649
+ ('tid' , 'tuple id offset value out of uint16 range' , [
648
650
(0 , - 1 ),
649
651
(0 , 2 ** 256 ),
650
652
(0 , 0xffff + 1 ),
651
653
(0 , 0xffffffff ),
652
654
(0 , 65536 ),
653
655
]),
654
- ('oid' , OverflowError , 'uint32 value out of range' , [
656
+ ('oid' , ' value out of uint32 range' , [
655
657
2 ** 32 ,
656
658
- 1 ,
657
659
]),
658
660
]
659
661
660
- for typname , errcls , errmsg , data in cases :
662
+ for typname , errmsg , data in cases :
661
663
stmt = await self .con .prepare ("SELECT $1::" + typname )
662
664
663
665
for sample in data :
664
666
with self .subTest (sample = sample , typname = typname ):
665
- with self .assertRaisesRegex (errcls , errmsg ):
667
+ full_errmsg = (
668
+ r'invalid input for query argument \$1:.*' + errmsg )
669
+
670
+ with self .assertRaisesRegex (
671
+ asyncpg .DataError , full_errmsg ):
666
672
await stmt .fetchval (sample )
667
673
668
674
async def test_arrays (self ):
@@ -733,37 +739,39 @@ class SomeContainer:
733
739
def __contains__ (self , item ):
734
740
return False
735
741
736
- with self .assertRaisesRegex (TypeError ,
742
+ with self .assertRaisesRegex (asyncpg . DataError ,
737
743
'sized iterable container expected' ):
738
744
result = await self .con .fetchval ("SELECT $1::int[]" ,
739
745
SomeContainer ())
740
746
741
- with self .assertRaisesRegex (ValueError , 'dimensions' ):
747
+ with self .assertRaisesRegex (asyncpg . DataError , 'dimensions' ):
742
748
await self .con .fetchval (
743
749
"SELECT $1::int[]" ,
744
750
[[[[[[[1 ]]]]]]])
745
751
746
- with self .assertRaisesRegex (ValueError , 'non-homogeneous' ):
752
+ with self .assertRaisesRegex (asyncpg . DataError , 'non-homogeneous' ):
747
753
await self .con .fetchval (
748
754
"SELECT $1::int[]" ,
749
755
[1 , [1 ]])
750
756
751
- with self .assertRaisesRegex (ValueError , 'non-homogeneous' ):
757
+ with self .assertRaisesRegex (asyncpg . DataError , 'non-homogeneous' ):
752
758
await self .con .fetchval (
753
759
"SELECT $1::int[]" ,
754
760
[[1 ], 1 , [2 ]])
755
761
756
- with self .assertRaisesRegex (ValueError , 'invalid array element' ):
762
+ with self .assertRaisesRegex (asyncpg .DataError ,
763
+ 'invalid array element' ):
757
764
await self .con .fetchval (
758
765
"SELECT $1::int[]" ,
759
766
[1 , 't' , 2 ])
760
767
761
- with self .assertRaisesRegex (ValueError , 'invalid array element' ):
768
+ with self .assertRaisesRegex (asyncpg .DataError ,
769
+ 'invalid array element' ):
762
770
await self .con .fetchval (
763
771
"SELECT $1::int[]" ,
764
772
[[1 ], ['t' ], [2 ]])
765
773
766
- with self .assertRaisesRegex (TypeError ,
774
+ with self .assertRaisesRegex (asyncpg . DataError ,
767
775
'sized iterable container expected' ):
768
776
await self .con .fetchval (
769
777
"SELECT $1::int[]" ,
@@ -887,11 +895,11 @@ async def test_range_types(self):
887
895
self .assertEqual (result , expected )
888
896
889
897
with self .assertRaisesRegex (
890
- TypeError , 'list, tuple or Range object expected' ):
898
+ asyncpg . DataError , 'list, tuple or Range object expected' ):
891
899
await self .con .fetch ("SELECT $1::int4range" , 'aa' )
892
900
893
901
with self .assertRaisesRegex (
894
- ValueError , 'expected 0, 1 or 2 elements' ):
902
+ asyncpg . DataError , 'expected 0, 1 or 2 elements' ):
895
903
await self .con .fetch ("SELECT $1::int4range" , (0 , 2 , 3 ))
896
904
897
905
cases = [(asyncpg .Range (0 , 1 ), asyncpg .Range (0 , 1 ), 1 ),
@@ -933,7 +941,8 @@ async def test_extra_codec_alias(self):
933
941
934
942
self .assertEqual (res , {'foo' : '2' , 'bar' : '3' })
935
943
936
- with self .assertRaisesRegex (ValueError , 'null value not allowed' ):
944
+ with self .assertRaisesRegex (asyncpg .DataError ,
945
+ 'null value not allowed' ):
937
946
await self .con .fetchval ('''
938
947
SELECT $1::hstore AS result
939
948
''' , {None : '1' })
0 commit comments