Skip to content

Commit 442f9e2

Browse files
authored
feat: created a sample app using flask and redis (keploy#26)
* feat: Created a sample app using flask and redis Signed-off-by: Aman172003 <tej171995@gmail.com> * removed errors Signed-off-by: Aman172003 <tej171995@gmail.com> --------- Signed-off-by: Aman172003 <tej171995@gmail.com>
1 parent b043a93 commit 442f9e2

File tree

19 files changed

+1777
-0
lines changed

19 files changed

+1777
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ This repo contains the samples for [Keploy's](https://keploy.io) integration wit
2525

2626
4. [FastAPI-Twilio](https://github.com/keploy/samples-python/tree/main/fastapi-twilio) - This application is a SMS sending API built using Python's FastAPI and Twilio for their SMS sharing service.
2727

28+
5. [Flask-Redis](https://github.com/keploy/samples-python/tree/main/flask-redis) - This Flask-based application provides a book management system utilizing Redis for caching and storage. It supports adding, retrieving, updating, and deleting book records, with optimized search functionality and cache management for improved performance. The API endpoints ensure efficient data handling and quick access to book information.
29+
2830
## Community Support ❤️
2931

3032
### 🤔 Questions?

flask-redis/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Use the official Python image from the Docker Hub
2+
FROM python:3.10-slim
3+
4+
# RUN apt-get update && apt-get install -y curl
5+
# Set the working directory in the container
6+
WORKDIR /app
7+
8+
# Copy the requirements file into the container
9+
COPY requirements.txt .
10+
11+
# Install the Python dependencies
12+
RUN pip install --no-cache-dir -r requirements.txt
13+
14+
# Copy the rest of the application code into the container
15+
COPY . .
16+
17+
# Download Keploy CA certificate and setup script
18+
# RUN curl -o ca.crt https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/ca.crt && \
19+
# curl -o setup_ca.sh https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/setup_ca.sh && \
20+
# chmod +x setup_ca.sh # Make the setup script executable
21+
22+
# Expose the port the app runs on
23+
EXPOSE 5000
24+
25+
# Define the command to run the app
26+
CMD ["python", "app.py"]

flask-redis/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# flask-redis
2+
3+
A sample App using flask and redis
4+
5+
## Setup application
6+
7+
1. Clone the repository and move to flask-redis folder
8+
2. Create a .env file and copy-paste below credentials:
9+
10+
```bash
11+
REDIS_HOST=redis
12+
REDIS_PORT=6379
13+
```
14+
15+
# Installing Redis
16+
17+
```sh
18+
brew install redis
19+
```
20+
If homebrew is not installed, then go to https://brew.sh/ and install it.
21+
22+
```bash
23+
git clone https://github.com/keploy/samples-typescript && cd samples-typescript/flask-redis
24+
25+
# Install the dependencies
26+
pip3 install -r requirements.txt
27+
```
28+
29+
# Installing Keploy
30+
31+
Let's get started by setting up the Keploy alias with this command:
32+
33+
```sh
34+
curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
35+
```
36+
37+
## Using Keploy :
38+
39+
There are 2 ways you can run this sample application.
40+
41+
1. [Natively on Linux/WSL](#natively-on-ubuntuwsl)
42+
2. [Using Docker](#running-sample-app-using-docker)
43+
44+
# Natively on Ubuntu/WSL
45+
46+
## Let's install certificates
47+
48+
1. **Install required packages:**
49+
50+
```sh
51+
sudo apt-get install -y --no-install-recommends ca-certificates curl
52+
```
53+
54+
This command installs necessary packages without additional recommended packages.
55+
56+
2. **Download CA certificate:**
57+
58+
```sh
59+
curl -o ca.crt https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/ca.crt
60+
```
61+
62+
This command downloads the CA certificate to `ca.crt`.
63+
64+
3. **Download setup script:**
65+
66+
```sh
67+
curl -o setup_ca.sh https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/setup_ca.sh
68+
```
69+
70+
This command downloads the setup script to `setup_ca.sh`.
71+
72+
4. **Make the setup script executable:**
73+
74+
```sh
75+
chmod +x setup_ca.sh
76+
```
77+
78+
This command changes the permissions of `setup_ca.sh` to make it executable.
79+
80+
5. **Run the setup script:**
81+
82+
```sh
83+
source ./setup_ca.sh
84+
```
85+
86+
This command executes the setup script in the current shell.
87+
88+
6. **Start the redis server:**
89+
```sh
90+
redis-server
91+
```
92+
This command starts the redis server.
93+
94+
## Capture the test cases
95+
96+
1. **Start recording tests:**
97+
```bash
98+
sudo -E env "PATH=$PATH" keploybin record -c 'python3 app.py'
99+
```
100+
101+
## Let's Generate the test cases
102+
103+
Make API Calls using Hoppscotch, Postman or cURL command. Keploy will capture those calls to generate test suites containing test cases and data mocks.
104+
105+
1. Refer to flask-redis/api.txt to make api calls.
106+
107+
2. **Observe terminal output:**
108+
Let's go ahead and create a few more test cases for different endpoints!
109+
110+
## Running the test cases
111+
112+
1. **Start the application:**
113+
114+
```bash
115+
python3 app.py
116+
```
117+
118+
2. **Run the recorded tests:**
119+
120+
```bash
121+
sudo -E env "PATH=$PATH" keploybin test -c 'python3 app.py' --delay 10
122+
```
123+
124+
3. **Observe test run results:**
125+
_Voila!! Our test cases have passed 🌟_
126+
127+
---
128+
129+
# Using Docker
130+
131+
Since we have to setup our app using docker(make sure your docker is running)
132+
133+
## Create a custom network for Keploy since we are using the Docker
134+
135+
```bash
136+
docker network create keploy-network
137+
```
138+
139+
## Capture the testcases
140+
141+
We will run the keploy in record mode with docker-compose to start our application:-
142+
143+
```bash
144+
keploy record -c "sudo docker-compose up" --containerName "flask-web"
145+
146+
```
147+
148+
#### Let's generate the testcases.
149+
150+
Make API Calls using [Hoppscotch](https://hoppscotch.io), [Postman](https://postman.com) or curl command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.
151+
152+
1. Refer to flask-redis/api.txt to make api calls
153+
154+
## Running the testcases
155+
156+
```bash
157+
keploy test -c 'sudo docker-compose up' --containerName "flask-web" --delay 10
158+
```
159+
160+
_Voila!! Our testcases has passed 🌟_

flask-redis/api.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---------------------------------------------------------------------------------------------------
2+
// Add a Book
3+
4+
curl -X POST http://localhost:5000/books/ \
5+
-H "Content-Type: application/json" \
6+
-d '{"title": "1984", "author": "George Orwell"}'
7+
8+
---------------------------------------------------------------------------------------------------
9+
10+
// Get All Books(with Pagination)
11+
12+
curl -X GET "http://localhost:5000/books/?page=1&limit=10"
13+
14+
---------------------------------------------------------------------------------------------------
15+
16+
// Get Book Details
17+
18+
curl -X GET http://localhost:5000/books/1
19+
20+
---------------------------------------------------------------------------------------------------
21+
22+
23+
//Search Books by Title
24+
25+
curl -X GET "http://localhost:5000/books/search?query=1984"
26+
27+
28+
---------------------------------------------------------------------------------------------------
29+
30+
//Update a Book
31+
32+
curl -X PUT http://localhost:5000/books/1 \
33+
-H "Content-Type: application/json" \
34+
-d '{"title": "1984 - Updated", "author": "George Orwell"}'
35+
36+
37+
---------------------------------------------------------------------------------------------------
38+
39+
//Delete a Book
40+
41+
curl -X DELETE http://localhost:5000/books/1
42+
43+
---------------------------------------------------------------------------------------------------
44+

flask-redis/app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from flask import Flask
2+
from routes.book_routes import book
3+
4+
app = Flask(__name__)
5+
6+
# Register Blueprints
7+
app.register_blueprint(book, url_prefix="/books")
8+
9+
@app.route('/', methods=['GET'])
10+
def hello():
11+
return {"message": "Welcome to the Book Management System!"}, 200
12+
13+
if __name__ == '__main__':
14+
app.run(host='0.0.0.0', port=5000, debug=True)

flask-redis/docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: "3.8"
2+
3+
services:
4+
web:
5+
build: .
6+
container_name: flask-web
7+
restart: always
8+
ports:
9+
- "5000:5000"
10+
depends_on:
11+
- redis
12+
environment:
13+
- FLASK_ENV=development
14+
- REDIS_HOST=redis
15+
- REDIS_PORT=6379
16+
# volumes:
17+
# - .:/usr/src/app
18+
networks:
19+
- keploy-network
20+
21+
redis:
22+
image: "redis:alpine"
23+
container_name: redis-server
24+
restart: always
25+
ports:
26+
- "6379:6379"
27+
# volumes:
28+
# - db_data:/var/lib/redis
29+
networks:
30+
- keploy-network
31+
32+
networks:
33+
keploy-network:
34+
external: true
35+
# volumes:
36+
# db_data:

flask-redis/dump.rdb

201 Bytes
Binary file not shown.

flask-redis/keploy.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
path: ""
2+
appId: 0
3+
appName: ""
4+
command: sudo docker-compose up
5+
port: 0
6+
dnsPort: 26789
7+
proxyPort: 16789
8+
debug: false
9+
disableTele: false
10+
disableANSI: false
11+
containerName: flask-web
12+
networkName: ""
13+
buildDelay: 30
14+
test:
15+
selectedTests: {}
16+
globalNoise:
17+
global: {}
18+
test-sets: {}
19+
delay: 5
20+
apiTimeout: 5
21+
skipCoverage: false
22+
coverageReportPath: ""
23+
ignoreOrdering: true
24+
mongoPassword: default@123
25+
language: ""
26+
removeUnusedMocks: false
27+
fallBackOnMiss: false
28+
jacocoAgentPath: ""
29+
basePath: ""
30+
mocking: true
31+
ignoredTests: {}
32+
record:
33+
filters: []
34+
recordTimer: 0s
35+
configPath: ""
36+
bypassRules: []
37+
generateGithubActions: false
38+
keployContainer: keploy-v2
39+
keployNetwork: keploy-network
40+
cmdType: native
41+
inCi: false
42+
43+
# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file.

0 commit comments

Comments
 (0)