Skip to content

Commit

Permalink
Merge pull request #20 from MidraLab/feat/gotest-workflow
Browse files Browse the repository at this point in the history
gotestsCiの追加
  • Loading branch information
eiei114 authored Nov 4, 2023
2 parents c1067b3 + d8c3fcb commit a498813
Show file tree
Hide file tree
Showing 11 changed files with 702 additions and 26 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/auto_mockgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ jobs:
go install go.uber.org/mock/mockgen@latest
go install github.com/sanposhiho/gomockhandler@latest
- name: Install gotests
run: |
go install github.com/cweill/gotests/gotests@latest
- name: Generate mocks
run: |
cd app
go generate ./...
- name: Go lint
run: |
cd app
go fmt ./...
go mod tidy
- name: Configure Git
run: |
git config user.name "GitHub Actions"
Expand Down
1 change: 1 addition & 0 deletions app/application/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
)

//go:generate gotests -w -all $GOFILE
type Middleware struct {
UserService service.UserServiceInterface
}
Expand Down
98 changes: 98 additions & 0 deletions app/application/middleware/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package middleware

import (
"reflect"
"testing"

"example.com/application/service"
"github.com/uptrace/bunrouter"
)

func TestNewMiddleware(t *testing.T) {
type args struct {
userService service.UserServiceInterface
}
tests := []struct {
name string
args args
want *Middleware
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewMiddleware(tt.args.userService); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewMiddleware() = %v, want %v", got, tt.want)
}
})
}
}

func TestMiddleware_AuthenticateMiddleware(t *testing.T) {
type fields struct {
UserService service.UserServiceInterface
}
tests := []struct {
name string
fields fields
want func(bunrouter.HandlerFunc) bunrouter.HandlerFunc
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Middleware{
UserService: tt.fields.UserService,
}
if got := m.AuthenticateMiddleware(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Middleware.AuthenticateMiddleware() = %v, want %v", got, tt.want)
}
})
}
}

func TestMiddleware_CorsMiddleware(t *testing.T) {
type fields struct {
UserService service.UserServiceInterface
}
tests := []struct {
name string
fields fields
want func(bunrouter.HandlerFunc) bunrouter.HandlerFunc
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Middleware{
UserService: tt.fields.UserService,
}
if got := m.CorsMiddleware(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Middleware.CorsMiddleware() = %v, want %v", got, tt.want)
}
})
}
}

func TestMiddleware_RecoverMiddleware(t *testing.T) {
type fields struct {
UserService service.UserServiceInterface
}
tests := []struct {
name string
fields fields
want func(bunrouter.HandlerFunc) bunrouter.HandlerFunc
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Middleware{
UserService: tt.fields.UserService,
}
if got := m.RecoverMiddleware(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Middleware.RecoverMiddleware() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions app/application/service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
)

//go:generate gotests -w -all $GOFILE
type UserService struct {
UserRepository repository.UserRepository
}
Expand Down
227 changes: 227 additions & 0 deletions app/application/service/user_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package service

import (
"context"
"reflect"
"testing"

"example.com/domain"
"example.com/domain/repository"
)

func TestNewUserService(t *testing.T) {
type args struct {
UserRepository repository.UserRepository
}
tests := []struct {
name string
args args
want *UserService
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewUserService(tt.args.UserRepository); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewUserService() = %v, want %v", got, tt.want)
}
})
}
}

func TestUserService_Add(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
name string
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
got, err := u.Add(tt.args.ctx, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("UserService.Add() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("UserService.Add() = %v, want %v", got, tt.want)
}
})
}
}

func TestUserService_UpdateUser(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
user domain.User
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
if err := u.UpdateUser(tt.args.ctx, tt.args.user); (err != nil) != tt.wantErr {
t.Errorf("UserService.UpdateUser() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestUserService_Delete(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
id string
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
got, err := u.Delete(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("UserService.Delete() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("UserService.Delete() = %v, want %v", got, tt.want)
}
})
}
}

func TestUserService_GetUserByUserId(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
id string
}
tests := []struct {
name string
fields fields
args args
want domain.User
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
got, err := u.GetUserByUserId(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("UserService.GetUserByUserId() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UserService.GetUserByUserId() = %v, want %v", got, tt.want)
}
})
}
}

func TestUserService_GetUserByAuthToken(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
authToken string
}
tests := []struct {
name string
fields fields
args args
want *domain.User
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
got, err := u.GetUserByAuthToken(tt.args.ctx, tt.args.authToken)
if (err != nil) != tt.wantErr {
t.Errorf("UserService.GetUserByAuthToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UserService.GetUserByAuthToken() = %v, want %v", got, tt.want)
}
})
}
}

func TestUserService_GetUserRanking(t *testing.T) {
type fields struct {
UserRepository repository.UserRepository
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
want []*domain.UserRanking
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &UserService{
UserRepository: tt.fields.UserRepository,
}
got, err := u.GetUserRanking(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("UserService.GetUserRanking() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UserService.GetUserRanking() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.21.1

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.1
github.com/uptrace/bun v1.1.16
github.com/uptrace/bun/dialect/mysqldialect v1.1.16
github.com/uptrace/bunrouter v1.0.20
go.uber.org/mock v0.3.0
)

require (
Expand Down
Loading

0 comments on commit a498813

Please sign in to comment.