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"
@@ -389,7 +377,7 @@ def test_cursor(self):
389
377
def test_failed_open (self ):
390
378
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
391
379
with self .assertRaises (sqlite .OperationalError ):
392
- con = sqlite .connect (YOU_CANNOT_OPEN_THIS )
380
+ sqlite .connect (YOU_CANNOT_OPEN_THIS )
393
381
394
382
def test_close (self ):
395
383
self .cx .close ()
@@ -653,7 +641,9 @@ def test_open_with_path_like_object(self):
653
641
""" Checks that we can successfully connect to a database using an object that
654
642
is PathLike, i.e. has __fspath__(). """
655
643
path = FakePath (TESTFN )
656
- with managed_connect (path ) as cx :
644
+ self .addCleanup (unlink , path )
645
+ self .assertFalse (os .path .exists (path ))
646
+ with contextlib .closing (sqlite .connect (path )) as cx :
657
647
self .assertTrue (os .path .exists (path ))
658
648
cx .execute (self ._sql )
659
649
@@ -663,23 +653,26 @@ def test_open_with_path_like_object(self):
663
653
def test_open_with_undecodable_path (self ):
664
654
path = TESTFN_UNDECODABLE
665
655
self .addCleanup (unlink , path )
666
- with managed_connect (path , in_mem = True ) as cx :
656
+ self .assertFalse (os .path .exists (path ))
657
+ with contextlib .closing (sqlite .connect (path )) as cx :
667
658
self .assertTrue (os .path .exists (path ))
668
659
cx .execute (self ._sql )
669
660
670
661
def test_open_uri (self ):
671
662
path = TESTFN
663
+ self .addCleanup (unlink , path )
672
664
uri = "file:" + urllib .parse .quote (os .fsencode (path ))
673
665
self .assertFalse (os .path .exists (path ))
674
- with managed_connect ( uri , uri = True ) as cx :
666
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
675
667
self .assertTrue (os .path .exists (path ))
676
668
cx .execute (self ._sql )
677
669
678
670
def test_open_unquoted_uri (self ):
679
671
path = TESTFN
672
+ self .addCleanup (unlink , path )
680
673
uri = "file:" + path
681
674
self .assertFalse (os .path .exists (path ))
682
- with managed_connect ( uri , uri = True ) as cx :
675
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
683
676
self .assertTrue (os .path .exists (path ))
684
677
cx .execute (self ._sql )
685
678
@@ -695,7 +688,7 @@ def test_open_uri_readonly(self):
695
688
sqlite .connect (path ).close ()
696
689
self .assertTrue (os .path .exists (path ))
697
690
# Cannot modify new DB
698
- with managed_connect ( uri , uri = True ) as cx :
691
+ with contextlib . closing ( sqlite . connect ( uri , uri = True ) ) as cx :
699
692
with self .assertRaises (sqlite .OperationalError ):
700
693
cx .execute (self ._sql )
701
694
@@ -704,14 +697,12 @@ def test_open_uri_readonly(self):
704
697
@unittest .skipUnless (TESTFN_UNDECODABLE , "only works if there are undecodable paths" )
705
698
def test_open_undecodable_uri (self ):
706
699
path = TESTFN_UNDECODABLE
700
+ self .addCleanup (unlink , path )
707
701
uri = "file:" + urllib .parse .quote (path )
708
702
self .assertFalse (os .path .exists (path ))
709
- try :
710
- with managed_connect (uri , uri = True , in_mem = True ) as cx :
711
- self .assertTrue (os .path .exists (path ))
712
- cx .execute (self ._sql )
713
- finally :
714
- unlink (path )
703
+ with contextlib .closing (sqlite .connect (uri , uri = True )) as cx :
704
+ self .assertTrue (os .path .exists (path ))
705
+ cx .execute (self ._sql )
715
706
716
707
def test_factory_database_arg (self ):
717
708
def factory (database , * args , ** kwargs ):
@@ -722,12 +713,11 @@ def factory(database, *args, **kwargs):
722
713
for database in (TESTFN , os .fsencode (TESTFN ),
723
714
FakePath (TESTFN ), FakePath (os .fsencode (TESTFN ))):
724
715
database_arg = None
725
- with sqlite .connect (database , factory = factory ):
726
- pass
716
+ sqlite .connect (database , factory = factory ).close ()
727
717
self .assertEqual (database_arg , database )
728
718
729
719
def test_database_keyword (self ):
730
- with sqlite .connect (database = ":memory:" ) as cx :
720
+ with contextlib . closing ( sqlite .connect (database = ":memory:" ) ) as cx :
731
721
self .assertEqual (type (cx ), sqlite .Connection )
732
722
733
723
0 commit comments