Skip to content

Golang RESTAPI Testing. Sample project using Gin, PGX, Postgresql, Testifty, and pgxmock

License

Notifications You must be signed in to change notification settings

reshimahendra/pgx-test-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang GIN and PGX Test example

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.

Package used in this test project:

Runing Test

Open your terminal bash, zsh, etc

This will run test via terminal

make test

This will show the test with cover in browser

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

Running Local Server

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

Playing with api operation (using curl):

# 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]"}

Build Application

make build

LICENSE

MIT

Releases

No releases published

Packages

No packages published