如何模拟python-oracle连接
我来自Java世界,我正试着用python模拟来表达我的想法。我有以下类,我试图模拟oracle方法调用以返回一个值。 这里比较棘手的一点是,mock正在创建更多的对象,我希望从中得到结果如何模拟python-oracle连接,python,python-3.x,magicmock,Python,Python 3.x,Magicmock,我来自Java世界,我正试着用python模拟来表达我的想法。我有以下类,我试图模拟oracle方法调用以返回一个值。 这里比较棘手的一点是,mock正在创建更多的对象,我希望从中得到结果 class Dao(object): def __init__(self, username, password, dsn): self.dsn = dsn self.username = username self.password = passwo
class Dao(object):
def __init__(self, username, password, dsn):
self.dsn = dsn
self.username = username
self.password = password
def __enter__(self):
self.cxn = cx_Oracle.connect(self.username, self.password, self.dsn)
self.cur = self.cxn.cursor()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.cur.close()
self.cxn.close()
def get_statistics(self):
self.cur.execute('select * from one.query')
self.cur.rowfactory = lambda *args: dict(zip([d[0] for d in self.cur.description], args))
return self.cur.fetchall()
到目前为止,我已经有了测试用例
class DaoTest(TestCase):
@patch('cx_Oracle.connect')
def test_request_logger_stats(self, oracle):
dao = Dao('uid','pass','dsn')
# dao.__enter__()
with patch('Connection') as cursor:
dao.cur = cursor
returnVal = dao.get_statistics()
self.assertTrue(returnVal == None)
dao.__exit__()