Simple Database API for Typed Python based PEP 249
Project description
Turu: Simple Database API for Typed Python
Installation
pip install turu[snowflake]
Why Turu?
SQL is a powerful language, but it's not very type safe, and recently many new SQL database engines have been created. In particular, cloud-based SQL database is difficult to test automatically in a local environment.
Turu was developed to allow for the unified handling and development of many SQL databases.
A simple interface according to PEP 249 – Python Database API Specification v2.0 is provided to easily record query results and injection mock data.
Supprted Database
Database | Supported | Installation |
---|---|---|
SQLite3 | Yes | pip install turu[sqlite3] |
MySQL | No | - |
PostgreSQL | No | - |
Snowflake | Yes | pip install turu[snowflake] |
BigQuery | Yes | 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.execute_map(Row, "select 1, 'a'") as cursor:
assert cursor.fetchone() == Row(id=1, name="a")
Recording Usage
import turu.sqlite3
from turu.core.record import record_as_csv
from pydantic import BaseModel
class Row(BaseModel):
id: int
name: str
connection = turu.sqlite3.connect("test.db")
with record_as_csv("test.csv", connection.execute_map(Row, "select 1, 'a'")) as cursor:
assert cursor.fetchone() == Row(id=1, name="a")
Testing Usage
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.execute_map(Row, "select 1, 'a'") as cursor:
assert cursor.fetchall() == expected
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
turu-0.4.2.tar.gz
(2.3 kB
view details)
Built Distribution
turu-0.4.2-py3-none-any.whl
(2.2 kB
view details)
File details
Details for the file turu-0.4.2.tar.gz
.
File metadata
- Download URL: turu-0.4.2.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66ccb80cfb6d045235ab5377574dfed926a58eda3d85eb515abfd9120bea6353 |
|
MD5 | 3f1702d437fac765293294a638a55131 |
|
BLAKE2b-256 | 324298db14daf7a7042b9c3a393156fc919eb25e629f77bb9f1ad26314233d98 |
Provenance
File details
Details for the file turu-0.4.2-py3-none-any.whl
.
File metadata
- Download URL: turu-0.4.2-py3-none-any.whl
- Upload date:
- Size: 2.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb1496022f3bdff221c37ec110b59d5b4f39e91c7ea32a4d9cecf2067a6f6fba |
|
MD5 | 62a7739854c396d76c23c35a5b1e8f1c |
|
BLAKE2b-256 | 0b8692d4f4d246f23305c1ec48a5970a35311ebbdbd3fe0c94dd3035ff08bea3 |