Skip to content

Commit

Permalink
Merge pull request #208 from Jakub3628800/ruff
Browse files Browse the repository at this point in the history
replace black + flake8 with ruff
  • Loading branch information
Jakub3628800 authored Apr 19, 2024
2 parents ff14eb5 + 7a45a3a commit 1aa7ded
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
25 changes: 6 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
default_language_version:
python: python3.10
repos:
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
args:
- --line-length=120

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
args:
- --ignore=D100, D103, D101
- --max-line-length=120
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.1
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
Expand All @@ -23,11 +15,6 @@ repos:
args: [--explicit-package-bases, --namespace-packages]
exclude: "upload_test_coverage.py"

- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports

- repo: https://github.com/jazzband/pip-tools
rev: 7.3.0
hooks:
Expand Down
28 changes: 22 additions & 6 deletions shortener/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ async def check_db_up(connection: asyncpg.Connection) -> bool:

async def get_url_target(short_url: str, connection: asyncpg.Connection) -> str:
try:
return await connection.fetchval("SELECT target from short_urls where url_key=$1;", short_url)
return await connection.fetchval(
"SELECT target from short_urls where url_key=$1;", short_url
)
except Exception:
raise UrlNotFoundException()

Expand All @@ -29,17 +31,31 @@ async def get_all_short_urls(connection: asyncpg.Connection):
await connection.fetchval(query="SELECT * from short_urls;")


async def create_url_target(short_url: str, target_url: str, connection: asyncpg.Connection):
async def create_url_target(
short_url: str, target_url: str, connection: asyncpg.Connection
):
try:
await connection.execute("INSERT INTO short_urls (url_key, target) VALUES ($1,$2);", short_url, target_url)
await connection.execute(
"INSERT INTO short_urls (url_key, target) VALUES ($1,$2);",
short_url,
target_url,
)
except asyncpg.exceptions.UniqueViolationError:
pass
return


async def update_url_target(short_url: str, new_target_url: str, connection: asyncpg.Connection):
return await connection.fetchval("UPDATE short_urls SET target = $1 WHERE url_key = $2;", new_target_url, short_url)
async def update_url_target(
short_url: str, new_target_url: str, connection: asyncpg.Connection
):
return await connection.fetchval(
"UPDATE short_urls SET target = $1 WHERE url_key = $2;",
new_target_url,
short_url,
)


async def delete_url_target(short_url: str, connection: asyncpg.Connection):
return await connection.fetchval("delete from short_urls where url_key=$1;", short_url)
return await connection.fetchval(
"delete from short_urls where url_key=$1;", short_url
)
6 changes: 5 additions & 1 deletion shortener/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
class PostgresSettings(BaseSettings):
"""Settings for postgres connection."""

model_config = {"env_prefix": "DB_", "env_file": ".env", "env_file_encoding": "utf-8"}
model_config = {
"env_prefix": "DB_",
"env_file": ".env",
"env_file_encoding": "utf-8",
}

host: str = "localhost"
port: int = 5432
Expand Down
4 changes: 3 additions & 1 deletion shortener/views/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


def openapi_schema(request: Request):
schemas = SchemaGenerator({"openapi": "3.0.0", "info": {"title": APP_NAME, "version": "1.0"}})
schemas = SchemaGenerator(
{"openapi": "3.0.0", "info": {"title": APP_NAME, "version": "1.0"}}
)
return schemas.OpenAPIResponse(request=request)


Expand Down
21 changes: 16 additions & 5 deletions shortener/views/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Endpoints for editing short_url: target mappings."""

from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
Expand Down Expand Up @@ -28,7 +29,9 @@ async def get_url(request: Request):
async with request.app.pool.acquire() as connection:
target_url = await get_url_target(short_url, connection)

return JSONResponse(content={"short_url": short_url, "target_url": target_url}, status_code=200)
return JSONResponse(
content={"short_url": short_url, "target_url": target_url}, status_code=200
)


async def create_url(request: Request):
Expand Down Expand Up @@ -61,9 +64,13 @@ async def create_url(request: Request):
short_url = body.get("short_url")
target_url = body.get("target_url")
async with request.app.pool.acquire() as connection:
await create_url_target(short_url=short_url, target_url=target_url, connection=connection)
await create_url_target(
short_url=short_url, target_url=target_url, connection=connection
)

return JSONResponse(content={"short_url": short_url, "target_url": target_url}, status_code=201)
return JSONResponse(
content={"short_url": short_url, "target_url": target_url}, status_code=201
)


async def update_url(request: Request):
Expand Down Expand Up @@ -92,9 +99,13 @@ async def update_url(request: Request):
target_url = body.get("target_url")

async with request.app.pool.acquire() as connection:
await update_url_target(short_url=short_url, new_target_url=target_url, connection=connection)
await update_url_target(
short_url=short_url, new_target_url=target_url, connection=connection
)

return JSONResponse(content={"short_url": short_url, "target_url": target_url}, status_code=200)
return JSONResponse(
content={"short_url": short_url, "target_url": target_url}, status_code=200
)


async def delete_url(request: Request):
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

@pytest.fixture(scope="function")
async def test_client():
async_pool = asyncpg.create_pool(min_size=5, max_size=25, **dict(PostgresSettings(_env_file=None)))
async_pool = asyncpg.create_pool(
min_size=5, max_size=25, **dict(PostgresSettings(_env_file=None))
)
async with async_pool as pool:
app.pool = pool
with TestClient(app=app) as client:
Expand Down Expand Up @@ -45,7 +47,9 @@ def psycopg2_cursor():
db_name = os.getenv("DB_NAME", "postgres")
db_user = os.getenv("DB_USER", "localuser")
db_password = os.getenv("DB_PASSWORD", "password123")
connection = psycopg2.connect(dbname=db_name, user=db_user, password=db_password, host=db_host, port=db_port)
connection = psycopg2.connect(
dbname=db_name, user=db_user, password=db_password, host=db_host, port=db_port
)

cur = connection.cursor()
cur.execute("SELECT 1")
Expand Down

0 comments on commit 1aa7ded

Please sign in to comment.