Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Jan 24, 2024
1 parent 49d02c4 commit 3498333
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 101 deletions.
5 changes: 1 addition & 4 deletions project/app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ async def get_all() -> List:


async def post(payload: SummaryPayloadSchema) -> int:
summary = TextSummary(
url=payload.url,
summary="dummy summary",
)
summary = TextSummary(url=payload.url, summary="")
await summary.save()
return summary.id

Expand Down
9 changes: 7 additions & 2 deletions project/app/api/summaries.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import List

from fastapi import APIRouter, HTTPException, Path
from fastapi import APIRouter, BackgroundTasks, HTTPException, Path

from app.api import crud
from app.models.tortoise import SummarySchema
from app.summarizer import generate_summary

from app.models.pydantic import ( # isort:skip
SummaryPayloadSchema,
Expand All @@ -29,9 +30,13 @@ async def read_all_summaries() -> List[SummarySchema]:


@router.post("/", response_model=SummaryResponseSchema, status_code=201)
async def create_summary(payload: SummaryPayloadSchema) -> SummaryResponseSchema:
async def create_summary(
payload: SummaryPayloadSchema, background_tasks: BackgroundTasks
) -> SummaryResponseSchema:
summary_id = await crud.post(payload)

background_tasks.add_task(generate_summary, summary_id, str(payload.url))

response_object = {"id": summary_id, "url": payload.url}
return response_object

Expand Down
21 changes: 21 additions & 0 deletions project/app/summarizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import nltk
from newspaper import Article

from app.models.tortoise import TextSummary


async def generate_summary(summary_id: int, url: str) -> None:
article = Article(url)
article.download()
article.parse()

try:
nltk.data.find("tokenizers/punkt")
except LookupError:
nltk.download("punkt")
finally:
article.nlp()

summary = article.summary

await TextSummary.filter(id=summary_id).update(summary=summary)
2 changes: 2 additions & 0 deletions project/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ flake8==7.0.0
gunicorn==21.0.1
httpx==0.26.0
isort==5.13.2
newspaper3k==0.2.8
pydantic-settings==2.1.0
pytest==7.4.4
pytest-cov==4.1.0
pytest-xdist==3.5.0
tortoise-orm==0.20.0
uvicorn==0.26.0
127 changes: 32 additions & 95 deletions project/tests/test_summaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import pytest

from app.api import summaries


def test_create_summary(test_app_with_db, monkeypatch):
def mock_generate_summary(summary_id, url):
return None

monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary)

def test_create_summary(test_app_with_db):
response = test_app_with_db.post(
"/summaries/", data=json.dumps({"url": "https://foo.bar"})
)
Expand Down Expand Up @@ -34,7 +41,12 @@ def test_create_summaries_invalid_json(test_app):
)


def test_read_summary(test_app_with_db):
def test_read_summary(test_app_with_db, monkeypatch):
def mock_generate_summary(summary_id, url):
return None

monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary)

response = test_app_with_db.post(
"/summaries/", data=json.dumps({"url": "https://foo.bar"})
)
Expand All @@ -46,7 +58,6 @@ def test_read_summary(test_app_with_db):
response_dict = response.json()
assert response_dict["id"] == summary_id
assert response_dict["url"] == "https://foo.bar/"
assert response_dict["summary"]
assert response_dict["created_at"]


Expand All @@ -71,7 +82,12 @@ def test_read_summary_incorrect_id(test_app_with_db):
}


def test_read_all_summaries(test_app_with_db):
def test_read_all_summaries(test_app_with_db, monkeypatch):
def mock_generate_summary(summary_id, url):
return None

monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary)

response = test_app_with_db.post(
"/summaries/", data=json.dumps({"url": "https://foo.bar"})
)
Expand All @@ -84,7 +100,12 @@ def test_read_all_summaries(test_app_with_db):
assert len(list(filter(lambda d: d["id"] == summary_id, response_list))) == 1


def test_remove_summary(test_app_with_db):
def test_remove_summary(test_app_with_db, monkeypatch):
def mock_generate_summary(summary_id, url):
return None

monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary)

response = test_app_with_db.post(
"/summaries/", data=json.dumps({"url": "https://foo.bar"})
)
Expand Down Expand Up @@ -116,7 +137,12 @@ def test_remove_summary_incorrect_id(test_app_with_db):
}


def test_update_summary(test_app_with_db):
def test_update_summary(test_app_with_db, monkeypatch):
def mock_generate_summary(summary_id, url):
return None

monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary)

response = test_app_with_db.post(
"/summaries/", data=json.dumps({"url": "https://foo.bar"})
)
Expand All @@ -135,95 +161,6 @@ def test_update_summary(test_app_with_db):
assert response_dict["created_at"]


# def test_update_summary_incorrect_id(test_app_with_db):
# response = test_app_with_db.put(
# "/summaries/999/",
# data=json.dumps({"url": "https://foo.bar", "summary": "updated!"})
# )
# assert response.status_code == 404
# assert response.json()["detail"] == "Summary not found"

# response = test_app_with_db.put(
# f"/summaries/0/",
# data=json.dumps({"url": "https://foo.bar", "summary": "updated!"})
# )
# assert response.status_code == 422
# assert response.json() == {
# "detail": [
# {
# "ctx": {"gt": 0},
# "input": "0",
# "loc": ["path", "id"],
# "msg": "Input should be greater than 0",
# "type": "greater_than",
# "url": "https://errors.pydantic.dev/2.5/v/greater_than",
# }
# ]
# }


# def test_update_summary_invalid_json(test_app_with_db):
# response = test_app_with_db.post(
# "/summaries/", data=json.dumps({"url": "https://foo.bar"})
# )
# summary_id = response.json()["id"]

# response = test_app_with_db.put(
# f"/summaries/{summary_id}/",
# data=json.dumps({})
# )
# assert response.status_code == 422
# assert response.json() == {
# "detail": [
# {
# "input": {},
# "loc": ["body", "url"],
# "msg": "Field required",
# "type": "missing",
# "url": "https://errors.pydantic.dev/2.5/v/missing",
# },
# {
# "input": {},
# "loc": ["body", "summary"],
# "msg": "Field required",
# "type": "missing",
# "url": "https://errors.pydantic.dev/2.5/v/missing",
# }
# ]
# }


# def test_update_summary_invalid_keys(test_app_with_db):
# response = test_app_with_db.post(
# "/summaries/", data=json.dumps({"url": "https://foo.bar"})
# )
# summary_id = response.json()["id"]

# response = test_app_with_db.put(
# f"/summaries/{summary_id}/",
# data=json.dumps({"url": "https://foo.bar"})
# )
# assert response.status_code == 422
# assert response.json() == {
# "detail": [
# {
# "input": {"url": "https://foo.bar"},
# "loc": ["body", "summary"],
# "msg": "Field required",
# "type": "missing",
# "url": "https://errors.pydantic.dev/2.5/v/missing",
# }
# ]
# }

# response = test_app_with_db.put(
# f"/summaries/{summary_id}/",
# data=json.dumps({"url": "invalid://url", "summary": "updated!"})
# )
# assert response.status_code == 422
# assert response.json()["detail"][0]["msg"] == "URL scheme should be 'http' or 'https'"


@pytest.mark.parametrize(
"summary_id, payload, status_code, detail",
[
Expand Down
Loading

0 comments on commit 3498333

Please sign in to comment.