From 97901f02a6f44a91f26c57c686a2eae2cb45702e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katariina=20J=C3=A4rvenm=C3=A4ki?= Date: Sun, 1 Mar 2026 13:38:18 +0200 Subject: [PATCH] Update README with test instructions and add unit tests --- README.md | 9 ++++++++- app/test_main.py | 24 ++++++++++++++++++++++++ urls.db | Bin 8192 -> 8192 bytes 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/test_main.py diff --git a/README.md b/README.md index ab6d762..2bde86f 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,11 @@ http://127.0.0.1:8000 http://127.0.0.1:8000/docs Test Short URL Endpoint -http://127.0.0.1:8000/shorten \ No newline at end of file +http://127.0.0.1:8000/shorten + +## Running a test +```bash +PYTHONPATH=./ pytest +export PYTHONPATH=$(pwd) +pytest + ``` \ No newline at end of file diff --git a/app/test_main.py b/app/test_main.py new file mode 100644 index 0000000..887e6aa --- /dev/null +++ b/app/test_main.py @@ -0,0 +1,24 @@ +from fastapi.testclient import TestClient +from app.main import app +import sys +import os + +# Add the 'app' folder to the sys.path +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from app.main import app # Now this should work correctly + +client = TestClient(app) + +def test_home(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"message": "URL Shortener API"} + +def test_shorten_url(): + response = client.post("/shorten", json={"url": "https://google.com"}) + assert response.status_code == 200 + data = response.json() + assert "short_url" in data + short_url = data["short_url"] + assert short_url.startswith("http://localhost:8000/") \ No newline at end of file diff --git a/urls.db b/urls.db index c22e4bbb69df301c12a192c85a9bc3e8c091b129..7ef956d5677906df01455681079f716e3ab9eeac 100644 GIT binary patch delta 202 zcmZp0XmFSy&B!@X#+i|GW5N=CA$ERs2L6ZqhxixsxA3R(yYZ`U78DTVua{+KWso%F zNHHu)$t=w%DJdwn($`PV&ri=u)l1ILm1P4<=Xn|xJ11e0W(7-U6_u3vnrsWiIqXpkh9DsDb&wk M@