11import sqlite3 as sqlite
22import unittest
33
4+ from .util import memory_database
5+
46
57class BackupTests (unittest .TestCase ):
68 def setUp (self ):
@@ -32,32 +34,32 @@ def test_bad_target_same_connection(self):
3234 self .cx .backup (self .cx )
3335
3436 def test_bad_target_closed_connection (self ):
35- bck = sqlite . connect ( ':memory:' )
36- bck .close ()
37- with self .assertRaises (sqlite .ProgrammingError ):
38- self .cx .backup (bck )
37+ with memory_database () as bck :
38+ bck .close ()
39+ with self .assertRaises (sqlite .ProgrammingError ):
40+ self .cx .backup (bck )
3941
4042 def test_bad_source_closed_connection (self ):
41- bck = sqlite . connect ( ':memory:' )
42- source = sqlite .connect (":memory:" )
43- source .close ()
44- with self .assertRaises (sqlite .ProgrammingError ):
45- source .backup (bck )
43+ with memory_database () as bck :
44+ source = sqlite .connect (":memory:" )
45+ source .close ()
46+ with self .assertRaises (sqlite .ProgrammingError ):
47+ source .backup (bck )
4648
4749 def test_bad_target_in_transaction (self ):
48- bck = sqlite . connect ( ':memory:' )
49- bck .execute ('CREATE TABLE bar (key INTEGER)' )
50- bck .executemany ('INSERT INTO bar (key) VALUES (?)' , [(3 ,), (4 ,)])
51- with self .assertRaises (sqlite .OperationalError ) as cm :
52- self .cx .backup (bck )
50+ with memory_database () as bck :
51+ bck .execute ('CREATE TABLE bar (key INTEGER)' )
52+ bck .executemany ('INSERT INTO bar (key) VALUES (?)' , [(3 ,), (4 ,)])
53+ with self .assertRaises (sqlite .OperationalError ) as cm :
54+ self .cx .backup (bck )
5355
5456 def test_keyword_only_args (self ):
5557 with self .assertRaises (TypeError ):
56- with sqlite . connect ( ':memory:' ) as bck :
58+ with memory_database ( ) as bck :
5759 self .cx .backup (bck , 1 )
5860
5961 def test_simple (self ):
60- with sqlite . connect ( ':memory:' ) as bck :
62+ with memory_database ( ) as bck :
6163 self .cx .backup (bck )
6264 self .verify_backup (bck )
6365
@@ -67,7 +69,7 @@ def test_progress(self):
6769 def progress (status , remaining , total ):
6870 journal .append (status )
6971
70- with sqlite . connect ( ':memory:' ) as bck :
72+ with memory_database ( ) as bck :
7173 self .cx .backup (bck , pages = 1 , progress = progress )
7274 self .verify_backup (bck )
7375
@@ -81,7 +83,7 @@ def test_progress_all_pages_at_once_1(self):
8183 def progress (status , remaining , total ):
8284 journal .append (remaining )
8385
84- with sqlite . connect ( ':memory:' ) as bck :
86+ with memory_database ( ) as bck :
8587 self .cx .backup (bck , progress = progress )
8688 self .verify_backup (bck )
8789
@@ -94,7 +96,7 @@ def test_progress_all_pages_at_once_2(self):
9496 def progress (status , remaining , total ):
9597 journal .append (remaining )
9698
97- with sqlite . connect ( ':memory:' ) as bck :
99+ with memory_database ( ) as bck :
98100 self .cx .backup (bck , pages = - 1 , progress = progress )
99101 self .verify_backup (bck )
100102
@@ -103,7 +105,7 @@ def progress(status, remaining, total):
103105
104106 def test_non_callable_progress (self ):
105107 with self .assertRaises (TypeError ) as cm :
106- with sqlite . connect ( ':memory:' ) as bck :
108+ with memory_database ( ) as bck :
107109 self .cx .backup (bck , pages = 1 , progress = 'bar' )
108110 self .assertEqual (str (cm .exception ), 'progress argument must be a callable' )
109111
@@ -116,7 +118,7 @@ def progress(status, remaining, total):
116118 self .cx .commit ()
117119 journal .append (remaining )
118120
119- with sqlite . connect ( ':memory:' ) as bck :
121+ with memory_database ( ) as bck :
120122 self .cx .backup (bck , pages = 1 , progress = progress )
121123 self .verify_backup (bck )
122124
@@ -140,20 +142,20 @@ def progress(status, remaining, total):
140142 self .assertEqual (str (err .exception ), 'nearly out of space' )
141143
142144 def test_database_source_name (self ):
143- with sqlite . connect ( ':memory:' ) as bck :
145+ with memory_database ( ) as bck :
144146 self .cx .backup (bck , name = 'main' )
145- with sqlite . connect ( ':memory:' ) as bck :
147+ with memory_database ( ) as bck :
146148 self .cx .backup (bck , name = 'temp' )
147149 with self .assertRaises (sqlite .OperationalError ) as cm :
148- with sqlite . connect ( ':memory:' ) as bck :
150+ with memory_database ( ) as bck :
149151 self .cx .backup (bck , name = 'non-existing' )
150152 self .assertIn ("unknown database" , str (cm .exception ))
151153
152154 self .cx .execute ("ATTACH DATABASE ':memory:' AS attached_db" )
153155 self .cx .execute ('CREATE TABLE attached_db.foo (key INTEGER)' )
154156 self .cx .executemany ('INSERT INTO attached_db.foo (key) VALUES (?)' , [(3 ,), (4 ,)])
155157 self .cx .commit ()
156- with sqlite . connect ( ':memory:' ) as bck :
158+ with memory_database ( ) as bck :
157159 self .cx .backup (bck , name = 'attached_db' )
158160 self .verify_backup (bck )
159161
0 commit comments