Skip to content

Commit cd57273

Browse files
chore: add migration, e2e, unit and integration tests
1 parent 0f809a7 commit cd57273

File tree

15 files changed

+447
-38
lines changed

15 files changed

+447
-38
lines changed

Makefile

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
IS_IN_PROGRESS = "is in progress ..."
2+
3+
## gen: will generate mock for usecases & repositories interfaces
4+
.PHONY: gen
5+
gen:
6+
@echo "make gen ${IS_IN_PROGRESS}"
7+
@mockgen -source internal/users/usecase.go -destination internal/generated/mocks/usecase_mock.go -package=mocks
8+
@mockgen -source internal/users/repository.go -destination internal/generated/mocks/repository_mock.go -package=mocks
9+
10+
## setup: Set up database temporary for integration testing
11+
.PHONY: setup
12+
setup:
13+
@docker-compose -f ./infrastructure/docker-compose.yml up -d
14+
@sleep 10
15+
16+
## down: Set down database temporary for integration testing
17+
.PHONY: down
18+
down:
19+
@docker-compose -f ./infrastructure/docker-compose.yml down -t 1
20+
21+
## integration-test: will test with integration tags
22+
.PHONY: integration-test
23+
integration-test:
24+
@echo "make integration-test ${IS_IN_PROGRESS}"
25+
@go clean -testcache
26+
@go test --race -timeout=90s -failfast \
27+
-vet= -cover -covermode=atomic -coverprofile=./.coverage/integration.out \
28+
-tags=integration ./internal/users/repository/...\
29+
30+
## unit-test: will test with unit tags
31+
.PHONY: unit-test
32+
unit-test:
33+
@echo "make unit-test ${IS_IN_PROGRESS}"
34+
@go clean -testcache
35+
@go test --race -timeout=90s -failfast \
36+
-vet= -cover -covermode=atomic -coverprofile=./.coverage/unit.out \
37+
-tags=unit ./internal/users/usecase/...\
38+
39+
## e2e-test: will test with e2e tags
40+
.PHONY: e2e-test
41+
e2e-test:
42+
@echo "make e2e-test ${IS_IN_PROGRESS}"
43+
@go clean -testcache
44+
@go test --race -timeout=90s -failfast \
45+
-vet= -cover -covermode=atomic -coverprofile=./.coverage/unit.out \
46+
-tags=e2e ./internal/users/delivery/...\
47+
48+
## tests: run tests and any dependencies
49+
.PHONY: tests
50+
tests: setup e2e-test unit-test integration-test down

cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log"
67

78
"github.com/labstack/echo/v4"
89
"github.com/martinyonatann/go-unit-test/config"
@@ -31,5 +32,5 @@ func main() {
3132

3233
http.MapRoutes(echoServer, handlers)
3334

34-
echoServer.Start(fmt.Sprintf(":%s", cfg.Database.Port))
35+
log.Println(echoServer.Start(fmt.Sprintf(":%s", cfg.Server.Port)))
3536
}

config/config.integration.test.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Server:
55
Database:
66
Host: "127.0.0.1"
77
Port: "3306"
8-
DBName: "go-unit-test"
9-
UserName: "admin"
8+
DBName: "go_unit_test"
9+
UserName: "root"
1010
Password: "pwd"

config/config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Server:
55
Database:
66
Host: "127.0.0.1"
77
Port: "3306"
8-
DBName: "go-unit-test"
9-
UserName: "admin"
8+
DBName: "go_unit_test"
9+
UserName: "root"
1010
Password: "pwd"

database/migrations/001_users.up.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE IF NOT EXISTS users (
2-
id CHAR(36) PRIMARY KEY,
3-
name VARCHAR(255),
4-
password VARCHAR(255),
2+
id CHAR(36) NOT NULL PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL,
4+
password VARCHAR(255) NOT NULL,
55
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
66
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
77
)

go.mod

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,31 @@ go 1.20
55
require (
66
github.com/go-faker/faker/v4 v4.2.0
77
github.com/go-sql-driver/mysql v1.6.0
8-
github.com/golang-migrate/migrate v3.5.4+incompatible
8+
github.com/golang-migrate/migrate/v4 v4.16.2
99
github.com/google/uuid v1.5.0
1010
github.com/invopop/validation v0.3.0
1111
github.com/jmoiron/sqlx v1.3.5
1212
github.com/labstack/echo/v4 v4.11.3
1313
github.com/pkg/errors v0.9.1
1414
github.com/spf13/viper v1.18.1
1515
github.com/stretchr/testify v1.8.4
16+
go.uber.org/mock v0.3.0
1617
)
1718

