Commit 922fca12 authored by Michele Del Giudice's avatar Michele Del Giudice
Browse files

Added tests & migration

parent ad212641
......@@ -28,6 +28,19 @@ export MYSQL_DB=car_rental
python app.py
```
Database migrations (Flask-Migrate / Alembic)
After installing requirements, initialize and run migrations with the Flask CLI. Example:
```bash
export FLASK_APP=app:create_app
flask db init # only once
flask db migrate -m "initial"
flask db upgrade
```
This will create an `migrations/` folder and apply the schema to the configured database.
API endpoints summary:
- GET /cars
......
......@@ -4,12 +4,15 @@ from flask import Flask, request, jsonify, abort
from config import Config
from models import db, Car, Reservation, CarType
from flask_migrate import Migrate
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
# Migrations
Migrate(app, db)
@app.errorhandler(404)
def not_found(e):
......
......@@ -3,7 +3,7 @@ import os
class Config:
# Database connection - use environment variables
MYSQL_USER = os.getenv('MYSQL_USER', 'root')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', '')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', 'root')
MYSQL_HOST = os.getenv('MYSQL_HOST', 'localhost')
MYSQL_PORT = os.getenv('MYSQL_PORT', '3306')
MYSQL_DB = os.getenv('MYSQL_DB', 'car_rental')
......
......@@ -2,3 +2,6 @@ Flask>=2.0
Flask-SQLAlchemy>=3.0
PyMySQL>=1.0
cryptography==46.0.3
Flask-Migrate>=4.0
pytest>=7.0
pytest-flask>=1.2
\ No newline at end of file
import json
from datetime import datetime, timedelta
import pytest
from app import create_app
from models import db, Car
@pytest.fixture
def app():
app = create_app()
app.config.update({
'TESTING': True,
# Use in-memory SQLite for tests
'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:',
})
with app.app_context():
db.create_all()
yield app
db.session.remove()
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
def test_create_and_get_car(client):
payload = {
'name': 'Test Car',
'seats': 4,
'ports': 3,
'type': 'small',
'brand': 'TestBrand',
'license_plate': 'TEST-123'
}
rv = client.post('/cars', json=payload)
assert rv.status_code == 201
data = rv.get_json()
assert data['name'] == 'Test Car'
# GET list
rv2 = client.get('/cars')
assert rv2.status_code == 200
arr = rv2.get_json()
assert any(c['license_plate'] == 'TEST-123' for c in arr)
def test_create_reservation(client):
# Create car first
car_payload = {
'name': 'Reserve Car',
'seats': 5,
'ports': 5,
'type': 'suv',
'brand': 'BrandX',
'license_plate': 'RES-001'
}
rv = client.post('/cars', json=car_payload)
assert rv.status_code == 201
car = rv.get_json()
pickup = datetime.utcnow()
return_dt = pickup + timedelta(days=2)
res_payload = {
'pickup_date': pickup.isoformat(),
'return_date': return_dt.isoformat(),
'car_id': car['id'],
'customer_fullname': 'John Tester',
'drive_license': 'DL-0001'
}
rv2 = client.post('/reservations', json=res_payload)
assert rv2.status_code == 201
res = rv2.get_json()
assert res['car_id'] == car['id']
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment