Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何模拟python-oracle连接_Python_Python 3.x_Magicmock - Fatal编程技术网

如何模拟python-oracle连接

如何模拟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

我来自Java世界,我正试着用python模拟来表达我的想法。我有以下类,我试图模拟oracle方法调用以返回一个值。 这里比较棘手的一点是,mock正在创建更多的对象,我希望从中得到结果

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__()