Skip to content

Commit

Permalink
Feature/add asyncpg compability (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaziamov authored Jan 8, 2025
1 parent 03de7e3 commit f80b42a
Show file tree
Hide file tree
Showing 14 changed files with 536 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
python-version: [ 3.8, 3.9, "3.10", 3.11, 3.12 ]
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"" ]

steps:
- name: Checkout code
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ publish:
package-install:
python3 -m pip install --user dist/*.whl --force
lint:
poetry run flake8 simplecrud
poetry run flake8 simplecrud
coverage:
poetry run pytest --cov=simplecrud --cov-report=xml
649 changes: 456 additions & 193 deletions poetry.lock

Large diffs are not rendered by default.

27 changes: 22 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
[tool.poetry]
name = "hexfrost-simplecrud"
version = "0.3.0"
description = "Library with operation like (get, create, update, delete) for SQLAlchemy ORM"
version = "0.3.1"
description = "Library for easy usage database CRUD operations"
authors = ["Ilia Kaziamov <[email protected]>"]
packages = [
{ include = "simplecrud" },
]
readme = "README.md"


[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
sqlalchemy = "^2.0.0"

sqlalchemy = { version = ">=2.0.0", optional = true }
asyncpg = { version = ">=0.29.0", optional = true }


[tool.poetry.extras]
asyncpg = ["asyncpg"]
sqlalchemy = ["sqlalchemy"]


[tool.poetry.group.dev.dependencies]
flake8 = "^7.0.0"
pytest = "^7.4.3"
aiosqlite = "^0.19.0"
pytest = "^7.4.3"
pytest-asyncio = "0.23"
pytest-cov = "^4.1.0"
python-dotenv = "^1.0.1"
psycopg2-binary = "^2.9.10"

[tool.poetry.group.test.dependencies]
sqlalchemy = "^2.0.36"
asyncpg = "^0.30.0"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
build-backend = "poetry.core.masonry.api"
4 changes: 4 additions & 0 deletions simplecrud/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from . import crud

from .asyncpg_commands.cruds import insert_to_table


File renamed without changes.
10 changes: 10 additions & 0 deletions simplecrud/asyncpg_commands/cruds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from asyncpg import connect


async def insert_to_table(conn: connect, table: str, data: dict):
keys = ", ".join(data.keys())
nums = ", ".join([f"${num}" for num in range(1, len(data.items()) + 1)])
values = list(data.values())
query = f"INSERT INTO {table} ({keys}) VALUES ({nums}) ON CONFLICT DO NOTHING;"
await conn.execute(query, *values)

1 change: 0 additions & 1 deletion simplecrud/crud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from typing import Dict, List

from sqlalchemy import select, delete
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions tests/test_asyncpg/test_crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

import psycopg2
import pytest

from asyncpg import connect
from dotenv import load_dotenv

load_dotenv()

from simplecrud import insert_to_table


@pytest.fixture(autouse=True)
def database_url():
return os.getenv("TEST_DATABASE_URL")

@pytest.fixture(autouse=True)
def table_for_test(database_url):
with psycopg2.connect(database_url) as pg_conn:
cur = pg_conn.cursor()
table = "test_table"
cur.execute(f"CREATE TABLE {table} (id SERIAL PRIMARY KEY, name TEXT, age INT)")
pg_conn.commit()
yield table
cur.execute(f"DROP TABLE {table}")
pg_conn.commit()


@pytest.mark.asyncio
async def test_insert_to_table(table_for_test, database_url):
data = {"name": "test", "age": 20}
table = "test_table"
conn = await connect(dsn=database_url)
await insert_to_table(conn, table, data)
with psycopg2.connect(database_url) as pg_conn:
cur = pg_conn.cursor()
cur.execute(f"SELECT * FROM {table}")
result = cur.fetchall()
assert result == [(1, "test", 20)]
Empty file.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session

from simplecrud.tests.test_crud import engine, Base

database_url = "sqlite:///./test.db"
engine = create_engine(database_url)
Expand Down

0 comments on commit f80b42a

Please sign in to comment.