Skip to content

Commit

Permalink
Added newtest for API routes interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-do committed Jan 2, 2024
1 parent 1117234 commit 0fc1561
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ lint:
pylint --disable=R,C *.py mylib/*.py
test:
#test
python -m pytest -vv --cov=mylib test_logic.py
python -m pytest -vv --cov=mylib --cov=main test_*.py
build:
#build container
deploy:
Expand Down
2 changes: 1 addition & 1 deletion cli-fire.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from mylib import logic

if __name__ == "__main__":
fire.Fire(logic)
fire.Fire(logic)
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Wikipedia API. Call /search or /wiki"}


@app.get("/search/{value}")
async def search(value: str):
"""Term to search in Wikipedia"""

result = search_wiki(value)
return {"result": result}


@app.get("/wiki/{name}")
async def wiki(name: str):
"""Retrieve a Wikipedia page"""

result = wikilogic(name)
return {"result": result}


@app.get("/phrase/{name}")
async def phrase(name: str):
"""Retrieve a Wikipedia page and return phrases"""

result = wikiphrases(name)
return {"result": result}

if __name__ == '__main__':
uvicorn.run(app, port=8080, host='0.0.0.0')

if __name__ == "__main__":
uvicorn.run(app, port=8080, host="0.0.0.0")
5 changes: 4 additions & 1 deletion mylib/logic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import wikipedia
from textblob import TextBlob


def wiki(name="Monza", lenght=1):
"""This is a Wikipedia fetcher"""

my_wiki = wikipedia.summary(name, lenght)
return my_wiki


def search_wiki(name):
"""Search Wikipedia for Names"""

results = wikipedia.search(name)
return results


def phrase(name):
"""Returns phrases from wikipedia"""

page = wiki(name)
blob = TextBlob(page)
return blob.noun_phrases
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ black==23.12.1
fire==0.5.0
fastapi==0.108.0
uvicorn==0.25.0
textblob==0.17.1
textblob==0.17.1
httpcore==1.0.2
httpx==0.26.0
24 changes: 24 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)


def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Wikipedia API. Call /search or /wiki"}


def test_read_phrase():
response = client.get("/phrase/Barack Obama")
assert response.status_code == 200
assert response.json() == {
"result": [
"barack hussein obama ii",
"bə-rahk hoo-sayn oh-bah-mə",
"august",
"american politician",
"44th president",
]
}

0 comments on commit 0fc1561

Please sign in to comment.