Skip to content

Commit ea98b7c

Browse files
Refactor user test
1 parent 93c8872 commit ea98b7c

File tree

9 files changed

+97
-31
lines changed

9 files changed

+97
-31
lines changed

Taskfile.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ tasks:
5353
test:
5454
cmds:
5555
- (rm /tmp/unit_coverage.out || echo "Deleted Old files")
56-
- go test -mod=mod -v -coverprofile /tmp/unit_coverage.out ./...
56+
- go test -test.v ./...

cmd/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package main
22

33
import (
4-
router "github.com/pauljamescleary/gomin/pkg/common"
4+
"flag"
5+
"os"
6+
7+
"github.com/pauljamescleary/gomin/pkg/common/handler"
8+
router "github.com/pauljamescleary/gomin/pkg/common/router"
9+
"github.com/rs/zerolog/log"
510
)
611

712
func main() {
8-
e := router.SetupRouter()
13+
configPath := flag.String("configpath", "", "Config Path")
14+
flag.Parse()
15+
if configPath == nil || len(*configPath) == 0 {
16+
log.Fatal().Msgf("Unable to load config path. Empty Path specified. %s", *configPath)
17+
}
18+
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
19+
// path/to/whatever does not exist
20+
log.Fatal().Msgf("Unable to load config path. Path not found. %s", *configPath)
21+
}
22+
h := handler.LoadHandler(configPath)
23+
e := router.SetupRouter(h)
924

1025
// Start server
1126
e.Logger.Fatal(e.Start(":1323"))

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ require (
88
github.com/labstack/echo/v4 v4.10.1
99
github.com/rs/zerolog v1.29.0
1010
github.com/spf13/viper v1.15.0
11+
github.com/stretchr/testify v1.8.2
1112
)
1213

1314
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/fsnotify/fsnotify v1.6.0 // indirect
1517
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
1618
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -23,6 +25,7 @@ require (
2325
github.com/mattn/go-isatty v0.0.17 // indirect
2426
github.com/mitchellh/mapstructure v1.5.0 // indirect
2527
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
28+
github.com/pmezard/go-difflib v1.0.0 // indirect
2629
github.com/spf13/afero v1.9.3 // indirect
2730
github.com/spf13/cast v1.5.0 // indirect
2831
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
193193
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
194194
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
195195
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
196-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
197196
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
197+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
198+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
198199
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
199200
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
200201
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

pkg/common/config/config.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package config
22

33
import (
4-
"flag"
5-
"os"
6-
74
"github.com/rs/zerolog/log"
85
"github.com/spf13/viper"
96
)
@@ -13,18 +10,7 @@ type Config struct {
1310
DbUrl string `mapstructure:"DB_URL"`
1411
}
1512

16-
func LoadConfig() (c Config, err error) {
17-
// Assumes a configpath Flag passed into our application
18-
configPath := flag.String("configpath", "", "Config Path")
19-
flag.Parse()
20-
if configPath == nil || len(*configPath) == 0 {
21-
log.Fatal().Msgf("Unable to load config path. Empty Path specified. ")
22-
}
23-
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
24-
// path/to/whatever does not exist
25-
log.Fatal().Msgf("Unable to load config path. Path not found. ")
26-
}
27-
13+
func LoadConfig(configPath *string) (c Config, err error) {
2814
// Actually load in the config file from the path provided
2915
viper.SetConfigFile(*configPath)
3016
viper.SetConfigType("yaml")

pkg/common/handler/handler.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handler
22

33
import (
4+
"fmt"
5+
6+
"github.com/pauljamescleary/gomin/pkg/common/config"
47
"github.com/pauljamescleary/gomin/pkg/common/db"
58
)
69

@@ -11,3 +14,18 @@ type Handler struct {
1114
func NewHandler(ur db.UserRepository) *Handler {
1215
return &Handler{UserRepo: ur}
1316
}
17+
18+
func LoadHandler(configPath *string) *Handler {
19+
cfg, _ := config.LoadConfig(configPath)
20+
return LoadHandlerFromConfig(cfg)
21+
}
22+
23+
func LoadHandlerFromConfig(cfg config.Config) *Handler {
24+
fmt.Printf("*** DB URL %s", cfg.DbUrl)
25+
26+
database := db.NewDatabase(cfg)
27+
userRepo, _ := db.NewUserRepository(database)
28+
handler := NewHandler(userRepo)
29+
30+
return handler
31+
}
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
package router
22

33
import (
4-
"fmt"
5-
6-
"github.com/pauljamescleary/gomin/pkg/common/config"
7-
"github.com/pauljamescleary/gomin/pkg/common/db"
84
"github.com/pauljamescleary/gomin/pkg/common/handler"
95

106
"github.com/labstack/echo/v4"
117
"github.com/labstack/echo/v4/middleware"
128
)
139

14-
func SetupRouter() *echo.Echo {
10+
func SetupRouter(handler *handler.Handler) *echo.Echo {
1511
e := echo.New()
1612

1713
// Middleware
1814
e.Use(middleware.Logger())
1915
e.Use(middleware.Recover())
2016

21-
cfg, _ := config.LoadConfig()
22-
fmt.Printf("*** DB URL %s", cfg.DbUrl)
23-
24-
database := db.NewDatabase(cfg)
25-
userRepo, _ := db.NewUserRepository(database)
26-
handler := handler.NewHandler(userRepo)
27-
2817
// Routes
2918
// e.GET("/users", api.GetAllUsers)
3019
e.POST("/users", handler.CreateUser)

pkg/common/router/router_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package router
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
9+
"github.com/labstack/echo/v4"
10+
"github.com/pauljamescleary/gomin/pkg/common/config"
11+
"github.com/pauljamescleary/gomin/pkg/common/handler"
12+
)
13+
14+
var testEchoContext *echo.Echo
15+
var testHandler *handler.Handler
16+
var testConfig *config.Config
17+
18+
func init() {
19+
testConfig = &config.Config{
20+
Port: 1323,
21+
DbUrl: "postgres://test:test@localhost:5435/gomin",
22+
}
23+
testHandler = handler.LoadHandlerFromConfig(*testConfig)
24+
testEchoContext = SetupRouter(testHandler)
25+
}
26+
27+
func makeRequest(method, url string, body interface{}) (*http.Request, *httptest.ResponseRecorder) {
28+
requestBody, _ := json.Marshal(body)
29+
req := httptest.NewRequest(method, url, bytes.NewBuffer(requestBody))
30+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
31+
rec := httptest.NewRecorder()
32+
33+
return req, rec
34+
}

pkg/common/router/user_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package router
2+
3+
import (
4+
"net/http"
5+
"os/user"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCreateUser(t *testing.T) {
12+
u := user.User{Name: "test1"}
13+
req, rec := makeRequest(http.MethodPost, "/users", u)
14+
c := testEchoContext.NewContext(req, rec)
15+
16+
// Assertions
17+
if assert.NoError(t, testHandler.CreateUser(c)) {
18+
assert.Equal(t, http.StatusCreated, rec.Code)
19+
}
20+
}

0 commit comments

Comments
 (0)