36
36
from test .support .os_helper import TESTFN , TESTFN_UNDECODABLE , unlink , temp_dir , FakePath
37
37
38
38
39
- # Helper for tests using TESTFN
40
- @contextlib .contextmanager
41
- def managed_connect (* args , in_mem = False , ** kwargs ):
42
- cx = sqlite .connect (* args , ** kwargs )
43
- try :
44
- yield cx
45
- finally :
46
- cx .close ()
47
- if not in_mem :
48
- unlink (TESTFN )
49
-
50
-
51
39
# Helper for temporary memory databases
52
40
def memory_database (* args , ** kwargs ):
53
41
cx = sqlite .connect (":memory:" , * args , ** kwargs )
@@ -331,7 +319,7 @@ def test_error_code_on_exception(self):
331
319
@unittest .skipIf (sqlite .sqlite_version_info <= (3 , 7 , 16 ),
332
320
"Requires SQLite 3.7.16 or newer" )
333
321
def test_extended_error_code_on_exception (self ):
334
- with managed_connect ( ":memory:" , in_mem = True ) as con :
322
+ with memory_database ( ) as con :
335
323
with con :
336
324
con .execute ("create table t(t integer check(t > 0))" )
337
325
errmsg = "constraint failed"
@@ -398,7 +386,7 @@ def test_cursor(self):
398
386
def test_failed_open (self ):
399
387
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
400
388
with self .assertRaises (sqlite .OperationalError ):
401
- con = sqlite .connect (YOU_CANNOT_OPEN_THIS )
389
+ sqlite .connect (YOU_CANNOT_OPEN_THIS )
402
390
403
391
def test_close (self ):
404
392
self .cx .close ()
@@ -662,7 +650,9 @@ def test_open_with_path_like_object(self):
662
650
""" Checks that we can successfully connect to a database using an object that
663
651
is PathLike, i.e. has __fspath__(). """
664
652
path = FakePath (TESTFN )
665
- with managed_connect (path ) as cx :
653
+ self .addCleanup (unlink , path )
654
+ self .assertFalse (os .path .exists (path ))
655
+ with contextlib .closing (sqlite .connect (path )) as cx :
666
656
self .assertTrue (os .path .exists (path ))
667
657
cx .execute (self ._sql )
668
658
@@ -672,23 +662,26 @@ def test_open_with_path_like_object(self):
672
662
def test_open_with_undecodable_path (self ):
673
663
path = TESTFN_UNDECODABLE
674
664
self .addCleanup (unlink , path )
675
- with managed_connect (path , in_mem = True ) as cx :
665
+ self .assertFalse (os .path .exists (path ))
666
+ with contextlib .closing (sqlite .connect (path )) as cx :
676
667
self .assertTrue (os .path .exists (path ))
677
668
cx .execute (self ._sql )
678
669
679
670
def test_open_uri (self ):
680
671
path = TESTFN
672
+ self .addCleanup (unlink , path )
681
673
uri = "file:" + urllib .parse .quote (os .fsencode (path ))
682
674
self .assertFalse (os .path .exists (path ))
683
- with managed_connect ( uri , uri = True ) as cx :
675
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
684
676
self .assertTrue (os .path .exists (path ))
685
677
cx .execute (self ._sql )
686
678
687
679
def test_open_unquoted_uri (self ):
688
680
path = TESTFN
681
+ self .addCleanup (unlink , path )
689
682
uri = "file:" + path
690
683
self .assertFalse (os .path .exists (path ))
691
- with managed_connect ( uri , uri = True ) as cx :
684
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
692
685
self .assertTrue (os .path .exists (path ))
693
686
cx .execute (self ._sql )
694
687
@@ -704,7 +697,7 @@ def test_open_uri_readonly(self):
704
697
sqlite .connect (path ).close ()
705
698
self .assertTrue (os .path .exists (path ))
706
699
# Cannot modify new DB
707
- with managed_connect ( uri , uri = True ) as cx :
700
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
708
701
with self .assertRaises (sqlite .OperationalError ):
709
702
cx .execute (self ._sql )
710
703
@@ -713,14 +706,12 @@ def test_open_uri_readonly(self):
713
706
@unittest .skipUnless (TESTFN_UNDECODABLE , "only works if there are undecodable paths" )
714
707
def test_open_undecodable_uri (self ):
715
708
path = TESTFN_UNDECODABLE
709
+ self .addCleanup (unlink , path )
716
710
uri = "file:" + urllib .parse .quote (path )
717
711
self .assertFalse (os .path .exists (path ))
718
- try :
719
- with managed_connect (uri , uri = True , in_mem = True ) as cx :
720
- self .assertTrue (os .path .exists (path ))
721
- cx .execute (self ._sql )
722
- finally :
723
- unlink (path )
712
+ with contextlib .closing (sqlite .connect (uri , uri = True )) as cx :
713
+ self .assertTrue (os .path .exists (path ))
714
+ cx .execute (self ._sql )
724
715
725
716
def test_factory_database_arg (self ):
726
717
def factory (database , * args , ** kwargs ):
@@ -731,12 +722,11 @@ def factory(database, *args, **kwargs):
731
722
for database in (TESTFN , os .fsencode (TESTFN ),
732
723
FakePath (TESTFN ), FakePath (os .fsencode (TESTFN ))):
733
724
database_arg = None
734
- with sqlite .connect (database , factory = factory ):
735
- pass
725
+ sqlite .connect (database , factory = factory ).close ()
736
726
self .assertEqual (database_arg , database )
737
727
738
728
def test_database_keyword (self ):
739
- with sqlite .connect (database = ":memory:" ) as cx :
729
+ with contextlib . closing ( sqlite .connect (database = ":memory:" ) ) as cx :
740
730
self .assertEqual (type (cx ), sqlite .Connection )
741
731
742
732
0 commit comments