Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

04cleanarchitecture: apply clean architecture and 100% unitest and integration test #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
outpkg: mocks
case: underscore
name: "{{.MockName}}.go"
with-expecter: true
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ $(eval export $(shell sed -ne 's/ *#.*$$//; /./ s/=.*$$// p' .env))

init:
docker compose -f starter/docker-compose.yaml up -d
go install github.com/vektra/mockery/v3@latest
go install github.com/vektra/mockery/v2@latest
go install github.com/google/wire/cmd/wire@latest
init/down:
docker compose -f starter/docker-compose.yaml down
run:
Expand All @@ -18,4 +19,19 @@ unit-test:
@echo ""
go test -cover ./internal/... -count=1

.PHONY: init init/down run mock unit-test
integration-test/db/up:
cp -f ./starter/init.sql ./integration_test/init.sql && \
docker-compose -f ./integration_test/docker-compose.yaml up -d
integration-test/db/down:
docker-compose -f ./integration_test/docker-compose.yaml down

integration-test:
@echo ""
@echo "--------- Run integration test ----------"
@echo ""
go test ./integration_test/... -failfast -count=1

di:
wire ./internal/di/...

.PHONY: init init/down run mock unit-test di
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ $ make init
2. Run
```
$ make run
```
```

9 changes: 9 additions & 0 deletions config/config-local-itest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server:
port: ':8000'
name: 'gotemplate'
database:
postgres:
host: 'localhost'
port: 54311
user: 'template'
dbname: 'templatedb'
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.0 // indirect
github.com/bytedance/sonic v1.10.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
Expand All @@ -23,7 +24,9 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/subcommands v1.0.1 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -41,6 +44,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -51,9 +55,12 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
Expand Down Expand Up @@ -140,8 +142,12 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand Down Expand Up @@ -201,6 +207,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand Down Expand Up @@ -264,6 +272,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -287,6 +297,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -400,6 +412,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down Expand Up @@ -441,6 +454,8 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
13 changes: 13 additions & 0 deletions integration_test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
postgres_itest:
image: postgres:13.3-alpine
restart: always
container_name: postgres_itest
ports:
- "54311:5432"
environment:
- POSTGRES_USER=${POSTGRES_USERNAME}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
65 changes: 65 additions & 0 deletions integration_test/iaccount/itest_account_create_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package iaccount

import (
"net/http"
"strings"
"testing"

itest "github.com/gaogao-asia/golang-template/integration_test"
adto "github.com/gaogao-asia/golang-template/internal/account/dto"
"github.com/gaogao-asia/golang-template/internal/domain"
"github.com/gaogao-asia/golang-template/pkg/utils"
"github.com/stretchr/testify/assert"
)

var (
createAccountsURLPath = "/api/v1/accounts"
)

func TestCreateAccounts(t *testing.T) {
tests := []struct {
name string
handler func(t *testing.T)
}{
{
name: createAccountsURLPath + " success",
handler: createAccountSuccess,
},
}

for _, test := range tests {
t.Run(test.name, test.handler)
}
}

// getUserByPhoneSuccess
func createAccountSuccess(t *testing.T) {
requestBody := strings.NewReader(`{
"name": "gaogao",
"email": "[email protected]",
"roles": ["admin","user"]
}`)

w, err := itest.CallAPI(http.MethodPost, createAccountsURLPath, requestBody)
assert.NoError(t, err)

response := w.Result()
statusCode, res, err := itest.ParseResponse(response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, statusCode)

var dataRes adto.CreateAccountResponse
bytes, err := utils.JsonMarshal(res.Data)
assert.NoError(t, err)
err = utils.JsonUnmarshal(bytes, &dataRes)
assert.NoError(t, err)

// check account response
assert.Equal(t, "gaogao", dataRes.Account.Name)
assert.Equal(t, "[email protected]", dataRes.Account.Email)
assert.Equal(t, []string{"admin", "user"}, dataRes.Account.Roles)

// Clean data
itest.Conn.DB.Model(&domain.Account{}).Where("id = ?", dataRes.Account.ID).Delete(&domain.Account{})
}
77 changes: 77 additions & 0 deletions integration_test/iaccount/itest_account_get_accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package iaccount

import (
"net/http"
"testing"

itest "github.com/gaogao-asia/golang-template/integration_test"

adto "github.com/gaogao-asia/golang-template/internal/account/dto"
"github.com/gaogao-asia/golang-template/internal/domain"
"github.com/gaogao-asia/golang-template/pkg/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

var (
getAccountsURLPath = "/api/v1/accounts"
)

func TestGetAccounts(t *testing.T) {
tests := []struct {
name string
handler func(t *testing.T)
}{
{
name: getAccountsURLPath + " success",
handler: getAccountSuccess,
},
}

for _, test := range tests {
t.Run(test.name, test.handler)
}
}

// getUserByPhoneSuccess
func getAccountSuccess(t *testing.T) {
idata := itest.PrepareAccounts(t)
defer idata.Cleanup()

w, err := itest.CallAPI(http.MethodGet, getAccountsURLPath, nil)
assert.NoError(t, err)

response := w.Result()
statusCode, res, err := itest.ParseResponse(response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, statusCode)

var dataRes adto.GetAccountsResponse
bytes, err := utils.JsonMarshal(res.Data)
assert.NoError(t, err)
err = utils.JsonUnmarshal(bytes, &dataRes)
assert.NoError(t, err)

// check len of accounts
assert.Equal(t, len(idata.Accounts), len(dataRes.Accounts))

// convert account response to map
resAccount := make(map[int64]*domain.Account)
lo.Map(idata.Accounts, func(v *domain.Account, i int) int {
resAccount[v.ID] = v
return i
})

// check account response
for i, v := range idata.Accounts {
if rv, ok := resAccount[v.ID]; ok {
assert.Equal(t, v.ID, rv.ID)
assert.Equal(t, v.Name, rv.Name)
assert.Equal(t, v.Email, rv.Email)
assert.Equal(t, v.Roles, rv.Roles)
} else {
t.Errorf("account %d not found", i)
}
}
}
16 changes: 16 additions & 0 deletions integration_test/iaccount/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package iaccount

import (
"os"
"testing"

itest "github.com/gaogao-asia/golang-template/integration_test"
)

func TestMain(m *testing.M) {
itest.Setup()

ret := m.Run()

os.Exit(ret)
}
8 changes: 8 additions & 0 deletions integration_test/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table accounts
(
id BIGSERIAL,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
roles varchar(255) NOT NULL,
PRIMARY KEY (id)
)
38 changes: 38 additions & 0 deletions integration_test/init_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package integrationtest

import (
"testing"

"github.com/gaogao-asia/golang-template/internal/domain"
)

type ITestData struct {
Accounts []*domain.Account
}

func PrepareAccounts(t *testing.T) ITestData {
accs := []*domain.Account{
{
Name: "gaogao",
Email: "[email protected]",
Roles: "admin,user",
},
{
Name: "minh",
Email: "[email protected]",
Roles: "user",
},
}
err := Conn.DB.Create(&accs).Error
if err != nil {
t.Fatal(err)
}

return ITestData{Accounts: accs}
}

func (s *ITestData) Cleanup() {
if len(s.Accounts) > 0 {
Conn.DB.Delete(s.Accounts)
}
}
Loading