From 0fc15612ba4fc58192079b442c72034b1ceb1102 Mon Sep 17 00:00:00 2001 From: Antonino Date: Tue, 2 Jan 2024 15:49:22 +0000 Subject: [PATCH] Added newtest for API routes interface --- Makefile | 2 +- cli-fire.py | 2 +- main.py | 9 +++++++-- mylib/logic.py | 5 ++++- requirements.txt | 4 +++- test_main.py | 24 ++++++++++++++++++++++++ 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 test_main.py diff --git a/Makefile b/Makefile index 57dd6fb..eed01cf 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/cli-fire.py b/cli-fire.py index c82749b..4af22d5 100755 --- a/cli-fire.py +++ b/cli-fire.py @@ -4,4 +4,4 @@ from mylib import logic if __name__ == "__main__": - fire.Fire(logic) \ No newline at end of file + fire.Fire(logic) diff --git a/main.py b/main.py index 190c8ad..8fa93de 100644 --- a/main.py +++ b/main.py @@ -6,10 +6,12 @@ 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""" @@ -17,6 +19,7 @@ async def search(value: str): result = search_wiki(value) return {"result": result} + @app.get("/wiki/{name}") async def wiki(name: str): """Retrieve a Wikipedia page""" @@ -24,6 +27,7 @@ async def wiki(name: str): result = wikilogic(name) return {"result": result} + @app.get("/phrase/{name}") async def phrase(name: str): """Retrieve a Wikipedia page and return phrases""" @@ -31,5 +35,6 @@ async def phrase(name: str): result = wikiphrases(name) return {"result": result} -if __name__ == '__main__': - uvicorn.run(app, port=8080, host='0.0.0.0') \ No newline at end of file + +if __name__ == "__main__": + uvicorn.run(app, port=8080, host="0.0.0.0") diff --git a/mylib/logic.py b/mylib/logic.py index 488674d..b547d14 100644 --- a/mylib/logic.py +++ b/mylib/logic.py @@ -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 diff --git a/requirements.txt b/requirements.txt index d220aca..f0686eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,6 @@ black==23.12.1 fire==0.5.0 fastapi==0.108.0 uvicorn==0.25.0 -textblob==0.17.1 \ No newline at end of file +textblob==0.17.1 +httpcore==1.0.2 +httpx==0.26.0 \ No newline at end of file diff --git a/test_main.py b/test_main.py new file mode 100644 index 0000000..bd15531 --- /dev/null +++ b/test_main.py @@ -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", + ] + }