|
| 1 | +# Test module converted from docs example - code-block 12 |
| 2 | +"""Minimal smoke test for drivers_and_querying example 12.""" |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from sqlspec.adapters.bigquery.driver import BigQueryDriver |
| 7 | + |
| 8 | +__all__ = ("test_example_12_bigquery_config",) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skip(reason="Requires BigQuery emulator setup with more complex configuration.") |
| 12 | +def test_example_12_bigquery_config(bigquery_service: BigQueryDriver) -> None: |
| 13 | + # start-example |
| 14 | + import datetime |
| 15 | + |
| 16 | + from google.api_core.client_options import ClientOptions |
| 17 | + from google.auth.credentials import AnonymousCredentials |
| 18 | + |
| 19 | + from sqlspec import SQLSpec |
| 20 | + from sqlspec.adapters.bigquery.config import BigQueryConfig |
| 21 | + |
| 22 | + config = BigQueryConfig( |
| 23 | + connection_config={ |
| 24 | + "project": bigquery_service.project, |
| 25 | + "dataset_id": bigquery_service.dataset, |
| 26 | + "client_options": ClientOptions(api_endpoint=f"http://{bigquery_service.host}:{bigquery_service.port}"), |
| 27 | + "credentials": AnonymousCredentials(), # type: ignore[no-untyped-call] |
| 28 | + } |
| 29 | + ) |
| 30 | + spec = SQLSpec() |
| 31 | + with spec.provide_session(config) as bigquery_session: |
| 32 | + bigquery_session.execute("SELECT 1 AS value") |
| 33 | + |
| 34 | + # Create the test table |
| 35 | + |
| 36 | + create_table_query = """ |
| 37 | + CREATE or replace TABLE events ( |
| 38 | + timestamp TIMESTAMP, |
| 39 | + event_type STRING |
| 40 | + ) |
| 41 | + """ |
| 42 | + bigquery_session.execute_script(create_table_query) |
| 43 | + |
| 44 | + print("Executing test query...") |
| 45 | + bigquery_session.execute( |
| 46 | + """ |
| 47 | + SELECT DATE(timestamp) as date, |
| 48 | + COUNT(*) as events |
| 49 | + FROM events |
| 50 | + WHERE timestamp >= @start_date |
| 51 | + GROUP BY date |
| 52 | + """, |
| 53 | + start_date=datetime.date(2025, 1, 1), |
| 54 | + ) |
| 55 | + # end-example |
0 commit comments