forked from simon-weber/Instant-SQLite-Audit-Trail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·137 lines (100 loc) · 4.2 KB
/
test.py
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
#!/usr/bin/env python3
import sqlite3
import unittest
import audit
class TestAudit(unittest.TestCase):
def setUp(self):
self.conn = sqlite3.connect(':memory:')
self.conn.execute('CREATE TABLE tab(c1, c2)')
audit.attach_log(self.conn)
def tearDown(self):
audit.detach_log(self.conn)
self.conn.close()
def test_string_to_python(self):
self.conn.execute("INSERT INTO tab VALUES('a', 'b')")
r = self.conn.execute("SELECT * FROM _audit").fetchone()
py_val = audit.to_python(r[4])
self.assertEqual(py_val,
[['c1', 'a'],
['c2', 'b']])
def test_nums_to_python(self):
self.conn.execute("INSERT INTO tab VALUES(5, 3.14)")
r = self.conn.execute("SELECT * FROM _audit").fetchone()
py_val = audit.to_python(r[4])
self.assertEqual(py_val,
[['c1', 5],
['c2', 3.14]])
def test_null_to_python(self):
self.conn.execute("INSERT INTO tab VALUES(NULL, NULL)")
r = self.conn.execute("SELECT * FROM _audit").fetchone()
py_val = audit.to_python(r[4])
self.assertEqual(py_val,
[['c1', None],
['c2', None]])
def test_insert(self):
self.conn.execute("INSERT INTO tab VALUES('audit', 'this')")
audit_rows = self.conn.execute("SELECT * FROM _audit").fetchall()
self.assertEqual(len(audit_rows), 1)
r = audit_rows[0]
#table and op
self.assertEqual(r[1:3], (u'tab', u'INSERT'))
#no previous, new is what we inserted
self.assertEqual(r[3:],
(None,
str([['c1', 'audit'],
['c2', 'this']]),
))
def test_update(self):
self.conn.execute("INSERT INTO tab VALUES('audit', 'this')")
self.assertEqual(
self.conn.execute("UPDATE tab SET c2='everything' WHERE"
" c2='this'").rowcount,
1)
audit_rows = self.conn.execute("SELECT * FROM _audit").fetchall()
self.assertEqual(len(audit_rows), 2)
r = audit_rows[1]
self.assertEqual(r[1:3], (u'tab', u'UPDATE'))
self.assertEqual(r[3:],
(str([['c1', 'audit'],
['c2', 'this']]),
str([['c1', 'audit'],
['c2', 'everything']]),
))
def test_delete(self):
self.conn.execute("INSERT INTO tab VALUES('audit', 'this')")
self.assertEqual(
self.conn.execute("DELETE FROM tab WHERE c1='audit'").rowcount,
1)
audit_rows = self.conn.execute("SELECT * FROM _audit").fetchall()
self.assertEqual(len(audit_rows), 2)
r = audit_rows[1]
self.assertEqual(r[1:3], (u'tab', u'DELETE'))
self.assertEqual(r[3:],
(str([['c1', 'audit'],
['c2', 'this']]),
None,
))
def test_update_null(self):
self.conn.execute("INSERT INTO tab VALUES('audit', NULL)")
self.assertEqual(
self.conn.execute("UPDATE tab SET c2='everything' WHERE"
" c2 is NULL").rowcount,
1)
audit_rows = self.conn.execute("SELECT * FROM _audit").fetchall()
self.assertEqual(len(audit_rows), 2)
r = audit_rows[1]
self.assertEqual(r[1:3], (u'tab', u'UPDATE'))
self.assertEqual(r[3:],
(str([['c1', 'audit'],
['c2', None]]),
str([['c1', 'audit'],
['c2', 'everything']]),
))
def test_detach(self):
audit.detach_log(self.conn)
self.conn.execute("INSERT INTO tab VALUES('no', 'audit')")
with self.assertRaises(sqlite3.OperationalError):
#no _audit table
self.conn.execute("SELECT * FROM _audit").fetchall()
if __name__ == '__main__':
unittest.main()