Skip to main content

Effortlessly validate and test your Google BigQuery queries with the power of pandas DataFrames in Python.

Project description

BQuest Logo

BQuest

Effortlessly validate and test your Google BigQuery queries with the power of pandas DataFrames in Python.

We would like to thank Mike Czech who is the original inventor of bquest!

Warning

This library is a work in progress!

Breaking changes should be expected until a 1.0 release, so version pinning is recommended.

CI: Overall outcome CD: gh-pages documentation PyPI version Project status (alpha, beta, stable) PyPI downloads Project license Python version compatibility Documentation: Black

Overview

  • Use BQuest in combination with your favorite testing framework (e.g. pytest).

  • Create temporary test tables from JSON or pandas DataFrame.

  • Run BQ configurations and plain SQL queries on your test tables and check the result.

Installation

Via PyPi (standard):

pip install bquest

Via Github (most recent):

pip install git+https://github.com/ottogroup/bquest

BQuest also requires a dedicated BigQuery dataset for storing test tables, e.g.

resource "google_bigquery_dataset" "bquest" {
  dataset_id    = "bquest"
  friendly_name = "bquest"
  description   = "Source tables for bquest tests"
  location      = "EU"
  default_table_expiration_ms = 3600000
}

We recommend setting an expiration time for tables in the bquest dataset to assure removal of those test tables upon test execution.

Example

Given a pandas DataFrame

foo

weight

prediction_date

bar

23

20190301

my

42

20190301

and its table definition

from bquest.tables import BQTableDefinitionBuilder

table_def_builder = BQTableDefinitionBuilder(GOOGLE_PROJECT_ID, dataset="bquest", location="EU")
table_definition = table_def_builder.from_df("abc.feed_latest", df)

you can use the config file ./abc/config.py

{
    "query": """
        SELECT
            foo,
            PARSE_DATE('%Y%m%d', prediction_date)
        FROM
            `{source_table}`
        WHERE
            weight > {THRESHOLD}
    """,
    "start_date": "prediction_date",
    "end_date": "prediction_date",
    "source_tables": {"source_table": "abc.feed_latest"},
    "feature_table_name": "abc.myid",
}

and the runner

from bquest.runner import BQConfigFileRunner, BQConfigRunner

runner = BQConfigFileRunner(
    BQConfigRunner(bq_client, bq_executor_func),
    "config/bq_config",
)

result_df = runner.run_config(
    "20190301",
    "20190308",
    [table_definition],
    "abc/config.py",
    templating_vars={"THRESHOLD": "30"},
)

to assert the result table

assert result_df.shape == (1, 2)
assert result_df.iloc[0]["foo"] == "my"

Testing

For the actual testing bquest relies on an accessible BigQuery project which can be configured with the gcloud client. The corresponding GOOGLE_PROJECT_ID is extracted from this project and used with pandas-gbq to write temporary tables to the bquest dataset that has to be pre- configured before testing on that project.

For Github CI we have configured an identity provider in our testing project which allows only core members of this repository to access the testing projects’ resources.

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

bquest-0.5.7.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bquest-0.5.7-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file bquest-0.5.7.tar.gz.

File metadata

  • Download URL: bquest-0.5.7.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for bquest-0.5.7.tar.gz
Algorithm Hash digest
SHA256 dd2a1b07d5d2d8c5971c98484aa4dd0a860ce562248cae2868c2aed4de3b80ba
MD5 3254bc13b0beabd2110c83106d2589ed
BLAKE2b-256 62019c3191707ae2aead16184f75cd0d830a896db42bf88d8083c6350c0f5e6d

See more details on using hashes here.

File details

Details for the file bquest-0.5.7-py3-none-any.whl.

File metadata

  • Download URL: bquest-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for bquest-0.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 64352b9b7ee3543626f2b7d37030c3ae9e33935d821399ed9a29e0417653526c
MD5 b5a4de0697ffe9bd9f1326d6066d5e75
BLAKE2b-256 eb55ece47f5ef67c020cc25986ffb7afdb53e9e45f76d06cb9085e4a0642b915

See more details on using hashes here.

Supported by

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