From ddd6eda64ffc23a5eb67e27b523ec16101386741 Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 10 Dec 2024 08:56:56 +0100 Subject: [PATCH] :poop:Add test part for later --- pytest.ini | 3 ++ tests/requirements.txt | 7 ++++ tests/test_main.py | 92 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 pytest.ini create mode 100644 tests/requirements.txt create mode 100644 tests/test_main.py diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..b985420 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +addopts = --cov=app --cov-report=term-missing +testpaths = tests \ No newline at end of file diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..d0d2290 --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,7 @@ +fastapi[standard]==0.115.5 +pydantic==2.9.2 +pymongo==4.10.1 +uvicorn==0.32.0 +python-jose==3.3.0 +pytest==8.3.4 +pytest-cov==6.0.0 \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..7246019 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,92 @@ +from fastapi.testclient import TestClient + +import sys +from pathlib import Path +sys.path.append(str(Path(__file__).absolute().parent.parent)) + +# Contains all constants +import app.config as config + +import pymongo + +# Import the app +from app.main import app + +# Database setup +client = pymongo.MongoClient(config.MONGODB_URL, username=config.MONGODB_USERNAME, password=config.MONGODB_PASSWORD) + +if config.MONGODB_DATABASE in client.list_database_names(): + try: + client.drop_database(config.MONGODB_DATABASE) + except Exception as e: + print(f"Error with database : {e}") + +# Create empty database +db = client[config.MONGODB_DATABASE] + + +client = TestClient(app) + +def test_number_one(): + print("OK") + +""" +def test_read_item(): + response = client.get("/items/foo", headers={"X-Token": "coneofsilence"}) + assert response.status_code == 200 + assert response.json() == { + "id": "foo", + "title": "Foo", + "description": "There goes my hero", + } + + +def test_read_item_bad_token(): + response = client.get("/items/foo", headers={"X-Token": "hailhydra"}) + assert response.status_code == 400 + assert response.json() == {"detail": "Invalid X-Token header"} + + +def test_read_nonexistent_item(): + response = client.get("/items/baz", headers={"X-Token": "coneofsilence"}) + assert response.status_code == 404 + assert response.json() == {"detail": "Item not found"} + + +def test_create_item(): + response = client.post( + "/items/", + headers={"X-Token": "coneofsilence"}, + json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"}, + ) + assert response.status_code == 200 + assert response.json() == { + "id": "foobar", + "title": "Foo Bar", + "description": "The Foo Barters", + } + + +def test_create_item_bad_token(): + response = client.post( + "/items/", + headers={"X-Token": "hailhydra"}, + json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"}, + ) + assert response.status_code == 400 + assert response.json() == {"detail": "Invalid X-Token header"} + + +def test_create_existing_item(): + response = client.post( + "/items/", + headers={"X-Token": "coneofsilence"}, + json={ + "id": "foo", + "title": "The Foo ID Stealers", + "description": "There goes my stealer", + }, + ) + assert response.status_code == 409 + assert response.json() == {"detail": "Item already exists"} +""" \ No newline at end of file