id | title | sidebar_label | tags | ||||
---|---|---|---|---|---|---|---|
users-profile-sample-application |
Users Profile Sample Application |
Gin-Mongo Sample 2 |
|
A sample application that get, create, update, and delete the data of a user in the database (MongoDB for this application).
- Go 1.16 or later
- Docker for running Keploy server
- Thunder Client / Postman Desktop Agent for testing localhost APIs
- Code Editor (VSCode, Sublime Text, etc.)
git clone https://github.com/keploy/keploy.git
cd keploy
docker-compose up
git clone https://github.com/keploy/samples-go
cd samples-go
cd users-profile
go get .
go run .
Sample Application Port: http://localhost:8080
/user
: POST - Create a new user in the database/user/:userId
: GET - Get a user from the database/user/:userId
: PUT - Edit an existing user in the database/user/:userId
: DELETE - Delete an existing user from the database/users
: GET - Get all users from the database
Keploy Port: http://localhost:8081/testlist
To generate Test Cases, you need to make some API calls. It could be using Thunder Client, Postman Desktop Agent, or your preferred API testing tool.
Once done, you can see the Test Cases on the Keploy server, like this:
To check the actual data being changed in the database. Open MongoDB Compass and enter the URI below to check the data.
mongodb+srv://littleironical:[email protected]/?retryWrites=true&w=majority
NOTE: This is just a testing URI. Do not use it for public use. If you want, you can create your own MongoDB and add data to it.
The current URI might or might not work. It's better to setup your own MongoDB and connect it with the application. To do that, check the Setup your MongoDB section below.
To generate Test Runs, close the application and run the below command:
go test -coverpkg=./... -covermode=atomic ./...
test run completed
{"run id": "0e5f88eb-2746-40de-ab58-c0864ef869ee", "passed overall": false}
--- FAIL: TestKeploy (6.70s)
keploy.go:44: Keploy test suite failed
FAIL
coverage: 100.0% of statements
FAIL users-profile 8.489s
? users-profile/configs [no test files]
? users-profile/controllers [no test files]
? users-profile/models [no test files]
? users-profile/responses [no test files]
? users-profile/routes [no test files]
FAIL
Once done, you can see the Test Runs on the Keploy server, like this:
Start with creating an account on MongoDB and follow the below steps:
Open the Project dropdown in the top-left corner and press the New Project button
Name your project and hit Next
If you wish to add members, you can add their email address, then press the Create Project button
Click on Build a Database button
Choose the Shared plan and click Continue
Click Create Cluster, you can continue with the default cluster setup
Now, you have to create a user and setup network controls to access the database.
First, add a username and create a password, then hit Create User
Then you have to add the IP addresses with which you want to connect the cluster. You can continue by clicking on Add My Current IP Address then Finish and Close
Alternatively, you can add
0.0.0.0
to let any IP address access the cluster.
Click on the Connect button
Choose the Connect your application option
Next, you have to select your driver and version.
In this sample application, we are using Go driver with 1.6 or later version. Once, you've entered the driver and version, copy the URI provided and close the popup.
Open the sample application in the code editor and open .env file
Create a variable MONGO-DB-URI
in that file and paste the URI like this:
MONGO-DB-URI=mongodb+srv://littleironical:[email protected]/?retryWrites=true&w=majority
To check the database, on MongoDB Compass, use this URI if you've setup your own MongoDB instead of the provided once.