Py学习  »  Python

python单元测试google bigquery

Hafizur Rahman • 5 年前 • 1389 次点击  

我在单元测试以下代码块时遇到问题:

from google.cloud import bigquery
from google.oauth2 import service_account

def run_query(query, gcp_ser_acc):
    credentials = 
    service_account.Credentials.from_service_account_info(gcp_ser_acc)
    client = bigquery.Client(gcp_ser_acc['project_id'], credentials)
    query_job = client.query(query)
    results = query_job.result()
    return results

我是新来的嘲笑,我尝试了以下测试:

def test_run_a_query_with_real_key(self):
    gcp_ser_acc = {
                'project_id': 'my_project_id',
                'private_key': 'my_private_key',
                'token_uri': 'my_token_uri',
                'client_email': 'my_client_email'
                }
    with mock.patch('service_account.Credentials', call_args=gcp_ser_acc, return_value={}):
        with mock.patch('bigquery.Client', call_args=(gcp_ser_acc['project_id'], {}), return_value={}):
            run_query('SELECT 1+1 as col', gcp_ser_acc)
            assert service_account.Credentials.called
            assert bigquery.Client.called

有人能模仿谷歌的东西,写一个单元测试吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43695
 
1389 次点击  
文章 [ 1 ]  |  最新文章 5 年前