|
| 1 | +# Flask + SQLite Sample App with Keploy Integration |
| 2 | + |
| 3 | +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](https://keploy.io)** by auto-capturing HTTP calls as test cases. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🚀 Features |
| 8 | + |
| 9 | +- 🧑 Add, retrieve, update, and delete students |
| 10 | +- 💾 Uses SQLite — no external DB setup required |
| 11 | +- 🔌 RESTful API with JSON input/output |
| 12 | +- ✅ Auto-generate test cases using [Keploy CLI](https://docs.keploy.io) |
| 13 | +- 🔄 Replay & validate API responses with Keploy |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 📦 Tech Stack |
| 18 | + |
| 19 | +- **Flask** — Lightweight web framework |
| 20 | +- **Flask-SQLAlchemy** — ORM for SQLite |
| 21 | +- **SQLite** — Built-in relational DB |
| 22 | +- **Keploy** — Testing toolkit for API auto-mocking and regression testing |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 🛠 Installation |
| 27 | + |
| 28 | +1. **Clone the repository** |
| 29 | +```bash |
| 30 | +git clone https://github.com/<your-username>/samples-python.git |
| 31 | +cd samples-python/flask-sqlite |
| 32 | +``` |
| 33 | + |
| 34 | +2. Set up virtual environment (optional) |
| 35 | +```bash |
| 36 | +python3 -m venv venv |
| 37 | +source venv/bin/activate |
| 38 | +``` |
| 39 | + |
| 40 | +3. Install Dependencies |
| 41 | + |
| 42 | +```bash |
| 43 | +pip install -r requirements.txt |
| 44 | +``` |
| 45 | +4. Run the Flask app |
| 46 | + |
| 47 | +```bash |
| 48 | +python app.py |
| 49 | +``` |
| 50 | +--- |
| 51 | + |
| 52 | +## API Endpoints |
| 53 | + |
| 54 | +```bash |
| 55 | + |
| 56 | +| Method | Endpoint | Description | |
| 57 | +| ------ | ---------------- | -------------------- | |
| 58 | +| GET | `/students` | Get all students | |
| 59 | +| POST | `/students` | Add a new student | |
| 60 | +| PUT | `/students/<id>` | Update student by ID | |
| 61 | +| DELETE | `/students/<id>` | Delete student by ID | |
| 62 | + |
| 63 | +``` |
| 64 | +--- |
| 65 | + |
| 66 | +## Sample Curl Commands |
| 67 | + |
| 68 | +### Add a student |
| 69 | + |
| 70 | +```bash |
| 71 | +curl -X POST http://localhost:5000/students \ |
| 72 | + -H "Content-Type: application/json" \ |
| 73 | + -d '{"name": "Alice", "age": 21}' |
| 74 | + |
| 75 | +# Get all students |
| 76 | +curl http://localhost:5000/students |
| 77 | + |
| 78 | +# Update student |
| 79 | +curl -X PUT http://localhost:5000/students/1 \ |
| 80 | + -H "Content-Type: application/json" \ |
| 81 | + -d '{"name": "Alice Updated", "age": 22}' |
| 82 | + |
| 83 | +# Delete student |
| 84 | +curl -X DELETE http://localhost:5000/students/1 |
| 85 | + |
| 86 | +``` |
| 87 | +--- |
| 88 | + |
| 89 | +## Running Keploy Tests |
| 90 | + |
| 91 | +Step 1: Record Tests |
| 92 | +Start Keploy in record mode: |
| 93 | + |
| 94 | +```bash |
| 95 | +keploy record --command "python app.py" |
| 96 | +``` |
| 97 | +Send some API requests via curl or Postman to generate test cases. |
| 98 | + |
| 99 | +Step 2: Replay Tests |
| 100 | +After recording, stop the app and run: |
| 101 | + |
| 102 | +```bash |
| 103 | +keploy test --command "python app.py" |
| 104 | +``` |
| 105 | +> Keploy will replay the previously captured requests and validate responses. |
| 106 | +
|
| 107 | +## Folder Structure |
| 108 | + |
| 109 | +```bash |
| 110 | +flask-sqlite/ |
| 111 | +├── app.py # Flask app entry point |
| 112 | +├── models.py # Student model |
| 113 | +├── requirements.txt # Python dependencies |
| 114 | +├── keploy.yml # Keploy config file |
| 115 | +├── keploy/ # Auto-generated test cases |
| 116 | +└── README.md # You are here! |
| 117 | +``` |
| 118 | +--- |
| 119 | + |
| 120 | +## Contributing |
| 121 | +> 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. |
| 122 | +
|
| 123 | +## About Keploy |
| 124 | +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. |
| 125 | + |
| 126 | +> Built with ❤️ for the Open Source Community. |
0 commit comments