-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add asyncpg compability (#27)
- Loading branch information
Showing
14 changed files
with
536 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters