久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何模擬 psycopg2 光標(biāo)對(duì)象?

How to mock psycopg2 cursor object?(如何模擬 psycopg2 光標(biāo)對(duì)象?)
本文介紹了如何模擬 psycopg2 光標(biāo)對(duì)象?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 Python2 中有這個(gè)代碼段:

I have this code segment in Python2:

def super_cool_method():
    con = psycopg2.connect(**connection_stuff)
    cur = con.cursor(cursor_factory=DictCursor)
    cur.execute("Super duper SQL query")
    rows = cur.fetchall()

    for row in rows:
        # do some data manipulation on row
    return rows

我想為其編寫一些單元測(cè)試.我想知道如何使用 mock.patch 來修補(bǔ)游標(biāo)和連接變量,以便它們返回一組假數(shù)據(jù)?我已經(jīng)為我的單元測(cè)試嘗試了以下代碼段,但無(wú)濟(jì)于事:

that I'd like to write some unittests for. I'm wondering how to use mock.patch in order to patch out the cursor and connection variables so that they return a fake set of data? I've tried the following segment of code for my unittests but to no avail:

@mock.patch("psycopg2.connect")
@mock.patch("psycopg2.extensions.cursor.fetchall")
def test_super_awesome_stuff(self, a, b):
    testing = super_cool_method()

但我似乎收到以下錯(cuò)誤:

But I seem to get the following error:

TypeError: can't set attributes of built-in/extension type 'psycopg2.extensions.cursor'

推薦答案

由于游標(biāo)是con.cursor的返回值,所以只需要mock連接,然后正確配置即可.例如,

Since the cursor is the return value of con.cursor, you only need to mock the connection, then configure it properly. For example,

query_result = [("field1a", "field2a"), ("field1b", "field2b")]
with mock.patch('psycopg2.connect') as mock_connect:
    mock_connect.cursor.return_value.fetchall.return_value = query_result
    super_cool_method()

這篇關(guān)于如何模擬 psycopg2 光標(biāo)對(duì)象?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 天堂网色 | 欧美日韩中文在线观看 | 一级美国黄色片 | 免费在线观看成年人视频 | 91精品国产综合久久久久久首页 | 亚洲欧美一区二区三区国产精品 | 美女131mm久久爽爽免费 | 成年无码av片在线 | 亚洲一区中文字幕 | 精品国产伦一区二区三区观看方式 | 欧美性大战久久久久久久蜜臀 | 欧美黑人巨大videos精品 | 国产又爽又黄的视频 | 免费黄色片视频 | 国产一区二区观看 | 国产在线97 | 99re在线视频观看 | 播放一级黄色片 | 久久伊人免费视频 | 免费成人毛片 | 亚洲成人综合在线 | 视频一区二区在线观看 | 麻豆久久久久 | 亚洲精品日韩在线观看 | 国产一级特黄真人毛片 | 国产资源网 | 日韩av电影院| 日本视频中文字幕 | 欧美国产日韩精品 | 黄色网址在线免费观看 | 日韩精品久久久久久 | 成人欧美一区二区三区 | 成人a视频片观看免费 | 狠狠涩 | 亚洲免费观看视频网站 | 另类视频在线 | 久热精品在线观看视频 | av黄色国产 | 四虎免费视频 | 国产精品一级 | 草b视频 |