29
29
30
30
from test .support import (
31
31
SHORT_TIMEOUT ,
32
- bigmemtest ,
33
32
check_disallow_instantiation ,
34
33
threading_helper ,
35
34
)
@@ -48,6 +47,22 @@ def managed_connect(*args, in_mem=False, **kwargs):
48
47
unlink (TESTFN )
49
48
50
49
50
+ # Helper for temporary memory databases
51
+ def memory_database ():
52
+ cx = sqlite .connect (":memory:" )
53
+ return contextlib .closing (cx )
54
+
55
+
56
+ # Temporarily limit a database connection parameter
57
+ @contextlib .contextmanager
58
+ def cx_limit (cx , category = sqlite .SQLITE_LIMIT_LENGTH , limit = 128 ):
59
+ try :
60
+ _prev = cx .setlimit (category , limit )
61
+ yield limit
62
+ finally :
63
+ cx .setlimit (category , _prev )
64
+
65
+
51
66
class ModuleTests (unittest .TestCase ):
52
67
def test_api_level (self ):
53
68
self .assertEqual (sqlite .apilevel , "2.0" ,
@@ -650,6 +665,15 @@ def __getitem__(slf, x):
650
665
with self .assertRaises (ZeroDivisionError ):
651
666
self .cu .execute ("select name from test where name=?" , L ())
652
667
668
+ def test_execute_too_many_params (self ):
669
+ category = sqlite .SQLITE_LIMIT_VARIABLE_NUMBER
670
+ msg = "too many SQL variables"
671
+ with cx_limit (self .cx , category = category , limit = 1 ):
672
+ self .cu .execute ("select * from test where id=?" , (1 ,))
673
+ with self .assertRaisesRegex (sqlite .OperationalError , msg ):
674
+ self .cu .execute ("select * from test where id!=? and id!=?" ,
675
+ (1 , 2 ))
676
+
653
677
def test_execute_dict_mapping (self ):
654
678
self .cu .execute ("insert into test(name) values ('foo')" )
655
679
self .cu .execute ("select name from test where name=:name" , {"name" : "foo" })
@@ -1036,14 +1060,12 @@ def test_cursor_executescript_with_surrogates(self):
1036
1060
insert into a(s) values ('\ud8ff ');
1037
1061
""" )
1038
1062
1039
- @unittest .skipUnless (sys .maxsize > 2 ** 32 , 'requires 64bit platform' )
1040
- @bigmemtest (size = 2 ** 31 , memuse = 3 , dry_run = False )
1041
- def test_cursor_executescript_too_large_script (self , maxsize ):
1042
- con = sqlite .connect (":memory:" )
1043
- cur = con .cursor ()
1044
- for size in 2 ** 31 - 1 , 2 ** 31 :
1045
- with self .assertRaises (sqlite .DataError ):
1046
- cur .executescript ("create table a(s);" .ljust (size ))
1063
+ def test_cursor_executescript_too_large_script (self ):
1064
+ msg = "query string is too large"
1065
+ with memory_database () as cx , cx_limit (cx ) as lim :
1066
+ cx .executescript ("select 'almost too large'" .ljust (lim - 1 ))
1067
+ with self .assertRaisesRegex (sqlite .DataError , msg ):
1068
+ cx .executescript ("select 'too large'" .ljust (lim ))
1047
1069
1048
1070
def test_cursor_executescript_tx_control (self ):
1049
1071
con = sqlite .connect (":memory:" )
0 commit comments