Skip to main content

Simple Typed Python Database Client based PEP 249

Project description

Turu: Simple Database Client for Typed Python

docs test pypi package

logo


Documentation: https://ya7010.github.io/turu-py/

Source Code: https://github.com/ya7010/turu-py


Installation

pip install "turu[snowflake]"

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

  • :rocket: Simple - Turu is a simple database api wrapper of PEP 249.
  • :bulb: Type Hint - Full support for type hints.
  • :zap: Async/Await - Async/Await supports.
  • :test_tube: 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

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

turu-0.13.3-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: turu-0.13.3.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.15 Linux/6.17.0-1010-azure

File hashes

Hashes for turu-0.13.3.tar.gz
Algorithm Hash digest
SHA256 685a000b201af4df3c649f315b2901464eddf03ec6e50166916c376eae5f3c6b
MD5 534b48291c1a44e3928618c54b2a9d72
BLAKE2b-256 620b63ef21d92bf1a4ed8e405b3850e31b87c80dbcea7dd9a44d76cc5dac7232

See more details on using hashes here.

File details

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

File metadata

  • Download URL: turu-0.13.3-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.10.15 Linux/6.17.0-1010-azure

File hashes

Hashes for turu-0.13.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b9a1f26ac9397e6c299d56a5c92e0b57fb2971b833425b0fd0438eae8c7ab09
MD5 7fb45b79a07b8a148eb18f436bcf1b57
BLAKE2b-256 06a71b5d17070003292bc3d33372bccb7a75de4ae7b9b1789ab60da4df5d6312

See more details on using hashes here.

Supported by

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