Skip to content

Commit a021b57

Browse files
Add Get User test
1 parent ea98b7c commit a021b57

File tree

7 files changed

+87
-3
lines changed

7 files changed

+87
-3
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module github.com/pauljamescleary/gomin
33
go 1.20
44

55
require (
6+
github.com/georgysavva/scany/v2 v2.0.0
67
github.com/google/uuid v1.3.0
8+
github.com/jackc/pgx-gofrs-uuid v0.0.0-20230224015001-1d428863c2e2
79
github.com/jackc/pgx/v5 v5.3.0
810
github.com/labstack/echo/v4 v4.10.1
911
github.com/rs/zerolog v1.29.0
@@ -14,6 +16,7 @@ require (
1416
require (
1517
github.com/davecgh/go-spew v1.1.1 // indirect
1618
github.com/fsnotify/fsnotify v1.6.0 // indirect
19+
github.com/gofrs/uuid/v5 v5.0.0 // indirect
1720
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
1821
github.com/hashicorp/hcl v1.0.0 // indirect
1922
github.com/jackc/pgpassfile v1.0.0 // indirect

go.sum

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
4646
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
4747
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
4848
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
49+
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
4950
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
5051
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5152
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -59,10 +60,15 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
5960
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
6061
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
6162
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
63+
github.com/georgysavva/scany/v2 v2.0.0 h1:RGXqxDv4row7/FYoK8MRXAZXqoWF/NM+NP0q50k3DKU=
64+
github.com/georgysavva/scany/v2 v2.0.0/go.mod h1:sigOdh+0qb/+aOs3TVhehVT10p8qJL7K/Zhyz8vWo38=
6265
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6366
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6467
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6568
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
69+
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
70+
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
71+
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
6672
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
6773
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
6874
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@@ -132,6 +138,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
132138
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
133139
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
134140
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
141+
github.com/jackc/pgx-gofrs-uuid v0.0.0-20230224015001-1d428863c2e2 h1:QWdhlQz98hUe1xmjADOl2mr8ERLrOqj0KWLdkrnNsRQ=
142+
github.com/jackc/pgx-gofrs-uuid v0.0.0-20230224015001-1d428863c2e2/go.mod h1:Ti7pyNDU/UpXKmBTeFgxTvzYDM9xHLiYKMsLdt4b9cg=
135143
github.com/jackc/pgx/v5 v5.3.0 h1:/NQi8KHMpKWHInxXesC8yD4DhkXPrVhmnwYkjp9AmBA=
136144
github.com/jackc/pgx/v5 v5.3.0/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
137145
github.com/jackc/puddle/v2 v2.2.0 h1:RdcDk92EJBuBS55nQMMYFXTxwstHug4jkhT5pq8VxPk=
@@ -149,6 +157,7 @@ github.com/labstack/echo/v4 v4.10.1 h1:rB+D8In9PWjsp1OpHaqK+t04nQv/SBD1IoIcXCg0l
149157
github.com/labstack/echo/v4 v4.10.1/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
150158
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
151159
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
160+
github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E=
152161
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
153162
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
154163
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
@@ -163,13 +172,14 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
163172
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
164173
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
165174
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
175+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
166176
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
167177
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
168178
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
169179
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
170180
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
171181
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
172-
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
182+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
173183
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
174184
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
175185
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
@@ -185,6 +195,7 @@ github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
185195
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
186196
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
187197
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
198+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
188199
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
189200
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
190201
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

pkg/common/db/db.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package db
33
import (
44
"context"
55

6+
pgxuuid "github.com/jackc/pgx-gofrs-uuid"
7+
"github.com/jackc/pgx/v5"
68
"github.com/jackc/pgx/v5/pgxpool"
79
"github.com/pauljamescleary/gomin/pkg/common/config"
810
"github.com/rs/zerolog/log"
@@ -24,6 +26,13 @@ func NewPgConnectionPool(c config.Config) (*pgxpool.Pool, error) {
2426
if err != nil {
2527
return nil, err
2628
}
29+
30+
// Register UUID support
31+
dbConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
32+
pgxuuid.Register(conn.TypeMap())
33+
return nil
34+
}
35+
2736
conn, err := pgxpool.NewWithConfig(context.Background(), dbConfig)
2837
if err != nil {
2938
return nil, err

pkg/common/db/user.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package db
33
import (
44
"context"
55

6+
"github.com/georgysavva/scany/v2/pgxscan"
67
"github.com/pauljamescleary/gomin/pkg/common/models"
78
)
89

910
type UserRepository interface {
1011
CreateUser(user *models.User) (*models.User, error)
12+
GetUser(id string) (*models.User, error)
1113
}
1214

1315
type PostgresUserRepository struct {
@@ -26,6 +28,25 @@ func (repo PostgresUserRepository) CreateUser(user *models.User) (*models.User,
2628
return user, nil
2729
}
2830

31+
func (repo PostgresUserRepository) GetUser(id string) (*models.User, error) {
32+
sql := `
33+
SELECT id, name
34+
FROM users
35+
WHERE id = $1
36+
`
37+
var user models.User
38+
rows, err := repo.db.Conn.Query(context.Background(), sql, id)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
if err := pgxscan.ScanOne(&user, rows); err != nil {
44+
panic(err)
45+
}
46+
47+
return &user, nil
48+
}
49+
2950
func NewUserRepository(db *Database) (*PostgresUserRepository, error) {
3051
return &PostgresUserRepository{db: db}, nil
3152
}

pkg/common/handler/user.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ func (h *Handler) CreateUser(c echo.Context) error {
2121
}
2222
return c.JSON(http.StatusCreated, newUser)
2323
}
24+
25+
func (h *Handler) GetUser(c echo.Context) error {
26+
id := c.Param("id")
27+
user, err := h.UserRepo.GetUser(id)
28+
29+
if err != nil {
30+
return c.JSON(http.StatusInternalServerError, err.Error())
31+
}
32+
if user == nil {
33+
return c.JSON(http.StatusNotFound, id)
34+
}
35+
36+
return c.JSON(http.StatusOK, user)
37+
}

pkg/common/router/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func SetupRouter(handler *handler.Handler) *echo.Echo {
1717
// Routes
1818
// e.GET("/users", api.GetAllUsers)
1919
e.POST("/users", handler.CreateUser)
20-
// e.GET("/users/:id", api.GetUser)
20+
e.GET("/users/:id", handler.GetUser)
2121
// e.PUT("/users/:id", api.UpdateUser)
2222
// e.DELETE("/users/:id", api.DeleteUser)
2323
return e

pkg/common/router/user_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package router
22

33
import (
4+
"encoding/json"
5+
"io"
46
"net/http"
57
"os/user"
68
"testing"
79

10+
"github.com/pauljamescleary/gomin/pkg/common/models"
811
"github.com/stretchr/testify/assert"
912
)
1013

@@ -13,8 +16,31 @@ func TestCreateUser(t *testing.T) {
1316
req, rec := makeRequest(http.MethodPost, "/users", u)
1417
c := testEchoContext.NewContext(req, rec)
1518

16-
// Assertions
1719
if assert.NoError(t, testHandler.CreateUser(c)) {
1820
assert.Equal(t, http.StatusCreated, rec.Code)
1921
}
2022
}
23+
24+
func TestGetUser(t *testing.T) {
25+
u := user.User{Name: "test1"}
26+
req, rec := makeRequest(http.MethodPost, "/users", u)
27+
c := testEchoContext.NewContext(req, rec)
28+
29+
if assert.NoError(t, testHandler.CreateUser(c)) {
30+
assert.Equal(t, http.StatusCreated, rec.Code)
31+
}
32+
33+
userBytes, _ := io.ReadAll(rec.Body)
34+
var createdUser *models.User
35+
_ = json.Unmarshal(userBytes, &createdUser)
36+
37+
req, rec = makeRequest(http.MethodGet, "/", nil)
38+
c = testEchoContext.NewContext(req, rec)
39+
c.SetPath("/users/:id")
40+
c.SetParamNames("id")
41+
c.SetParamValues(createdUser.ID.String())
42+
43+
if assert.NoError(t, testHandler.GetUser(c)) {
44+
assert.Equal(t, http.StatusOK, rec.Code)
45+
}
46+
}

0 commit comments

Comments
 (0)