Skip to content

Instantly share code, notes, and snippets.

@tribals
Last active August 13, 2020 11:29
Show Gist options
  • Save tribals/49668c4c332f03462eb15d172cc06f4a to your computer and use it in GitHub Desktop.
Save tribals/49668c4c332f03462eb15d172cc06f4a to your computer and use it in GitHub Desktop.
Usage of Pendulum's `DateTime` in SQLAlchemy columns of type `DateTime`
version: '3.7'
services:
postgres:
image: postgres:12.3
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
ports:
- 5432:5432
from sqlalchemy import Column, DateTime, Integer, String, Text
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
title = Column(String)
published_at = Column(DateTime)
content = Column(Text)
[tool.poetry]
name = "sqla-pendulum"
version = "0.1.0"
description = "Usage of Pendulum's `DateTime` in SQLA columns"
authors = ["Anton Siluev <burning2007@ya.ru>"]
license = "MIT"
[tool.poetry.dependencies]
python = "~3.7"
pytest = "~6.0"
sqlalchemy = "~1.3"
psycopg2 = "~2.8"
pendulum = "~2.1"
[tool.poetry.dev-dependencies]
black = "19.10b0"
isort = "~5.4"
[tool.black]
skip-string-normalization = true
line-length = 99
[tool.isort]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 99
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
import pendulum
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from models import Base, Post
_DATABASE_URL = 'postgresql+psycopg2://sqla-pendulum:sesame@localhost/sqla-pendulum'
@pytest.fixture
def engine():
engine = create_engine(_DATABASE_URL)
Base.metadata.create_all(bind=engine)
yield engine
# Base.metadata.drop_all(bind=engine) # uncomment this if you want to keep rows in table
@pytest.fixture
def session(engine):
return Session(bind=engine)
def test_pendulum_datetime_can_be_used_in_sqla_column_of_type_datetime(session):
post = Post(title='Nevermind', published_at=pendulum.now())
session.add(post)
session.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment