Skip to content

Commit a2f008c

Browse files
committed
tmp
1 parent b160dbf commit a2f008c

File tree

3 files changed

+61
-79
lines changed

3 files changed

+61
-79
lines changed

docs/examples/usage/conftest.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,6 @@
1-
from collections.abc import Generator
2-
from typing import Any
3-
4-
import pytest
5-
from google.api_core.client_options import ClientOptions
6-
from google.auth.credentials import AnonymousCredentials
7-
from pytest_databases.docker.bigquery import BigQueryService
8-
9-
from sqlspec.adapters.bigquery.config import BigQueryConfig
10-
from sqlspec.adapters.bigquery.driver import BigQueryDriver
11-
121
pytest_plugins = [
132
"pytest_databases.docker.postgres",
143
"pytest_databases.docker.mysql",
154
"pytest_databases.docker.oracle",
165
"pytest_databases.docker.bigquery",
176
]
18-
19-
20-
@pytest.fixture
21-
def table_schema_prefix(bigquery_service: "BigQueryService") -> str:
22-
"""Create a table schema prefix."""
23-
return f"`{bigquery_service.project}`.`{bigquery_service.dataset}`"
24-
25-
26-
@pytest.fixture
27-
def bigquery_config(bigquery_service: "BigQueryService", table_schema_prefix: str) -> BigQueryConfig:
28-
"""Create a BigQuery config object."""
29-
return BigQueryConfig(
30-
connection_config={
31-
"project": bigquery_service.project,
32-
"dataset_id": table_schema_prefix,
33-
"client_options": ClientOptions(api_endpoint=f"http://{bigquery_service.host}:{bigquery_service.port}"),
34-
"credentials": AnonymousCredentials(), # type: ignore[no-untyped-call]
35-
}
36-
)
37-
38-
39-
@pytest.fixture
40-
def bigquery_session(bigquery_config: BigQueryConfig) -> Generator[BigQueryDriver, Any, None]:
41-
"""Create a BigQuery sync session."""
42-
43-
with bigquery_config.provide_session() as session:
44-
yield session
45-
46-
@pytest.fixture
47-
async def postgres_session_async(postgres_service: "PostgresService") -> Generator[Any, Any, None]:
48-
"""Create a Postgres async session."""
49-
from sqlspec.adapters.asyncpg import AsyncpgConfig
50-
51-
config = AsyncpgConfig(
52-
pool_config={
53-
"dsn": f"postgresql://{postgres_service.user}:{postgres_service.password}@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}",
54-
"min_size": 1,
55-
"max_size": 5,
56-
}
57-
)
58-
async with config.provide_session() as session:
59-
yield session
Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
# Test module converted from docs example - code-block 12
22
"""Minimal smoke test for drivers_and_querying example 12."""
33

4-
from sqlspec.adapters.bigquery import BigQueryDriver
4+
from sqlspec.adapters.bigquery.driver import BigQueryDriver
55

66

7-
def test_example_12_bigquery_config(bigquery_session: "BigQueryDriver") -> None:
7+
def test_example_12_bigquery_config(bigquery_service: BigQueryDriver) -> None:
88
import datetime
99

10-
from sqlspec import SQLSpec
11-
12-
SQLSpec()
13-
14-
bigquery_session.execute("SELECT 1 AS value")
15-
16-
# Create the test table
10+
from google.api_core.client_options import ClientOptions
11+
from google.auth.credentials import AnonymousCredentials
1712

18-
create_table_query = """
19-
CREATE TABLE if not exists events (
20-
timestamp TIMESTAMP,
21-
event_type STRING
13+
from sqlspec import SQLSpec
14+
from sqlspec.adapters.bigquery.config import BigQueryConfig
15+
16+
config = BigQueryConfig(
17+
connection_config={
18+
"project": bigquery_service.project,
19+
"dataset_id": bigquery_service.dataset,
20+
"client_options": ClientOptions(api_endpoint=f"http://{bigquery_service.host}:{bigquery_service.port}"),
21+
"credentials": AnonymousCredentials(), # type: ignore[no-untyped-call]
22+
}
2223
)
23-
"""
24-
bigquery_session.execute_script(create_table_query)
24+
spec = SQLSpec()
25+
with spec.provide_session(config) as bigquery_session:
26+
bigquery_session.execute("SELECT 1 AS value")
27+
28+
# Create the test table
2529

26-
bigquery_session.execute(
30+
create_table_query = """
31+
CREATE or replace TABLE events (
32+
timestamp TIMESTAMP,
33+
event_type STRING
34+
)
2735
"""
28-
SELECT DATE(timestamp) as date,
29-
COUNT(*) as events
30-
FROM events
31-
WHERE timestamp >= @start_date
32-
GROUP BY date
33-
""",
34-
start_date=datetime.date(2025, 1, 1),
35-
)
36+
bigquery_session.execute_script(create_table_query)
37+
38+
print("Executing test query...")
39+
bigquery_session.execute(
40+
"""
41+
SELECT DATE(timestamp) as date,
42+
COUNT(*) as events
43+
FROM events
44+
WHERE timestamp >= @start_date
45+
GROUP BY date
46+
""",
47+
start_date=datetime.date(2025, 1, 1),
48+
)
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
# Test module converted from docs example - code-block 13
22
"""Minimal smoke test for drivers_and_querying example 13."""
33

4+
from sqlspec import SQLSpec
5+
from sqlspec.adapters.sqlite import SqliteConfig
6+
47

58
def test_example_13_placeholder() -> None:
6-
# Examples are documentation snippets; ensure module importable
7-
assert True
9+
spec = SQLSpec()
10+
config = SqliteConfig(pool_config={"database": ":memory:", "timeout": 5.0, "check_same_thread": False})
11+
with spec.provide_session(config) as session:
12+
create_table_query = (
13+
"""create table if not exists users (id default int primary key, name varchar(128), email text)"""
14+
)
15+
16+
session.execute(create_table_query)
17+
# Examples are documentation snippets; ensure module importable
18+
result = session.execute("SELECT * FROM users WHERE id = ?", 1)
19+
20+
# INSERT query
21+
result = session.execute("INSERT INTO users (name, email) VALUES (?, ?)", "Alice", "alice@example.com")
22+
23+
# UPDATE query
24+
result = session.execute("UPDATE users SET email = ? WHERE id = ?", "newemail@example.com", 1)
25+
print(f"Updated {result.rows_affected} rows")
26+
27+
# DELETE query
28+
result = session.execute("DELETE FROM users WHERE id = ?", 1)
29+
config.close_pool()

0 commit comments

Comments
 (0)