Skip to main content

Simple Database API for Typed Python based PEP 249

Project description

Turu: Simple Database API for Typed Python

test pypi package

Installation

pip install turu[snowflake]

Why Turu?

SQL is a powerful language, but it has many dialects and is especially difficult to automatically test cloud-based SQL databases 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.

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


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.6.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

turu-0.4.6-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

File details

Details for the file turu-0.4.6.tar.gz.

File metadata

  • Download URL: turu-0.4.6.tar.gz
  • Upload date:
  • Size: 2.2 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

Hashes for turu-0.4.6.tar.gz
Algorithm Hash digest
SHA256 c88a56cf94c7b25568ca1eebb983c2e324d04f72dc7ee0c730dbebae6a66b83f
MD5 6d77cc369938b0ab0a96dd43de36b6cd
BLAKE2b-256 27cd153654da4294e711f357a3df6f9c20ed33f852bf7d11b8ced3f8c63fe28a

See more details on using hashes here.

Provenance

File details

Details for the file turu-0.4.6-py3-none-any.whl.

File metadata

  • Download URL: turu-0.4.6-py3-none-any.whl
  • Upload date:
  • Size: 2.1 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

Hashes for turu-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 cbb3ab9e540e1580b2dfdf00e66104d8f0177e993b1ab2388c3915711b3f8679
MD5 1ebb064c9562c30fc0d1b008993893c9
BLAKE2b-256 a5fccab56e6855ac1448eefed425387ca0ad1fb67b9bf5365205c176c1e64b1b

See more details on using hashes here.

Provenance

Supported by

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