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 153 154 155 156 157 158 159 160
|
"""This module serves as a mock object for the DB-API 2 module"""
import sys
import pytest
__all__ = ['dbapi']
threadsafety = 2
@pytest.fixture
def dbapi():
"""Get mock DB API 2 module."""
mock_db = sys.modules[__name__]
mock_db.threadsafety = 2
return mock_db
class Error(Exception):
pass
class DatabaseError(Error):
pass
class OperationalError(DatabaseError):
pass
class InterfaceError(DatabaseError):
pass
class InternalError(DatabaseError):
pass
class ProgrammingError(DatabaseError):
pass
def connect(database=None, user=None):
return Connection(database, user)
class Connection:
has_ping = False
num_pings = 0
def __init__(self, database=None, user=None):
self.database = database
self.user = user
self.valid = False
if database == 'error':
raise OperationalError
self.open_cursors = 0
self.num_uses = 0
self.num_queries = 0
self.num_pings = 0
self.session = []
self.valid = True
def close(self):
if not self.valid:
raise InternalError
self.open_cursors = 0
self.num_uses = 0
self.num_queries = 0
self.session = []
self.valid = False
def commit(self):
if not self.valid:
raise InternalError
self.session.append('commit')
def rollback(self):
if not self.valid:
raise InternalError
self.session.append('rollback')
def ping(self):
cls = self.__class__
cls.num_pings += 1
if not cls.has_ping:
raise AttributeError
if not self.valid:
raise OperationalError
def cursor(self, name=None):
if not self.valid:
raise InternalError
return Cursor(self, name)
class Cursor:
def __init__(self, con, name=None):
self.con = con
self.valid = False
if name == 'error':
raise OperationalError
self.result = None
self.inputsizes = []
self.outputsizes = {}
con.open_cursors += 1
self.valid = True
def close(self):
if not self.valid:
raise InternalError
self.con.open_cursors -= 1
self.valid = False
def execute(self, operation):
if not self.valid or not self.con.valid:
raise InternalError
self.con.num_uses += 1
if operation.startswith('select '):
self.con.num_queries += 1
self.result = operation[7:]
elif operation.startswith('set '):
self.con.session.append(operation[4:])
self.result = None
elif operation == 'get sizes':
self.result = (self.inputsizes, self.outputsizes)
self.inputsizes = []
self.outputsizes = {}
else:
raise ProgrammingError
def fetchone(self):
if not self.valid:
raise InternalError
result = self.result
self.result = None
return result
def callproc(self, procname):
if not self.valid or not self.con.valid or not procname:
raise InternalError
self.con.num_uses += 1
def setinputsizes(self, sizes):
if not self.valid:
raise InternalError
self.inputsizes = sizes
def setoutputsize(self, size, column=None):
if not self.valid:
raise InternalError
self.outputsizes[column] = size
def __del__(self):
if self.valid:
self.close()
|