1
1
import sqlite3 as sqlite
2
2
import unittest
3
3
4
+ from .util import memory_database
5
+
4
6
5
7
class BackupTests (unittest .TestCase ):
6
8
def setUp (self ):
@@ -32,32 +34,32 @@ def test_bad_target_same_connection(self):
32
34
self .cx .backup (self .cx )
33
35
34
36
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 )
39
41
40
42
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 )
46
48
47
49
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 )
53
55
54
56
def test_keyword_only_args (self ):
55
57
with self .assertRaises (TypeError ):
56
- with sqlite . connect ( ':memory:' ) as bck :
58
+ with memory_database ( ) as bck :
57
59
self .cx .backup (bck , 1 )
58
60
59
61
def test_simple (self ):
60
- with sqlite . connect ( ':memory:' ) as bck :
62
+ with memory_database ( ) as bck :
61
63
self .cx .backup (bck )
62
64
self .verify_backup (bck )
63
65
@@ -67,7 +69,7 @@ def test_progress(self):
67
69
def progress (status , remaining , total ):
68
70
journal .append (status )
69
71
70
- with sqlite . connect ( ':memory:' ) as bck :
72
+ with memory_database ( ) as bck :
71
73
self .cx .backup (bck , pages = 1 , progress = progress )
72
74
self .verify_backup (bck )
73
75
@@ -81,7 +83,7 @@ def test_progress_all_pages_at_once_1(self):
81
83
def progress (status , remaining , total ):
82
84
journal .append (remaining )
83
85
84
- with sqlite . connect ( ':memory:' ) as bck :
86
+ with memory_database ( ) as bck :
85
87
self .cx .backup (bck , progress = progress )
86
88
self .verify_backup (bck )
87
89
@@ -94,7 +96,7 @@ def test_progress_all_pages_at_once_2(self):
94
96
def progress (status , remaining , total ):
95
97
journal .append (remaining )
96
98
97
- with sqlite . connect ( ':memory:' ) as bck :
99
+ with memory_database ( ) as bck :
98
100
self .cx .backup (bck , pages = - 1 , progress = progress )
99
101
self .verify_backup (bck )
100
102
@@ -103,7 +105,7 @@ def progress(status, remaining, total):
103
105
104
106
def test_non_callable_progress (self ):
105
107
with self .assertRaises (TypeError ) as cm :
106
- with sqlite . connect ( ':memory:' ) as bck :
108
+ with memory_database ( ) as bck :
107
109
self .cx .backup (bck , pages = 1 , progress = 'bar' )
108
110
self .assertEqual (str (cm .exception ), 'progress argument must be a callable' )
109
111
@@ -116,7 +118,7 @@ def progress(status, remaining, total):
116
118
self .cx .commit ()
117
119
journal .append (remaining )
118
120
119
- with sqlite . connect ( ':memory:' ) as bck :
121
+ with memory_database ( ) as bck :
120
122
self .cx .backup (bck , pages = 1 , progress = progress )
121
123
self .verify_backup (bck )
122
124
@@ -140,20 +142,20 @@ def progress(status, remaining, total):
140
142
self .assertEqual (str (err .exception ), 'nearly out of space' )
141
143
142
144
def test_database_source_name (self ):
143
- with sqlite . connect ( ':memory:' ) as bck :
145
+ with memory_database ( ) as bck :
144
146
self .cx .backup (bck , name = 'main' )
145
- with sqlite . connect ( ':memory:' ) as bck :
147
+ with memory_database ( ) as bck :
146
148
self .cx .backup (bck , name = 'temp' )
147
149
with self .assertRaises (sqlite .OperationalError ) as cm :
148
- with sqlite . connect ( ':memory:' ) as bck :
150
+ with memory_database ( ) as bck :
149
151
self .cx .backup (bck , name = 'non-existing' )
150
152
self .assertIn ("unknown database" , str (cm .exception ))
151
153
152
154
self .cx .execute ("ATTACH DATABASE ':memory:' AS attached_db" )
153
155
self .cx .execute ('CREATE TABLE attached_db.foo (key INTEGER)' )
154
156
self .cx .executemany ('INSERT INTO attached_db.foo (key) VALUES (?)' , [(3 ,), (4 ,)])
155
157
self .cx .commit ()
156
- with sqlite . connect ( ':memory:' ) as bck :
158
+ with memory_database ( ) as bck :
157
159
self .cx .backup (bck , name = 'attached_db' )
158
160
self .verify_backup (bck )
159
161
0 commit comments