Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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进行BigQuery单元测试_Python_Google Cloud Platform - Fatal编程技术网

使用Python进行BigQuery单元测试

使用Python进行BigQuery单元测试,python,google-cloud-platform,Python,Google Cloud Platform,我试图用模拟对象来测试BigQuery类,以表示表。myBigQueryRequest类的实例必须提供BigQuery表uri。我是否可以直接从Python创建一个模拟BigQuery表?怎么可能呢 class BigQueryRequest: """BigQueryRequest Contains a BigQuery request with its parameter. Receive a table uri ($project_id.

我试图用模拟对象来测试BigQuery类,以表示表。my
BigQueryRequest
类的实例必须提供
BigQuery表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()

你必须自己做,谷歌云没有为GCP产品或服务提供官方的模拟库


您也可以尝试另一种方法。

如果您计划测试SQL并根据输入断言结果,我建议。该框架允许您在Python中与BigQuery交互,并使测试更加可靠

您有3种向其中注入数据的方法:

  • 创建能够隔离名称的数据集和表,从而拥有自己的命名空间
  • 依赖临时表,在临时表中,数据是用数据文字插入的
  • 数据文字合并到查询中
希望这有帮助