模拟psycopg2

时间:2020-08-08 09:36:56

标签: pytest pytest-mock

我在模块中具有以下功能,我们将其称为importer.py

def audience_mapping():
    with psycopg2.connect(dbname=db, user=user, password=pass,
                            host=host) as conn:
        with conn.cursor() as cur:

            cur.execute(QUERY)

            rows = cur.fetchall()

            from_db = {}
            for row in rows:
                from_db[row[0]] = row[4]

            return from_db

和test_importer.py中的测试

import pytest

@mock_s3
def test_file_written_when_article_present(mocker, mock_return_true_published_today_check):
    ...
    ...
    mock_db_result = [("col1", col2)]
    psycopg2 = mocker.patch('functions.tp_article_importer.psycopg2')
    psycopg2.connect.return_value.cursor.return_value.fetchall.return_value = mock_db_result
    ...
    ...

这个模拟语句似乎对cur.fetchall()的值没有影响。

0 个答案:

没有答案