Skip to content

Commit cacb976

Browse files
committed
Add test and github action
1 parent 19b654a commit cacb976

File tree

5 files changed

+122
-15
lines changed

5 files changed

+122
-15
lines changed

.github/workflows/tests.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
postgres:
15+
image: postgres:14
16+
env:
17+
POSTGRES_USER: postgres
18+
POSTGRES_PASSWORD: postgres
19+
POSTGRES_DB: fastapi_db
20+
ports:
21+
- 5432:5432
22+
options: >-
23+
--health-cmd pg_isready
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
28+
env:
29+
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/fastapi_db
30+
PROJECT_NAME: "FastAPI with PostgreSQL"
31+
API_VERSION: "v1"
32+
PYTHONPATH: .
33+
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v4
37+
38+
- name: Set up Python
39+
uses: actions/setup-python@v5
40+
with:
41+
python-version: "3.11"
42+
43+
- name: Install dependencies
44+
run: |
45+
python -m pip install --upgrade pip
46+
pip install -r requirements.txt
47+
48+
- name: Wait for PostgreSQL to be ready
49+
run: |
50+
for i in {1..10}; do
51+
pg_isready -h localhost -p 5432 && break
52+
sleep 2
53+
done
54+
55+
- name: Run tests
56+
run: pytest

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version: '3.9'
33
services:
44
web:
55
build: .
6+
environment:
7+
- PYTHONPATH=/app
68
container_name: fastapi_app
79
command: uvicorn app.main:app --host 0.0.0.0 --port 8001
810
volumes:

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ sqlalchemy
88
psycopg2-binary
99
passlib[bcrypt]
1010
python-jose[cryptography]
11-
python-multipart
11+
python-multipart
12+
httpx

tests/test_endpoints.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from fastapi.testclient import TestClient
2+
from app.main import app
3+
4+
client = TestClient(app)
5+
6+
def test_root_and_hello():
7+
res = client.get("/")
8+
assert res.status_code == 200
9+
assert res.json() == {"message": "Welcome to the API"}
10+
11+
res = client.get("/hello")
12+
assert res.status_code == 200
13+
assert res.json() == {"message": "Hello World"}
14+
15+
def test_register_and_duplicate_user():
16+
res = client.post("/auth/register", json={"username": "charlie", "password": "pass123"})
17+
assert res.status_code == 200
18+
assert "id" in res.json()
19+
20+
res = client.post("/auth/register", json={"username": "charlie", "password": "pass123"})
21+
assert res.status_code == 400
22+
assert res.json()["detail"] == "Username already registered"
23+
24+
def test_login_with_correct_and_wrong_credentials():
25+
res = client.post("/auth/token", data={"username": "charlie", "password": "pass123"})
26+
assert res.status_code == 200
27+
token = res.json()["access_token"]
28+
assert token is not None
29+
30+
res = client.post("/auth/token", data={"username": "charlie", "password": "wrongpass"})
31+
assert res.status_code == 400
32+
assert res.json()["detail"] == "Incorrect username or password"
33+
34+
def test_protected_endpoints_with_token():
35+
# Login
36+
res = client.post("/auth/token", data={"username": "charlie", "password": "pass123"})
37+
token = res.json()["access_token"]
38+
headers = {"Authorization": f"Bearer {token}"}
39+
40+
# Crear item
41+
item = {"name": "Monitor", "price": 189.99, "in_stock": True}
42+
res = client.post("/items/", json=item, headers=headers)
43+
assert res.status_code == 200
44+
created = res.json()
45+
assert created["name"] == "Monitor"
46+
assert created["price"] == 189.99
47+
assert created["in_stock"] is True
48+
49+
# Listar items
50+
res = client.get("/items/", headers=headers)
51+
assert res.status_code == 200
52+
items = res.json()
53+
assert isinstance(items, list)
54+
assert any(i["name"] == "Monitor" for i in items)
55+
56+
def test_protected_endpoints_without_token():
57+
item = {"name": "ShouldFail", "price": 0.0, "in_stock": False}
58+
res = client.post("/items/", json=item)
59+
assert res.status_code == 401
60+
61+
res = client.get("/items/")
62+
assert res.status_code == 401

tests/test_hello.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)