Turu: Simple Database Client for Typed Python
Documentation: https://ya7010.github.io/turu-py/
Source Code: https://github.com/ya7010/turu-py
Installation
pip install "turu[snowflake]"
Development
Install uv and sync every package in the workspace:
uv sync --all-packages --all-extras
Run the checks for all packages with:
make ci
Why Turu?
SQL is a powerful language, but it has many dialects, and Cloud Native Databases are especially difficult to test automatically in a local environment.
Turu was developed as a simple tool to assist local development. It provides a simple interface according to PEP 249 – Python Database API Specification v2.0 and allows for easy recording of query results and injection mock data.
Features
- 🚀 Simple - Turu is a simple database api wrapper of PEP 249.
- 💡 Type Hint - Full support for type hints.
- ⚡ Async/Await - Async/Await supports.
- 🧪 Recoed and Mock - Record and mock database queries for testing.
Supprted Database
| Database | Sync Support | Async Support | Installation |
|---|---|---|---|
| SQLite3 | Yes | Yes | pip install "turu[sqlite3]" |
| MySQL | Yes | Yes | pip install "turu[mysql]" |
| PostgreSQL | Yes | Yes | pip install "turu[postgres]" |
| Snowflake | Yes | Yes | pip install "turu[snowflake]" |
| BigQuery | Yes | No | pip install "turu[bigquery]" |
Usage
Basic Usage
from pydantic import BaseModel
class Row(BaseModel):
id: int
name: str
connection = turu.sqlite3.connect("test.db")
with connection.cursor() as cursor:
assert cursor.execute_map(Row, "select 1, 'a'").fetchone() == Row(id=1, name="a")
Testing
import turu.sqlite3
from pydantic import BaseModel
class Row(BaseModel):
id: int
name: str
expected1 = [Row(id=1, name="a"), Row(id=2, name="b")]
expected2 = [Row(id=3, name="c"), Row(id=4, name="d")]
expected3 = [Row(id=5, name="e"), Row(id=6, name="f")]
connection = turu.sqlite3.MockConnection()
(
connection.chain()
.inject_response(Row, expected1)
.inject_response(Row, expected2)
.inject_response(Row, expected3)
)
for expected in [expected1, expected2, expected3]:
with connection.cursor() as cursor:
assert cursor.execute_map(Row, "select 1, 'a'").fetchall() == expected
Recording and Testing
Your Production Code
import os
import turu.sqlite3
from turu.core.record import record_to_csv
from your_package.data import RECORD_DIR
from your_package.schema import Row
def do_something(connection: turu.sqlite3.Connection):
with record_to_csv(
RECORD_DIR / "test.csv",
connection.curosr(),
enable=os.environ.get("ENABLE_RECORDING"),
limit=100,
) as cursor:
... # Your logic
Your Test Code
import turu.sqlite3
from your_package.data import RECORD_DIR
from your_package.schema import Row
def test_do_something(connection: turu.sqlite3.MockConnection):
connection.inject_response_from_csv(Row, RECORD_DIR / "test.csv")
assert do_something(connection) is None
Metadata
Release files for turu 0.13.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| turu-0.13.4.tar.gz | 278.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| turu-0.13.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 281.5 kB
Release files / turu-0.13.4.tar.gz
| Download URL | turu-0.13.4.tar.gz |
|---|---|
| Size | 278.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c03855b563a1a62581ab45312ff1e1fdaf4d567e08fa3a22d01a192293c5b46
|
|
BLAKE2b-256 checksum How to use checksums |
f1fc4c98e1171317a13ba882d6d674be842447488b1deb7807d5e4927f2240aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / turu-0.13.4-py3-none-any.whl
| Download URL | turu-0.13.4-py3-none-any.whl |
|---|---|
| Size | 2.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e6fff449da07a36180f911aa860d791f95a69eedb76121317ba4f68c3de043bd
|
|
BLAKE2b-256 checksum How to use checksums |
4e2a5efa82ebc67e8e592c7436ce67a75910720f91d62b92da8d9fa76398e780
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|