Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add tests #25

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repos:
rev: 23.11.0
hooks:
- id: black
language_version: python3.11
language_version: python3.10

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
Expand Down
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ pre-commit install
```

Now `pre-commit` will run automatically on `git commit` :)

## Write and run tests

You should create unit tests for each new feature or API change (see [test_api.py](https://github.com/openfoodfacts/open-prices/blob/main/tests/test_api.py)).

To run tests, just launch:
```bash
pytest
```
52 changes: 29 additions & 23 deletions alembic/versions/20231115_1745_20145023aad0_add_proof_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,49 @@
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = '20145023aad0'
down_revision: Union[str, None] = '43571a31006d'
revision: str = "20145023aad0"
down_revision: Union[str, None] = "43571a31006d"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('proofs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('file_path', sa.String(), nullable=False),
sa.Column('mimetype', sa.String(), nullable=True),
sa.Column('owner', sa.String(), nullable=True),
sa.Column('created', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('id')
op.create_table(
"proofs",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("file_path", sa.String(), nullable=False),
sa.Column("mimetype", sa.String(), nullable=True),
sa.Column("owner", sa.String(), nullable=True),
sa.Column(
"created",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=True,
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f('ix_proofs_created'), 'proofs', ['created'], unique=False)
op.create_index(op.f('ix_proofs_id'), 'proofs', ['id'], unique=False)
op.create_index(op.f('ix_proofs_mimetype'), 'proofs', ['mimetype'], unique=False)
op.create_index(op.f('ix_proofs_owner'), 'proofs', ['owner'], unique=False)
op.add_column('prices', sa.Column('proof_id', sa.Integer(), nullable=True))
op.create_foreign_key(None, 'prices', 'proofs', ['proof_id'], ['id'])
op.create_index(op.f("ix_proofs_created"), "proofs", ["created"], unique=False)
op.create_index(op.f("ix_proofs_id"), "proofs", ["id"], unique=False)
op.create_index(op.f("ix_proofs_mimetype"), "proofs", ["mimetype"], unique=False)
op.create_index(op.f("ix_proofs_owner"), "proofs", ["owner"], unique=False)
op.add_column("prices", sa.Column("proof_id", sa.Integer(), nullable=True))
op.create_foreign_key(None, "prices", "proofs", ["proof_id"], ["id"])
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'prices', type_='foreignkey')
op.drop_column('prices', 'proof_id')
op.drop_index(op.f('ix_proofs_owner'), table_name='proofs')
op.drop_index(op.f('ix_proofs_mimetype'), table_name='proofs')
op.drop_index(op.f('ix_proofs_id'), table_name='proofs')
op.drop_index(op.f('ix_proofs_created'), table_name='proofs')
op.drop_table('proofs')
op.drop_constraint(None, "prices", type_="foreignkey")
op.drop_column("prices", "proof_id")
op.drop_index(op.f("ix_proofs_owner"), table_name="proofs")
op.drop_index(op.f("ix_proofs_mimetype"), table_name="proofs")
op.drop_index(op.f("ix_proofs_id"), table_name="proofs")
op.drop_index(op.f("ix_proofs_created"), table_name="proofs")
op.drop_table("proofs")
# ### end Alembic commands ###
6 changes: 4 additions & 2 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"url": "https://www.gnu.org/licenses/agpl-3.0.en.html",
},
)
add_pagination(app)
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
init_sentry(settings.sentry_dns)

Expand Down Expand Up @@ -111,7 +110,7 @@ async def authentication(
r = requests.post(settings.oauth2_server_url, data=data) # type: ignore
if r.status_code == 200:
token = await create_token(form_data.username)
user: schemas.UserBase = {"user_id": form_data.username, "token": token} # type: ignore
user = schemas.UserBase(user_id=form_data.username, token=token)
crud.create_user(db, user=user)
return {"access_token": token, "token_type": "bearer"}
elif r.status_code == 403:
Expand Down Expand Up @@ -206,3 +205,6 @@ async def status_endpoint():
@app.get("/robots.txt", response_class=PlainTextResponse)
def robots_txt():
return """User-agent: *\nDisallow: /"""


add_pagination(app)
4 changes: 2 additions & 2 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def get_user_by_token(db: Session, token: str):

def create_user(db: Session, user: UserBase):
# first we delete any existing user
delete_user(db, user_id=user["user_id"])
delete_user(db, user_id=user.user_id)
# then we (re)create a user
db_user = User(user_id=user["user_id"], token=user["token"])
db_user = User(user_id=user.user_id, token=user.token)
db.add(db_user)
db.commit()
db.refresh(db_user)
Expand Down
Loading
Loading