This repository was archived by the owner on May 21, 2020. It is now read-only.
forked from husio/python-sqlite3-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
148 lines (124 loc) · 4.85 KB
/
tests.py
File metadata and controls
148 lines (124 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import unittest
import sqlite3
def extend_importpath():
# extend import paths so that sqlitebck shared object file will be visible
# for import script
import sys
import fnmatch
libbck_path = None
basedir = os.path.abspath(os.path.dirname(__file__))
for root, dirs, files in os.walk(basedir):
if libbck_path:
break
for fname in files:
if fnmatch.fnmatch(fname, 'sqlitebck*.so'):
libbck_path = os.path.join(basedir, root)
break
if not libbck_path:
raise Exception("Cannot find \"sqlitebck\" library file")
print(libbck_path)
sys.path.insert(0, libbck_path)
extend_importpath()
import sqlitebck
class CopyInheritedTest(unittest.TestCase):
def setUp(self):
class Connection(sqlite3.Connection):
pass
self.inherited = Connection(':memory:')
self.native = sqlite3.connect(':memory:')
def tearDown(self):
self.inherited.close()
self.native.close()
def create_test_db(self, conn):
curr = conn.cursor()
curr.execute('CREATE TABLE t(col VARCHAR(20))')
curr.execute('INSERT INTO t(col) VALUES(\'test\')')
conn.commit()
curr.close()
def test_inerited_source(self):
self.create_test_db(self.inherited)
sqlitebck.copy(self.inherited, self.native)
curr = self.native.cursor()
curr.execute('SELECT col FROM t')
self.assertEqual(curr.fetchone(), ('test', ))
def test_inerited_destination(self):
self.create_test_db(self.native)
sqlitebck.copy(self.native, self.inherited)
curr = self.inherited.cursor()
curr.execute('SELECT col FROM t')
self.assertEqual(curr.fetchone(), ('test', ))
class SqliteBackpTest(unittest.TestCase):
def setUp(self):
self.db_filename = '/tmp/dest_db.sqlite3.db'
self.open_connections = []
def tearDown(self):
if os.path.isfile(self.db_filename):
os.unlink(self.db_filename)
# cleanup databases connections...
for db_conn in self.open_connections:
db_conn.close()
def test_copy_from_not_sqlite_db(self):
source_non_db = object()
dest_db = sqlite3.connect(':memory:')
self.assertRaises(TypeError,
sqlitebck.copy, source_non_db, dest_db)
def test_copy_to_not_sqlite_db(self):
source_db = sqlite3.connect(':memory:')
dest_non_db = object()
self.assertRaises(TypeError,
sqlitebck.copy, source_db, dest_non_db)
def test_copy_from_memory_database(self):
source_db = sqlite3.connect(':memory:')
self.open_connections.append(source_db)
s_curr = source_db.cursor()
s_curr.execute('CREATE TABLE foo(bar VARCHAR(20))')
for value in ["foo", "bar", "baz"]:
s_curr.execute('INSERT INTO foo VALUES (?)', (value, ))
source_db.commit()
s_curr.execute('SELECT * FROM foo ORDER BY bar DESC')
source_data = s_curr.fetchall()
s_curr.close()
# save database to file
db_dest = sqlite3.connect(self.db_filename)
sqlitebck.copy(source=source_db, dest=db_dest)
# not try to fetch this data from file database
self.open_connections.append(db_dest)
d_curr = db_dest.cursor()
d_curr.execute('SELECT * FROM foo ORDER BY bar DESC')
dest_data = d_curr.fetchall()
d_curr.close()
self.assertEqual(source_data, dest_data)
def test_load_from_file_into_memory(self):
source_db = sqlite3.connect(self.db_filename)
self.open_connections.append(source_db)
s_curr = source_db.cursor()
s_curr.execute('CREATE TABLE baz(foo VARCHAR(20))')
for value in ["abc", "x-x-x", "123"]:
s_curr.execute('INSERT INTO baz VALUES (?)', (value, ))
source_db.commit()
s_curr.execute('SELECT * FROM baz ORDER BY foo DESC')
source_data = s_curr.fetchall()
s_curr.close()
# save database to file
db_dest = sqlite3.connect(':memory:')
sqlitebck.copy(source_db, db_dest)
# remove file database
os.unlink(self.db_filename)
# not try to fetch this data from file database
self.open_connections.append(db_dest)
d_curr = db_dest.cursor()
d_curr.execute('SELECT * FROM baz ORDER BY foo DESC')
dest_data = d_curr.fetchall()
d_curr.close()
self.assertEqual(source_data, dest_data)
def test_database_locked(self):
db = sqlite3.connect(':memory:')
db2 = sqlite3.connect(':memory:')
curr = db.cursor()
curr.execute('CREATE TABLE foo(bar INTEGER)')
curr.execute('INSERT INTO foo VALUES(2)')
curr.close()
self.assertRaises(sqlite3.DatabaseError, sqlitebck.copy, db, db2)
if __name__ == '__main__':
unittest.main()