Skip to content

Commit

Permalink
Merge pull request #61 from quangdangfit/more-unittest
Browse files Browse the repository at this point in the history
add server test
  • Loading branch information
quangdangfit authored Sep 9, 2023
2 parents 0814d32 + b56eb8d commit bc5fa2c
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go Shop
[![Master](https://github.com/quangdangfit/goshop/workflows/master/badge.svg)](https://github.com/quangdangfit/goshop/actions)
[![Codecov](https://img.shields.io/codecov/c/github/quangdangfit/goshop?style=flat-square)](https://codecov.io/gh/quangdangfit/goshop)
[![codecov](https://codecov.io/gh/quangdangfit/goshop/graph/badge.svg?token=78BO8FQDB0)](https://codecov.io/gh/quangdangfit/goshop)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/quangdangfit/goshop?style=flat-square)
[![GitHub](https://img.shields.io/github/license/jrapoport/gothic?style=flat-square)](https://github.com/quangdangfit/goshop/blob/master/LICENSE)

Expand Down
8 changes: 8 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"github.com/quangdangfit/gocommon/logger"
"github.com/quangdangfit/gocommon/validation"

orderModel "goshop/internal/order/model"
productModel "goshop/internal/product/model"
grpcServer "goshop/internal/server/grpc"
httpServer "goshop/internal/server/http"
userModel "goshop/internal/user/model"
"goshop/pkg/config"
"goshop/pkg/dbs"
"goshop/pkg/redis"
Expand Down Expand Up @@ -37,6 +40,11 @@ func main() {
logger.Fatal("Cannot connect to database", err)
}

err = db.AutoMigrate(&userModel.User{}, &productModel.Product{}, orderModel.Order{}, orderModel.OrderLine{})
if err != nil {
logger.Fatal("Database migration fail", err)
}

validator := validation.New()

cache := redis.New(redis.Config{
Expand Down
3 changes: 0 additions & 3 deletions internal/order/port/http/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (

"github.com/gin-gonic/gin"
"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/mock"

dbMocks "goshop/pkg/dbs/mocks"
)

func TestRoutes(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
mockDB.On("AutoMigrate", mock.Anything, mock.Anything).Return(nil).Times(1)
Routes(gin.New().Group("/"), mockDB, validation.New())
}
1 change: 0 additions & 1 deletion internal/order/repository/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type OrderRepo struct {
}

func NewOrderRepository(db dbs.IDatabase) *OrderRepo {
_ = db.AutoMigrate(&model.Order{}, &model.OrderLine{})
return &OrderRepo{db: db}
}

Expand Down
1 change: 0 additions & 1 deletion internal/order/repository/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (suite *OrderRepositoryTestSuite) SetupTest() {
logger.Initialize(config.TestEnv)

suite.mockDB = mocks.NewIDatabase(suite.T())
suite.mockDB.On("AutoMigrate", mock.Anything, mock.Anything).Return(nil).Times(1)
suite.repo = NewOrderRepository(suite.mockDB)
}

Expand Down
1 change: 0 additions & 1 deletion internal/order/repository/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type ProductRepo struct {
}

func NewProductRepository(db dbs.IDatabase) *ProductRepo {
_ = db.AutoMigrate(&model.Product{})
return &ProductRepo{db: db}
}

Expand Down
1 change: 0 additions & 1 deletion internal/order/repository/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (suite *ProductRepositoryTestSuite) SetupTest() {
logger.Initialize(config.TestEnv)

suite.mockDB = mocks.NewIDatabase(suite.T())
suite.mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
suite.repo = NewProductRepository(suite.mockDB)
}

Expand Down
2 changes: 0 additions & 2 deletions internal/product/port/http/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (

"github.com/gin-gonic/gin"
"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/mock"

dbMocks "goshop/pkg/dbs/mocks"
redisMocks "goshop/pkg/redis/mocks"
)

func TestRoutes(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
mockRedis := redisMocks.NewIRedis(t)
Routes(gin.New().Group("/"), mockDB, validation.New(), mockRedis)
}
1 change: 0 additions & 1 deletion internal/product/repository/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type ProductRepo struct {
}

func NewProductRepository(db dbs.IDatabase) *ProductRepo {
_ = db.AutoMigrate(&model.Product{})
return &ProductRepo{db: db}
}

Expand Down
1 change: 0 additions & 1 deletion internal/product/repository/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (suite *ProductRepositoryTestSuite) SetupTest() {
logger.Initialize(config.TestEnv)

suite.mockDB = mocks.NewIDatabase(suite.T())
suite.mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
suite.repo = NewProductRepository(suite.mockDB)
}

Expand Down
19 changes: 19 additions & 0 deletions internal/server/grpc/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package grpc

import (
"testing"

"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/assert"

dbMocks "goshop/pkg/dbs/mocks"
redisMocks "goshop/pkg/redis/mocks"
)

func TestNewServer(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockRedis := redisMocks.NewIRedis(t)

server := NewServer(validation.New(), mockDB, mockRedis)
assert.NotNil(t, server)
}
15 changes: 0 additions & 15 deletions internal/server/http/routes.go

This file was deleted.

11 changes: 11 additions & 0 deletions internal/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"

_ "goshop/docs"
orderHttp "goshop/internal/order/port/http"
productHttp "goshop/internal/product/port/http"
userHttp "goshop/internal/user/port/http"
"goshop/pkg/config"
"goshop/pkg/dbs"
"goshop/pkg/redis"
Expand Down Expand Up @@ -60,3 +63,11 @@ func (s Server) Run() error {
func (s Server) GetEngine() *gin.Engine {
return s.engine
}

func (s Server) MapRoutes() error {
v1 := s.engine.Group("/api/v1")
userHttp.Routes(v1, s.db, s.validator)
productHttp.Routes(v1, s.db, s.validator, s.cache)
orderHttp.Routes(v1, s.db, s.validator)
return nil
}
41 changes: 41 additions & 0 deletions internal/server/http/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package http

import (
"testing"

"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/assert"

dbMocks "goshop/pkg/dbs/mocks"
redisMocks "goshop/pkg/redis/mocks"
)

func TestNewServer(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockRedis := redisMocks.NewIRedis(t)

server := NewServer(validation.New(), mockDB, mockRedis)
assert.NotNil(t, server)
}

func TestServer_GetEngine(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockRedis := redisMocks.NewIRedis(t)

server := NewServer(validation.New(), mockDB, mockRedis)
assert.NotNil(t, server)

engine := server.GetEngine()
assert.NotNil(t, engine)
}

func TestServer_MapRoutes(t *testing.T) {
mockDB := dbMocks.NewIDatabase(t)
mockRedis := redisMocks.NewIRedis(t)

server := NewServer(validation.New(), mockDB, mockRedis)
assert.NotNil(t, server)

err := server.MapRoutes()
assert.Nil(t, err)
}
2 changes: 0 additions & 2 deletions internal/user/port/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"testing"

"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/mock"
goGRPC "google.golang.org/grpc"

"goshop/pkg/dbs/mocks"
)

func TestRegisterHandlers(t *testing.T) {
mockDB := mocks.NewIDatabase(t)
mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
RegisterHandlers(goGRPC.NewServer(), mockDB, validation.New())
}
2 changes: 0 additions & 2 deletions internal/user/port/http/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (

"github.com/gin-gonic/gin"
"github.com/quangdangfit/gocommon/validation"
"github.com/stretchr/testify/mock"

"goshop/pkg/dbs/mocks"
)

func TestRoutes(t *testing.T) {
mockDB := mocks.NewIDatabase(t)
mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
Routes(gin.New().Group("/"), mockDB, validation.New())
}
1 change: 0 additions & 1 deletion internal/user/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type UserRepo struct {
}

func NewUserRepository(db dbs.IDatabase) *UserRepo {
_ = db.AutoMigrate(&model.User{})
return &UserRepo{db: db}
}

Expand Down
1 change: 0 additions & 1 deletion internal/user/repository/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (suite *UserRepositoryTestSuite) SetupTest() {
logger.Initialize(config.TestEnv)

suite.mockDB = mocks.NewIDatabase(suite.T())
suite.mockDB.On("AutoMigrate", mock.Anything).Return(nil).Times(1)
suite.repo = NewUserRepository(suite.mockDB)
}

Expand Down
5 changes: 5 additions & 0 deletions test/http/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func setup() {
logger.Fatal("Cannot connect to database", err)
}

err = dbTest.AutoMigrate(&userModel.User{}, &productModel.Product{}, orderModel.Order{}, orderModel.OrderLine{})
if err != nil {
logger.Fatal("Database migration fail", err)
}

validator := validation.New()
testCache = redis.New(redis.Config{
Address: cfg.RedisURI,
Expand Down

0 comments on commit bc5fa2c

Please sign in to comment.