This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fakedb.py
152 lines (112 loc) · 3.33 KB
/
fakedb.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
A fake DB-API 2 driver.
"""
# DB names used to trigger certain behaviours.
INVALID_DB = 'invalid-db'
INVALID_CURSOR = 'invalid-cursor'
apilevel = '2.0'
threadsafety = 2
paramstyle = 'qmark'
def connect(database):
return Connection(database)
class Connection(object):
"""
A fake connection.
"""
def __init__(self, database):
super(Connection, self).__init__()
self.database = database
self.session = []
self.cursors = set()
self.executed = 0
if database == INVALID_DB:
self.valid = False
raise OperationalError()
self.valid = True
def close(self):
if not self.valid:
raise ProgrammingError("Cannot close a closed connection.")
self.valid = False
for cursor in cursors:
cursor.close()
self.session.append('close')
if self.database == INVALID_DB:
raise OperationalError()
def commit(self):
self.session.append('commit')
def rollback(self):
self.session.append('rollback')
def cursor(self):
self.session.append('cursor')
if not self.valid:
raise InterfaceError()
return Cursor(self)
class Cursor(object):
"""
A fake cursor.
"""
__slots__ = ['connection', 'valid', 'result', 'rowcount']
def __init__(self, connection):
self.connection = connection
self.result = None
if connection.database == INVALID_CURSOR:
self.valid = False
raise OperationalError()
connection.cursors.add(self)
self.valid = True
self.rowcount = -1
def close(self):
self.connection.session.append('cursor-close')
if not self.valid:
raise InterfaceError()
self.connection.cursors.remove(self)
self.valid = False
def execute(self, stmt, args=()):
if not self.valid or not self.connection.valid:
raise InterfaceError()
stmt = stmt.lstrip().lower()
# It's the ping!
if stmt == 'select 1':
return self
stmt_type, = stmt.split(' ', 1)
if stmt_type in ('select', 'update', 'insert', 'delete'):
self.result = None if args is () else args
self.connection.session.append(stmt_type)
self.connection.executed += 1
else:
self.result = None
raise ProgrammingError()
def callproc(self, procname, args=()):
if not self.valid or not self.connection.valid:
raise InterfaceError()
self.result = None if args is () else args
self.connection.session.append('proc:' + procname)
self.connection.executed += 1
def fetchone(self):
if not self.valid:
raise InterfaceError()
result = self.result
self.result = None
return result
def fetchall(self):
return ()
class Warning(StandardError):
pass
class Error(StandardError):
pass
class InterfaceError(Error):
pass
class DatabaseError(Error):
pass
class DataError(DatabaseError):
pass
class OperationalError(DatabaseError):
pass
class IntegrityError(DatabaseError):
pass
class InternalError(DatabaseError):
pass
class ProgrammingError(DatabaseError):
pass
class NotSupportedError(DatabaseError):
pass