💩Add test part for later
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
5ef508b939
commit
ddd6eda64f
@ -0,0 +1,3 @@
|
||||
[pytest]
|
||||
addopts = --cov=app --cov-report=term-missing
|
||||
testpaths = tests
|
@ -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
|
@ -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"}
|
||||
"""
|
Loading…
Reference in new issue