Skip to content

Latest commit

 

History

History
158 lines (112 loc) · 4.14 KB

File metadata and controls

158 lines (112 loc) · 4.14 KB

Flask + SQLite Sample App with Keploy Integration

This is a simple Student Management REST API built using Python's Flask framework and SQLite for storage. It demonstrates basic CRUD operations (Create, Read, Update, Delete) and showcases how to write API tests with Keploy by auto-capturing HTTP calls as test cases.


🚀 Features

  • 🧑 Add, retrieve, update, and delete students
  • 💾 Uses SQLite — no external DB setup required
  • 🔌 RESTful API with JSON input/output
  • ✅ Auto-generate test cases using Keploy CLI
  • 🔄 Replay & validate API responses with Keploy

📦 Tech Stack

  • Flask — Lightweight web framework
  • Flask-SQLAlchemy — ORM for SQLite
  • SQLite — Built-in relational DB
  • Keploy — Testing toolkit for API auto-mocking and regression testing

🛠 Installation

  1. Clone the repository
git clone https://github.com/<your-username>/samples-python.git
cd samples-python/flask-sqlite
  1. Set up virtual environment (optional)
python3 -m venv venv
source venv/bin/activate
  1. Install Dependencies
pip install -r requirements.txt
  1. Run the Flask app
python app.py

API Endpoints

| Method | Endpoint         | Description          |
| ------ | ---------------- | -------------------- |
| GET    | `/students`      | Get all students     |
| POST   | `/students`      | Add a new student    |
| PUT    | `/students/<id>` | Update student by ID |
| DELETE | `/students/<id>` | Delete student by ID |

Sample Curl Commands

http://localhost:5000/

{
    "message": "Flask Student API"
}

Add a student

curl -X POST http://localhost:5000/students \
     -H "Content-Type: application/json" \
     -d '{"name": "Alice", "age": 21}'

Get all students

curl http://localhost:5000/students

Update student

curl -X PUT http://localhost:5000/students/1
-H "Content-Type: application/json"
-d '{"name": "Alice Updated", "age": 22}'

Delete student

curl -X DELETE http://localhost:5000/students/1

---

## Running Keploy Tests

Step 1: Record Tests
Start Keploy in record mode:

```bash
keploy record --command "python app.py"

Send some API requests via curl or Postman to generate test cases.

Step 2: Replay Tests After recording, stop the app and run:

keploy test --command "python app.py"

Keploy will replay the previously captured requests and validate responses.

Folder Structure

flask-sqlite/
├── app.py               # Flask app entry point
├── models.py            # Student model
├── requirements.txt     # Python dependencies
├── keploy.yml           # Keploy config file
├── keploy/              # Auto-generated test cases
└── README.md            # You are here!

Contributing

Want to improve or add another example (e.g. FastAPI, SQLModel, etc.)? Contributions are welcome! Fork this repo, create your example folder, and submit a PR.

About Keploy

Keploy is a developer-friendly open-source testing toolkit that auto-generates test cases from API calls in real-time and replays them to catch regressions — without writing any test code.

Built with ❤️ for the Open Source Community.

Stage Improvement Type Description
Stage 1 Validation & Pagination Add search, filter, pagination for students
Stage 2 Security Add authentication, role-based access
Stage 3 Code Quality Integrate Marshmallow/Pydantic, add tests
Stage 4 Documentation & Deployment Add Swagger, Dockerize, CI/CD pipeline
Stage 5 Frontend & Realtime Features Build React UI, add WebSocket updates

Stage 1

Get End points

/students?page=1&per_page=5 — Gets first 5 students.

/students?name=John — Filters students whose names contain "John".

/students?age=20 — Filters students aged 20.

/students?page=2&per_page=3&name=ann — Combination.