1819
require (
19-
github.com/Microsoft/go-winio v0.6.1 // indirect
2020
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2121
github.com/distribution/reference v0.5.0 // indirect
2222
github.com/docker/distribution v2.8.3+incompatible // indirect
2323
github.com/docker/docker v24.0.7+incompatible // indirect
24-
github.com/docker/go-connections v0.4.0 // indirect
25-
github.com/docker/go-units v0.5.0 // indirect
2624
github.com/fsnotify/fsnotify v1.7.0 // indirect
25+
github.com/hashicorp/errwrap v1.1.0 // indirect
26+
github.com/hashicorp/go-multierror v1.1.1 // indirect
2727
github.com/hashicorp/hcl v1.0.0 // indirect
2828
github.com/labstack/gommon v0.4.0 // indirect
2929
github.com/magiconair/properties v1.8.7 // indirect
3030
github.com/mattn/go-colorable v0.1.13 // indirect
3131
github.com/mattn/go-isatty v0.0.19 // indirect
3232
github.com/mitchellh/mapstructure v1.5.0 // indirect
33-
github.com/opencontainers/go-digest v1.0.0 // indirect
34-
github.com/opencontainers/image-spec v1.0.2 // indirect
3533
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
3634
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3735
github.com/sagikazarmark/locafero v0.4.0 // indirect

go.sum

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
12
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
2-
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
33
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
44
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
88
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/dhui/dktest v0.3.16 h1:i6gq2YQEtcrjKbeJpBkWjE8MmLZPYllcjOFbTZuPDnw=
910
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
1011
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
1112
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
1213
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
1314
github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM=
1415
github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
1516
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
16-
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
1717
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
18-
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
1918
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
2019
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
2120
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
@@ -24,11 +23,16 @@ github.com/go-faker/faker/v4 v4.2.0/go.mod h1:F/bBy8GH9NxOxMInug5Gx4WYeG6fHJZ8Ol
2423
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
2524
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2625
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
27-
github.com/golang-migrate/migrate v3.5.4+incompatible h1:R7OzwvCJTCgwapPCiX6DyBiu2czIUMDCB118gFTKTUA=
28-
github.com/golang-migrate/migrate v3.5.4+incompatible/go.mod h1:IsVUlFN5puWOmXrqjgGUfIRIbU7mr8oNBE2tyERd9Wk=
26+
github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA=
27+
github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o=
2928
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
3029
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
3130
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
31+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
32+
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
33+
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
34+
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
35+
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
3236
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
3337
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
3438
github.com/invopop/validation v0.3.0 h1:o260kbjXzoBO/ypXDSSrCLL7SxEFUXBsX09YTE9AxZw=
@@ -41,8 +45,8 @@ github.com/labstack/echo/v4 v4.11.3 h1:Upyu3olaqSHkCjs1EJJwQ3WId8b8b1hxbogyommKk
4145
github.com/labstack/echo/v4 v4.11.3/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws=
4246
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
4347
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
44-
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
4548
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
49+
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
4650
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
4751
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
4852
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
@@ -52,14 +56,14 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
5256
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
5357
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
5458
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
55-
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
5659
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
60+
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
5761
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
5862
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
63+
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
64+
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
5965
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
60-
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
6166
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
62-
github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
6367
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
6468
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
6569
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -101,6 +105,8 @@ github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQ
101105
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
102106
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
103107
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
108+
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
109+
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
104110
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
105111
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
106112
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=

infrastructure/docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ services:
1818
retries: 10
1919
entrypoint:
2020
sh -c "
21-
echo 'CREATE DATABASE IF NOT EXISTS go-unit-test;' > /docker-entrypoint-initdb.d/init.sql;
21+
echo 'CREATE DATABASE IF NOT EXISTS go_unit_test;' > /docker-entrypoint-initdb.d/init.sql;
2222
/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"

internal/generated/mocks/repository_mock.go

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/generated/mocks/usecase_mock.go

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/users/delivery/http/handlers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ func (h *handlers) DetailHandler(c echo.Context) error {
4141
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
4242
}
4343

44-
return c.JSON(http.StatusOK, map[string]interface{}{"data": detail})
44+
return c.JSON(http.StatusOK, detail)
4545
}

0 commit comments

Comments
 (0)