Skip to content

Commit

Permalink
added mockdb api testing
Browse files Browse the repository at this point in the history
  • Loading branch information
prosenjitjoy committed Sep 18, 2023
1 parent 6dea034 commit b5ee889
Show file tree
Hide file tree
Showing 19 changed files with 1,078 additions and 569 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ DB_USER=postgres
DB_PASS=postgres
DATABASE_URL=postgres://postgres:postgres@localhost:5432/bankdb
MIGRATE_URL=pgx5://postgres:postgres@localhost:5432/bankdb
SERVER_ADDR = localhost:8000
SERVER_ADDR=localhost:5000
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ delete_database:
open_database:
podman exec -it ${CONTAINER_NAME} psql -U ${DB_USER} -d ${DB_NAME}

create_migrations:
create_migration:
migrate create -ext sql -dir database/migration -seq create_tables

migrate_up:
Expand All @@ -24,10 +24,13 @@ migrate_down:
sqlc_generate:
sqlc generate

mock_generate:
mockgen -package mockdb -destination database/mock/store.go main/database/db Store

run_test:
go test -v -cover ./...

run_server:
go run main.go

.PHONY: create_container create_database delete_database open_database migrate_up migrate_down sqlc_generate run_test run_server
.PHONY: create_container create_database delete_database open_database create_migration migrate_up migrate_down sqlc_generate mock_generate run_test run_server
40 changes: 20 additions & 20 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

type createAccountRequest struct {
Owner string `json:"owner" binding:"required"`
Currency string `json:"currency" binding:"required,oneof=USD EUR"`
Currency string `json:"currency" binding:"required,oneof=USD EUR CAD"`
}

func (s *Server) createAccount(ctx *gin.Context) {
var req createAccountRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

Expand All @@ -28,11 +28,11 @@ func (s *Server) createAccount(ctx *gin.Context) {

account, err := s.store.CreateAccount(ctx, &arg)
if err != nil {
ctx.IndentedJSON(http.StatusInternalServerError, errorResponse(err))
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusOK, account)
ctx.JSON(http.StatusOK, account)
}

type getAccountRequest struct {
Expand All @@ -42,22 +42,22 @@ type getAccountRequest struct {
func (s *Server) getAcount(ctx *gin.Context) {
var req getAccountRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

account, err := s.store.GetAccount(ctx, req.ID)
if err != nil {
if err == pgx.ErrNoRows {
ctx.IndentedJSON(http.StatusNotFound, errorResponse(err))
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusInternalServerError, errorResponse(err))
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusOK, account)
ctx.JSON(http.StatusOK, account)
}

type listAccountRequest struct {
Expand All @@ -68,7 +68,7 @@ type listAccountRequest struct {
func (s *Server) listAcount(ctx *gin.Context) {
var req listAccountRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

Expand All @@ -79,11 +79,11 @@ func (s *Server) listAcount(ctx *gin.Context) {

accounts, err := s.store.ListAccounts(ctx, arg)
if err != nil {
ctx.IndentedJSON(http.StatusInternalServerError, errorResponse(err))
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusOK, accounts)
ctx.JSON(http.StatusOK, accounts)
}

type updateAccountURI struct {
Expand All @@ -97,13 +97,13 @@ type updateAccountJSON struct {
func (s *Server) updateAccount(ctx *gin.Context) {
var uri updateAccountURI
if err := ctx.ShouldBindUri(&uri); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

var req updateAccountJSON
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

Expand All @@ -115,15 +115,15 @@ func (s *Server) updateAccount(ctx *gin.Context) {
account, err := s.store.UpdateAccount(ctx, arg)
if err != nil {
if err == pgx.ErrNoRows {
ctx.IndentedJSON(http.StatusNotFound, errorResponse(err))
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusInternalServerError, errorResponse(err))
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusOK, account)
ctx.JSON(http.StatusOK, account)
}

type deleteAccountRequest struct {
Expand All @@ -133,19 +133,19 @@ type deleteAccountRequest struct {
func (s *Server) deleteAccount(ctx *gin.Context) {
var req deleteAccountRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.IndentedJSON(http.StatusBadRequest, errorResponse(err))
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

err := s.store.DeleteAccount(ctx, req.ID)
if err != nil {
if err == pgx.ErrNoRows {
ctx.IndentedJSON(http.StatusNotFound, errorResponse(err))
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
ctx.IndentedJSON(http.StatusInternalServerError, errorResponse(err))
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.IndentedJSON(http.StatusOK, gin.H{"msg": "successfully deleted"})
ctx.JSON(http.StatusOK, gin.H{"msg": "successfully deleted"})
}
Loading

0 comments on commit b5ee889

Please sign in to comment.