This is simple Golang test example using Gin, and pgx driver for PostgreSQL. This simple example project also was a reminder for me in case I need to do same test using similar stack in the future (as for few days ago, i am really stuck testing my project and dig out my brain searching here and there for completing the testing task). Hopefully it will help you too.
Open your terminal bash, zsh, etc
make test
make test-cover
Or if you already have the proof.out
file from previous generated test, and you want to see the coverage profile on browser, run:
make show-test-cover
make run
The local server will be running on http://127.0.0.1:8000
Available api endpoint for the local server:
No | Method | Endpoint | Description |
---|---|---|---|
1 | POST |
/v1/account/ |
Create/ insert new user data |
2 | GET |
/v1/account/:id |
Get data by ID |
3 | GET |
/v1/account/ |
Get all user data |
4 | PUT |
/v1/account/:id |
Update user data based on its ID |
5 | DELETE |
/v1/account/:id |
Delete user data based on its ID |
# POST/ create new user data
curl http://127.0.0.1:8000/v1/account/ -X POST -H 'content-type: application/json' \
--data '{"firstname":"john","lastname":"doe","email":"[email protected]","passkey":"secret"}'
# Server response
# {"id":1,"first_name":"john","last_name":"doe","email":"[email protected]"}
curl http://127.0.0.1:8000/v1/account/ -X POST -H 'content-type: application/json' \
--data '{"firstname":"janne","lastname":"doe","email":"[email protected]","passkey":"secret"}'
# Server response
# {"id":2,"first_name":"janne","last_name":"doe","email":"[email protected]"}
curl http://127.0.0.1:8000/v1/account/ -X POST -H 'content-type: application/json' \
--data '{"firstname":"donny","lastname":"trumpy","email":"[email protected]","passkey":"secret"}'
# Server response
# {"id":3,"firstname":"donny","lastname":"trumpy","email":"[email protected]"}
# UPDATE DATA
curl http://127.0.0.1:8000/v1/account/2 -X PUT -H 'content-type: application/json' \
--data '{"id":2,"firstname":"janne","lastname":"sweety","email":"[email protected]","passkey":"secret"}'
# Server response
# {"id":2,"firstname":"janne","lastname":"sweety","email":"[email protected]"}
# GET DATA by ID
curl http://127.0.0.1:8000/v1/account/2
# Server response
# {"id":2,"first_name":"janne","last_name":"sweety","email":"[email protected]"}
curl http://127.0.0.1:8000/v1/account/1
# Server response
# {"id":1,"first_name":"john","last_name":"doe","email":"[email protected]"}
# GET ALL DATA
curl http://127.0.0.1:8000/v1/account/
# Server response
#[{"id":1,"first_name":"john","last_name":"doe","email":"[email protected]"},{"id":2,"first_name":"janne","last_name":"sweety","email":"[email protected]"},{"id":3,"firstname":"donny","lastname":"trumpy","email":"[email protected]"}]
# DELETE DATA
curl http://127.0.0.1:8000/v1/account/1 -X DELETE
# Server response
# {"id":1,"first_name":"john","last_name":"doe","email":"[email protected]"}
make build