Skip to content

Commit

Permalink
refactor: implement tests using gomock (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Feb 4, 2024
1 parent 4f9f4d5 commit d3949a5
Show file tree
Hide file tree
Showing 19 changed files with 403 additions and 66 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/reusable-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
with:
go-version: 1.21.5

- name: Setup dependencies
run: go get -v ./...
- name: Setup Go environment
run: make setup

- name: Setup firesrore emulator
- name: Install firesrore emulator
run: curl -sL https://firebase.tools | bash

- name: Run go format
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
### Project Settings ###
.env*
!.env.development
tmp/
*.log

mock/
tmp/

### Firebase ###
.idea
**/node_modules/*
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"go.inferGopath": true,
"go.goroot": "${env:HOME}/.asdf/installs/golang/1.21.5/go",
"go.gopath": "${env:HOME}/.asdf/installs/golang/1.21.5/packages",
"gopls": {
"ui.semanticTokens": true
},
"[go]": {
"editor.defaultFormatter": "golang.go",
},
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ OPEN_API_GO_DST := interal/${OPEN_API_GO_PACKAGE}
OPEN_API_NODE_GENERATOR := typescript-fetch
OPEN_API_NODE_DST := src/openapi

setup:
setup: dependencies generate
cp .pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

dependencies:
go get -v ./...
go install go.uber.org/mock/mockgen@latest

run:
go run cmd/app/main.go
Expand All @@ -31,11 +34,17 @@ build:
go build -o tmp/app cmd/app/main.go

test:
firebase emulators:exec 'go test ./...'
firebase emulators:exec --only firestore --import ${API_REPOSITORY_ROOT}/fixtures 'go test ./...'

generate:
go generate ./...

start-firestore-emulator:
firebase emulators:start --only firestore

edit-firestore-emulator-fixtures:
firebase emulators:start --only firestore --import ${API_REPOSITORY_ROOT}/fixtures --export-on-exit

start-docs-server:
@docker run --detach --name ${OPEN_API_DOCS_SERVER} -v "${API_REPOSITORY_ROOT}:/api" -p 8081:8081 \
redocly/cli preview-docs \
Expand Down
15 changes: 14 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/kumachan-mis/knodeledge-api/internal/api"
"github.com/kumachan-mis/knodeledge-api/internal/db"
"github.com/kumachan-mis/knodeledge-api/internal/repository"
"github.com/kumachan-mis/knodeledge-api/internal/service"
"github.com/kumachan-mis/knodeledge-api/internal/usecase"
"github.com/subosito/gotenv"
)

Expand Down Expand Up @@ -46,7 +49,17 @@ func main() {
})
})

router.POST("/api/hello-world", api.HelloWorldHandler)
client := db.FirestoreClient()
if client == nil {
log.Fatalf("Failed to get firestore client")
}

r := repository.NewHelloWorldRepository(*client)
s := service.NewHelloWorldService(r)
uc := usecase.NewHelloWorldUseCase(s)
a := api.NewHelloWorldApi(uc)

router.POST("/api/hello-world", a.HandleHelloWorld)

err = router.Run(":8080")
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions fixtures/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "13.1.0",
"firestore": {
"version": "1.18.2",
"path": "firestore_export",
"metadata_file": "firestore_export/firestore_export.overall_export_metadata"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ module github.com/kumachan-mis/knodeledge-api
go 1.21.5

require (
cloud.google.com/go/firestore v1.14.0
firebase.google.com/go v3.13.0+incompatible
github.com/gin-contrib/cors v1.5.0
github.com/gin-gonic/gin v1.9.1
github.com/stretchr/testify v1.8.4
github.com/subosito/gotenv v1.6.0
go.uber.org/mock v0.4.0
)

require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/firestore v1.14.0 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
firebase.google.com/go v3.13.0+incompatible // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/cors v1.5.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -39,6 +40,7 @@ require (
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -61,7 +63,6 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.156.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
Expand Down
Loading

0 comments on commit d3949a5

Please sign in to comment.