You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import InstallReminder from '@site/src/components/InstallReminder';
23
23
import SectionDivider from '@site/src/components/SectionDivider';
24
24
25
-
# Using Docker Compose π³
26
-
27
-
This guide walks you through generating tests and DB mocks for a sample CRUD app built with [FastHttp](https://github.com/valyala/fasthttp) and [Postgres](https://www.postgresql.org) using Keploy.
We will be using Docker Compose to run both the application and Postgres inside Docker containers.
39
-
40
-
### Lights, Camera, Record! π₯
41
-
42
-
Fire up the application and Postgres instance with Keploy. Keep an eye on the two key flags:
43
-
`-c`: Command to run the app (e.g., `docker compose up`).
44
-
45
-
`--container-name`: The container name in the `docker-compose.yml` for traffic interception.
46
-
47
-
```bash
48
-
keploy record -c "docker compose up" --container-name "fasthttpPostgresApp"
49
-
```
50
-
51
-
> Keploy waits for the container to be up before intercepting. If your compose services need extra time to build or initialize, you can add `--build-delay <seconds>` to the command.
52
-
53
-
Getting logs like this? Perfect! π
54
-

55
-
56
-
π₯ Challenge time! Generate some test cases. How? Just **make some API calls**. Postman, Hoppscotch or even curl - take your pick!
57
-
58
-
Let's create some users and books:
59
-
60
-
#### Post Requests
61
-
62
-
```bash
63
-
curl -X POST -H "Content-Type: application/json" -d '{"name":"Author Name"}' http://localhost:8080/authors
64
-
curl -X POST -H "Content-Type: application/json" -d '{"title":"Book Title","author_id":1}' http://localhost:8080/books
65
-
```
66
-
67
-
#### Get Request
68
-
69
-
```bash
70
-
curl -i http://localhost:8080/books
71
-
```
72
-
73
-
π Woohoo! With simple API calls, you've crafted test cases with mocks! Dive into the Keploy directory and feast your eyes on the newly minted `test-1.yml` and `mocks.yml`.
74
-
75
-
Here's a peek of what you get:
76
-
77
-
```yaml
78
-
version: api.keploy.io/v1beta1
79
-
kind: Http
80
-
name: test-1
81
-
spec:
82
-
metadata: {}
83
-
req:
84
-
method: POST
85
-
proto_major: 1
86
-
proto_minor: 1
87
-
url: http://localhost:8080/authors
88
-
header:
89
-
Accept: "*/*"
90
-
Content-Length: "22"
91
-
Content-Type: application/json
92
-
Host: localhost:8080
93
-
User-Agent: curl/7.88.1
94
-
body: '{"name":"Author Name"}'
95
-
timestamp: 2024-06-24T13:05:47.732915734+05:30
96
-
resp:
97
-
status_code: 201
98
-
header:
99
-
Content-Length: "0"
100
-
Date: Mon, 24 Jun 2024 07:35:47 GMT
101
-
Server: Server
102
-
body: ""
103
-
status_message: Created
104
-
proto_major: 0
105
-
proto_minor: 0
106
-
timestamp: 2024-06-24T13:05:49.810554677+05:30
107
-
objects: []
108
-
assertions:
109
-
noise:
110
-
header.Date: []
111
-
created: 1719214549
112
-
curl: |-
113
-
curl --request POST \
114
-
--url http://localhost:8080/authors \
115
-
--header 'Host: localhost:8080' \
116
-
--header 'User-Agent: curl/7.88.1' \
117
-
--header 'Accept: */*' \
118
-
--header 'Content-Type: application/json' \
119
-
--data '{"name":"Author Name"}'
120
-
```
121
-
122
-
This is how the generated **mock.yml** will look like:
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. ππ
This guide walks you through generating tests and DB mocks for a sample CRUD app built with [FastHttp](https://github.com/valyala/fasthttp) and [Postgres](https://www.postgresql.org) using Keploy.
27
+
This guide walks you through generating tests and DB mocks for a sample CRUD app built with FastHttp and Postgres using Keploy.
199
28
200
29
<InstallReminder />
201
30
@@ -208,32 +37,34 @@ go mod download
208
37
209
38
We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (Postgres) chill on Docker. Ready? Let's get the party started! π
210
39
211
-
If youβre using WSL on Windows, start in your home directory:
40
+
#### Point the app to local Postgres
212
41
213
-
```bash
214
-
wsl ~
215
-
```
42
+
Update the Postgres URL to `localhost:5432` in `app.go` (mentioned at line 21 in the sample).
216
43
217
-
###Point the app to local Postgres
44
+
#### Start Postgres
218
45
219
-
Update the Postgres URL to `localhost:5432` in `main.go` (mentioned at line 21 in the sample).
46
+
```bash
47
+
docker compose up postgres
48
+
```
220
49
221
-
###Start Postgres
50
+
#### Record with Keploy while running the app
222
51
223
52
```bash
224
-
docker compose up postgres
53
+
go build -o app
225
54
```
226
55
227
-
### Record with Keploy while running the app
56
+
### Lights, Camera, Record! π₯
228
57
229
58
```bash
230
-
go build -cover
231
59
keploy record -c "./app"
232
60
```
233
61
234
62
Keep an eye out for the `-c` flag! It's the command charm to run the app. Whether you're using `go run main.go` or the binary path like `./app`, it's your call.
63
+
235
64
If you're seeing logs that resemble the ones below, you're on the right track:
π Look at you go! With a few simple API calls, you've crafted test cases with mocks! Peek into the Keploy directory and behold the freshly minted `test-1.yml` and `mocks.yml`.
93
+
Look at you go! With a few simple API calls, you've crafted test cases with mocks! Peek into the Keploy directory and behold the freshly minted `test-1.yml` and `mocks.yml`.
259
94
260
95
### πββοΈ Run the Tests!
261
96
@@ -269,14 +104,12 @@ keploy test -c "./app" --delay 5
269
104
270
105
When all is said and done, your test results should look a little something like this:
Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the `mocks.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold!
275
110
276
111
### Wrapping it up π
277
112
278
-
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. ππ
113
+
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible
Copy file name to clipboardExpand all lines: versioned_docs/version-3.0.0/quickstart/go-mux-mysql.md
+10-17Lines changed: 10 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ keyword:
23
23
import InstallReminder from '@site/src/components/InstallReminder';
24
24
import SectionDivider from '@site/src/components/SectionDivider';
25
25
26
-
# Using Docker Compose π³
26
+
# Using Docker π³
27
27
28
28
A sample url shortener app to test Keploy integration capabilities using Mux and MySQL.
29
29
@@ -44,12 +44,6 @@ We will be using Docker compose to run the application as well as MySQL on Docke
44
44
docker run -p 3306:3306 --rm --name mysql --network keploy-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
45
45
```
46
46
47
-
#### Create Docker volume (optional)
48
-
49
-
```bash
50
-
docker volume create --driver local --opt type=debugfs --opt device=debugfs debugfs
51
-
```
52
-
53
47
### Capture the Testcases
54
48
55
49
Build the Docker image of our application:
@@ -94,7 +88,7 @@ this will return the shortened url. The ts would automatically be ignored during
94
88
#### Access all shortened URLs
95
89
96
90
```bash
97
-
curl --request GET --url http://localhost:8080/all
91
+
curl http://localhost:8080/all
98
92
```
99
93
100
94
Now both these API calls were captured as **editable** testcases and written to `keploy/tests` folder. The keploy directory would also have `mocks` file that contains all the outputs of MySQL operations. Here's what the folder structure look like:
@@ -140,10 +134,7 @@ git clone https://github.com/keploy/samples-go.git && cd samples-go/mux-mysql
140
134
go mod download
141
135
```
142
136
143
-
We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (MySQL) chill on Docker. Ready? Let's get the party started!π
144
-
145
-
> To establish a network for your application using Keploy on Docker, follow these steps.
146
-
> If you're using a docker-compose network, replace keploy-network with your app's `docker_compose_network_name` below.
137
+
Weβll run our sample application locally, with the database running inside a Docker container. Ready? Letβs get the party started! π
@@ -195,7 +186,9 @@ this will return the shortened url.
195
186
#### Redirect to original url from shortened url
196
187
197
188
```zsh
198
-
curl -request GET localhost:8080/links/1
189
+
190
+
curl http://localhost:8080/links/1
191
+
199
192
```
200
193
201
194
Now, let's see the magic! πͺπ« Both these API calls were captured as a test case and should be visible on the Keploy CLI. You should see a `keploy` folder with the test cases and data mocks created.
Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.ππ
219
-
220
-
Hope this helps you out, if you still have any questions, reach out to us .
0 commit comments