简体   繁体   English

如何在 BigQuery javascript UDF 中断言

[英]How to assert in BigQuery javascript UDF

I would like to assert invariants in my Javascript UDF in BigQuery.我想在 BigQuery 的 Javascript UDF 中断言不变量。 Specifically, the query enclosing the UDF should fail gracefully if the invariant is violated.具体来说,如果不变量被违反,包含 UDF 的查询应该优雅地失败。 Is there a way to call an assert function to achieve this behavior?有没有办法调用断言 function 来实现这种行为?

You can use a test in Python with PyTest that uses BigQuery Python client to execute a sql query with your udf , then add your expected assertions:您可以在PythonPyTest中使用测试,该测试使用BigQuery Python 客户端使用您的udf执行sql查询,然后添加您预期的断言:

from google.cloud import bigquery

def test_query_udf():
    client = bigquery.Client()
    query_job = client.query(
        """
        SELECT
          `{{project_id}}.{{dataset}}`.my_udf('my_param')
        FROM `{{project_id}}.{{dataset}}.table`
        """
    )

    results = query_job.result()  # Waits for job to complete.
    
    # Add your assertions on results

For this kind of integration tests, you have to launch it in an existing infrastructure.对于这种集成测试,您必须在现有基础架构中启动它。

Sometimes for integrations developers prefer using a short lived infra but it's not easy to manage.有时对于集成,开发人员更喜欢使用短暂的基础设施,但它不容易管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM