-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
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 sqlalchemy.orm import Session | ||
|
||
import models | ||
from app import schemas | ||
|
||
|
||
def get_product(db: Session, product_gid: int): | ||
product = db.query(models.Product).filter(models.Product.gid ==product_gid).first() | ||
|
||
return product |
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,27 @@ | ||
from fastapi import Depends, FastAPI, HTTPException | ||
from sqlalchemy.orm import Session | ||
|
||
from app import crud, schemas | ||
import models | ||
from database import SessionLocal, engine | ||
|
||
models.Base.metadata.create_all(bind=engine) | ||
|
||
app = FastAPI() | ||
|
||
|
||
# Dependency | ||
def get_db(): | ||
db = SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
|
||
@app.get("/products/{product_gid}/", response_model=schemas.Product) | ||
def read_product(product_gid: int, db: Session = Depends(get_db)): | ||
db_product = crud.get_product(db, product_gid=product_gid) | ||
if db_product is None: | ||
raise HTTPException(status_code=404, detail="Product not found") | ||
return db_product |
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,24 @@ | ||
from typing import List | ||
from pydantic import BaseModel, ConfigDict | ||
from datetime import datetime | ||
|
||
|
||
class Price(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
gid: int | ||
amount: float = None | ||
product_gid: int | ||
created: datetime | ||
|
||
|
||
class Product(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
gid: int | ||
name: str = None | ||
url: str = None | ||
prices: List[Price] | ||
created: datetime | ||
last_updated: datetime | ||
|
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,42 @@ | ||
from fastapi.testclient import TestClient | ||
from fastapi import Depends | ||
import models | ||
from database import SessionLocal, engine | ||
from sqlalchemy.orm import Session | ||
|
||
models.Base.metadata.create_all(bind=engine) | ||
|
||
from .main import app | ||
from datetime import datetime | ||
|
||
client = TestClient(app) | ||
|
||
|
||
def test_read_main(): | ||
|
||
name = "Sample Shirt" | ||
url = "https://www.outdoorstore.com" | ||
amount = 123.45 | ||
|
||
db = SessionLocal() | ||
|
||
product = db.query(models.Product).filter_by(name=name).one_or_none() | ||
if not product: | ||
# Add a test product | ||
product = models.Product(name=name, url=url, created=datetime.now(), last_updated=datetime.now()) | ||
db.add(product) | ||
db.commit() | ||
db.refresh(product) | ||
|
||
# Add a test price | ||
price = models.Price(amount=amount, product=product, created=datetime.now()) | ||
db.add(price) | ||
db.commit() | ||
|
||
response = client.get(f'/products/{product.gid}/') | ||
assert response.status_code == 200 | ||
data = response.json() | ||
|
||
assert data['name'] == name | ||
assert data['url'] == url | ||
assert data['prices'][0]['amount'] == amount |
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,13 @@ | ||
from os import getenv | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker, declarative_base | ||
|
||
SQLALCHEMY_DATABASE_URL = getenv('DATABASE_URL', 'postgresql://postgres:postgres@localhost/changedetection') | ||
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db" | ||
|
||
engine = create_engine( | ||
SQLALCHEMY_DATABASE_URL | ||
) | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
Base = declarative_base() |
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
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 |
---|---|---|
|
@@ -26,7 +26,6 @@ psycopg2 | |
alembic | ||
sentry-sdk | ||
|
||
|
||
|
||
# This is for testing | ||
pytest | ||
pytest | ||
httpx |
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 |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
set -e | ||
|
||
alembic upgrade head | ||
pytest tests.py | ||
pytest | ||
scrapy check |