Skip to content

Instantly share code, notes, and snippets.

View madeinoz67's full-sized avatar

Stephen Eaton madeinoz67

View GitHub Profile
@madeinoz67
madeinoz67 / conftest.py
Last active June 6, 2021 11:58
conftest.py for pytest when using async sqlalchemy v1.4 database testing using sqlite with aiosqlite driver. Alembic for database setup and tear-downs. this is what I finally got working when testing services for fastapi
import os
import warnings
from contextvars import ContextVar
from typing import AsyncGenerator
from unittest import mock
import alembic
import pytest
from alembic.config import Config
@madeinoz67
madeinoz67 / tz.py
Created June 4, 2021 12:22
helper module to deal with datetime and timezones in sqlalchemy
"""
This is a helper module to deal with datetime and timezones.
For convenience datetime related modules (e.g. dateutil.parser, pytz, etc) are
imported here so they can be accessed using this module.
Usage:
>>> from fastapi_sqlalchemy import tz
>>> dt1 = tz.utcnow()
>>> dt2 = tz.utcdatetime(2015, 1, 2, 1, 2, 3)
>>> dt2 = dt2.astimezone(tz.LOCAL)
>>> dt2 = dt2.astimezone(tz.UTC)
@madeinoz67
madeinoz67 / db_session.py
Last active June 4, 2021 06:16
FastApi async sqlalchemy sessions
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine # noqa
engine = create_async_engine("")
SessionLocal = sessionmaker(
class_=AsyncSession,
autocommit=False,
autoflush=False,
bind=engine