python - 使用 Python 进行 BigQuery 单元测试
问题描述
我正在尝试使用 Mock 对象测试 BigQuery 类来表示表。BigQueryRequest
我班级的实例必须提供BigQuery table uri
. 我可以直接从 Python 创建一个模拟 BigQuery 表吗?怎么可能?
class BigQueryRequest:
"""BigQueryRequest
Contains a BigQuery request with its parameter.
Receive a table uri ($project_id.$dataset.$table) to run query
Args:
uri (str): BigQuery table uri
Properties:
BigQueryRequest.project: return the project running BigQuery
BigQueryRequest.dataset: return the dataset
BigQueryRequest.table: return the table to query
BigQueryRequest.destination_project: same as project but for destination project
BigQueryRequest.destination_dataset: same as project but for destination dataset
BigQueryRequest.destination_table: same as project but for destination table
Methods:
from_uri(): (@classmethod) parse a BigQuery uri to its project, dataset, table
destination(): return a uri of the BigQuery request destination table
query(): run the given BigQuery query
Private methods:
__set_destination(): generate a destination uri following the nomenclature or reuse entry uri
"""
def __init__(self, uri="", step="", params={}):
self.project, self.dataset, self.table = self.from_uri(uri)
self.step = step
self.params = self.set_params(params)
self.overwrite = False
(
self.destination_project,
self.destination_dataset,
self.destination_table,
) = self.__set_destination()
解决方案
您必须自己做,Google Cloud 不为 GCP 产品或服务提供官方模拟库。
您也可以尝试https://github.com/Khan/tinyquery作为替代方案。