Skip to content

Commit

Permalink
chore: add ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 20, 2024
1 parent bfeb618 commit fcda8f1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"workspaceFolder": "/app",
"features": {
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers-contrib/features/ruff:1": {},
"ghcr.io/devcontainers/features/sshd:1": {}
"ghcr.io/devcontainers/features/sshd:1": {},
"ghcr.io/devcontainers/features/python:1": {}
}

// Features to add to the dev container. More info: https://containers.dev/features.
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- run: poetry install
- name: setup pytest.ini
run: mv .github/pytest.ini pytest.ini
- name: Linting
run: poetry run ruff check
- name: Run tests
env:
CI: "true"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ __pycache__
.ssh
htmlcov/
.coverage
.gnupg
37 changes: 26 additions & 11 deletions aim/digifeeds/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
from aim.digifeeds.database import schemas
from aim.digifeeds.database import models


def get_item(db: Session, barcode: str):
return db.query(models.Item).filter(models.Item.barcode == barcode).first()


def get_items(db: Session, in_zephir: bool | None):
if in_zephir == True:
#this is working
return db.query(models.Item).filter(models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")).all()
elif in_zephir == False:
#this is not working
return db.query(models.Item).filter(~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")).all()
if in_zephir is True:
return (
db.query(models.Item)
.filter(
models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")
)
.all()
)
elif in_zephir is False:
return (
db.query(models.Item)
.filter(
~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")
)
.all()
)

return db.query(models.Item).all()

Expand All @@ -23,15 +35,18 @@ def add_item(db: Session, item: schemas.ItemCreate):
db.refresh(db_item)
return db_item


def get_status(db: Session, name: str):
return db.query(models.Status).filter(models.Status.name == name).first()


def get_statuses(db: Session):
return db.query(models.Status).all()


def add_item_status(db: Session, item: models.Item, status: models.Status):
db_item_status = models.ItemStatus(item=item, status=status)
db.add(db_item_status)
db.commit()
db.refresh(item)
return item
db_item_status = models.ItemStatus(item=item, status=status)
db.add(db_item_status)
db.commit()
db.refresh(item)
return item
4 changes: 2 additions & 2 deletions aim/digifeeds/database/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from aim.digifeeds.database import crud, models, schemas
from aim.digifeeds.database import crud, schemas
from aim.services import S

if S.ci_on == None: # pragma: no cover
if S.ci_on is None: # pragma: no cover
engine = create_engine(S.mysql_database)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# models.Base.metadata.create_all(bind=engine)
Expand Down
9 changes: 4 additions & 5 deletions aim/digifeeds/database/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

from alembic import context

from aim.digifeeds.database.models import Base
from aim.services import S

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Set url from environment variable
config.set_main_option('sqlalchemy.url', S.mysql_database)
config.set_main_option("sqlalchemy.url", S.mysql_database)

# Interpret the config file for Python logging.
# This line sets up loggers basically.
Expand All @@ -21,7 +22,7 @@

# add your model's MetaData object here
# for 'autogenerate' support
from aim.digifeeds.database.models import Base

target_metadata = Base.metadata

# other values from the config, defined by the needs of env.py,
Expand Down Expand Up @@ -68,9 +69,7 @@ def run_migrations_online() -> None:
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()
Expand Down
2 changes: 1 addition & 1 deletion aim/digifeeds/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_statuses(session: Session):
objects = []
for status in statuses:
sts = session.query(Status).filter_by(name = status["name"]).first()
if sts == None:
if sts is None:
objects.append(Status(**status))

print(f"Statuses to load: {objects}")
Expand Down
39 changes: 20 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ alembic = "^1.13.2"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.2"
ruff = "^0.2.2"
pytest-cov = "^5.0.0"
ruff = "^0.6.6"

[tool.pytest.ini_options]
addopts = "--cov=aim --cov-report=html --cov-report=term:skip-covered"
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import StaticPool, create_engine
from sqlalchemy.orm import sessionmaker
from aim.digifeeds.database.main import app, get_db
from aim.digifeeds.database.models import Base, load_statuses, Status
from aim.digifeeds.database.models import Base, load_statuses

from aim.services import S

Expand Down
4 changes: 2 additions & 2 deletions tests/digifeeds/database/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_get_item(self, db_session):

def test_get_item_that_does_not_exist(self, db_session):
item_in_db = get_item(barcode="does not exist", db=db_session)
assert(item_in_db) == None
assert(item_in_db) is None

def test_get_items_all(self, db_session):
item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode"))
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_get_status_that_exists(self, db_session):

def test_get_status_that_does_not_exist(self, db_session):
status = get_status(db=db_session, name="does_not_exist")
assert(status) == None
assert(status) is None

def test_get_statuses(self, db_session):
statuses = get_statuses(db=db_session)
Expand Down
2 changes: 1 addition & 1 deletion tests/digifeeds/database/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aim.digifeeds.database import crud, models
from aim.digifeeds.database import crud
from aim.digifeeds.database.schemas import ItemCreate
import pytest

Expand Down

0 comments on commit fcda8f1

Please sign in to comment.