Skip to main content

Test BigQuery query using BigQuery

Project description

BigQueryのクエリをテストするためのツール

Run pytest

BigQueryへのクエリロジックのテストができます

Basic Usage

Simple

from bqqtest import QueryTest
from google.cloud import bigquery

# expected
expected_schema = [
    {"name": "name", "type": "STRING", "mode": "NULLABLE"},
    {"name": "value", "type": "INT64", "mode": "NULLABLE"},
]
expected_datum = [["abc", 100], ["bbb", 333]]
expected = {"schema": expected_schema, "datum": expected_datum}

# actual
target_schema = [
    {"name": "name", "type": "STRING", "mode": "NULLABLE"},
    {"name": "value", "type": "INT64", "mode": "NULLABLE"},
]
target_datum = [["abc", 100], ["bbb", 333]]
tables = {"test.target_table": {"schema": target_schema, "datum": target_datum}}
eval_query = {"query": "SELECT * FROM test.target_table", "params": []}

qt = QueryTest(bigquery.Client(), expected, tables, eval_query)
success, diff = qt.run()
success  # True

Group By

from bqqtest import QueryTest
from google.cloud import bigquery

# expected
expected_schema = [
    {"name": "item", "type": "STRING", "mode": "NULLABLE"},
    {"name": "total", "type": "INT64", "mode": "NULLABLE"},
]
expected_datum = [["abc", 300], ["bbb", 333]]
expected = {"schema": expected_schema, "datum": expected_datum}

# actual
target_schema = [
    {"name": "item", "type": "STRING", "mode": "NULLABLE"},
    {"name": "value", "type": "INT64", "mode": "NULLABLE"},
]
target_datum = [["abc", 100], ["bbb", 333], ["abc", 200]]
tables = {"test.target_table": {"schema": target_schema, "datum": target_datum}}
eval_query = {
    "query": "SELECT item, SUM(value) AS total FROM test.target_table GROUP BY item",
    "params": [],
}

qt = QueryTest(bigquery.Client(), expected, tables, eval_query)
success, diff = qt.run()
success  # True

Multi Table

from bqqtest import QueryTest
from google.cloud import bigquery


# expected
expected_schema = [
    {"name": "item", "type": "STRING", "mode": "NULLABLE"},
    {"name": "value", "type": "INT64", "mode": "NULLABLE"},
]
expected_datum = [["abc", 100], ["bbb", 333], ["xxxx", 888], ["zzzz", 999]]
expected = {"schema": expected_schema, "datum": expected_datum}

# actual
target_schema = [
    {"name": "item", "type": "STRING", "mode": "NULLABLE"},
    {"name": "value", "type": "INT64", "mode": "NULLABLE"},
]
target_datum1 = [["abc", 100], ["bbb", 333]]
target_datum2 = [["xxxx", 888], ["zzzz", 999]]
tables = {
    "test.table1": {"schema": target_schema, "datum": target_datum1},
    "test.table2": {"schema": target_schema, "datum": target_datum2},
}
eval_query = {
    "query": "SELECT * FROM `test.table1` UNION ALL SELECT * FROM `test.table2`",
    "params": [],
}

qt = QueryTest(bigquery.Client(), expected, tables, eval_query)
success, diff = qt.run()
success  # True

特徴

see also https://qiita.com/tamanobi/items/9434ca0dbd5f0d3018d9

  • WITH を利用して、 BigQuery に保存されないテストデータを一時的に生成します。
    • BigQuery は保存されているデータ走査した量とAPIリクエスト数で課金されるため、費用抑えてユニットテストができます。
    • 料金の詳細は、 BigQuery の公式ドキュメントを参照してください
  • テストをするために、クエリを書き直す必要はありません
    • ライブラリ内部では、対象テーブルの Identifier を書き換えてテーブルを差し替えます

注意

BigQuery へ直接クエリを発行します。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bqqtest-0.6.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

bqqtest-0.6.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file bqqtest-0.6.0.tar.gz.

File metadata

  • Download URL: bqqtest-0.6.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.7.6 Linux/5.0.0-1032-azure

File hashes

Hashes for bqqtest-0.6.0.tar.gz
Algorithm Hash digest
SHA256 7b17d72eea8d0ced7c2d33128ea8ed77857135b85a4b8ec0e525941cc6b95b12
MD5 69cf0ecebecc9c16439e07f98c27d209
BLAKE2b-256 aaf239e35e3588e14503e6ce326531b712cc153946e8e0618532330f44c95c2d

See more details on using hashes here.

File details

Details for the file bqqtest-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: bqqtest-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.7.6 Linux/5.0.0-1032-azure

File hashes

Hashes for bqqtest-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33c1296cc539ca84daeaee4d30e0cca98457e6e0481c123af70893232db115be
MD5 b6401d68be73ba5a9ca954b2cb90e9c8
BLAKE2b-256 d320f8c721e664d03afc342c39b7abe7522be2527180a4cbdec05e8dd25dcf33

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page