init project
This commit is contained in:
30
modules/system/container/container.go
Normal file
30
modules/system/container/container.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/modules/system/controller"
|
||||
"app/modules/system/dao"
|
||||
"app/modules/system/routes"
|
||||
"app/modules/system/service"
|
||||
"log"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(dao.NewDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(service.NewSystemService); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(controller.NewController); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(routes.NewRoute, dig.Group("route")); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
27
modules/system/controller/controller.go
Normal file
27
modules/system/controller/controller.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"app/modules/system/dao"
|
||||
"app/modules/system/dto"
|
||||
"app/modules/system/service"
|
||||
"app/providers/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Controller interface {
|
||||
GetName(*gin.Context) (dto.Name, error)
|
||||
}
|
||||
|
||||
type ControllerImpl struct {
|
||||
Conf *config.Config
|
||||
svc service.SystemService
|
||||
}
|
||||
|
||||
func NewController(Conf *config.Config, dao dao.Dao, svc service.SystemService) Controller {
|
||||
return &ControllerImpl{Conf: Conf, svc: svc}
|
||||
}
|
||||
|
||||
func (c *ControllerImpl) GetName(ctx *gin.Context) (dto.Name, error) {
|
||||
return c.svc.GetName(ctx)
|
||||
}
|
||||
29
modules/system/dao/dao.go
Normal file
29
modules/system/dao/dao.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"app/providers/config"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Dao interface {
|
||||
Release(context.Context, int, string) error
|
||||
}
|
||||
|
||||
type DaoImpl struct {
|
||||
Conf *config.Config
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewDao(DB *gorm.DB) Dao {
|
||||
return &DaoImpl{DB: DB}
|
||||
}
|
||||
|
||||
func (c *DaoImpl) Release(ctx context.Context, a int, b string) error {
|
||||
if a == 20 {
|
||||
return errors.New("A cant't be 20 ")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
60
modules/system/dao/dao_test.go
Normal file
60
modules/system/dao/dao_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"app/providers/config"
|
||||
_ "app/providers/httpsvc"
|
||||
_ "app/providers/logger"
|
||||
_ "app/providers/mysql"
|
||||
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Fields struct {
|
||||
dig.In
|
||||
|
||||
Conf *config.Config
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func TestDaoImpl_Release(t *testing.T) {
|
||||
var ff Fields
|
||||
err := container.Container.Invoke(func(f Fields) {
|
||||
ff = f
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
a int
|
||||
b string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields Fields
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{"1. ", ff, args{context.Background(), 10, "Rogee"}, false},
|
||||
{"2. ", ff, args{context.Background(), 20, "Rogee"}, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := &DaoImpl{
|
||||
Conf: tt.fields.Conf,
|
||||
DB: tt.fields.DB,
|
||||
}
|
||||
if err := c.Release(tt.args.ctx, tt.args.a, tt.args.b); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DaoImpl.Release() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
5
modules/system/dto/name.go
Normal file
5
modules/system/dto/name.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type Name struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
22
modules/system/routes/routes.go
Normal file
22
modules/system/routes/routes.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"app/contracts"
|
||||
"app/modules/system/controller"
|
||||
"app/providers/http"
|
||||
|
||||
"github.com/rogeecn/gen"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
controller controller.Controller
|
||||
svc *http.Service
|
||||
}
|
||||
|
||||
func NewRoute(c controller.Controller, svc *http.Service) contracts.Route {
|
||||
return &Route{controller: c, svc: svc}
|
||||
}
|
||||
|
||||
func (r *Route) Register() {
|
||||
r.svc.Engine.GET("/name", gen.DataFunc(r.controller.GetName))
|
||||
}
|
||||
26
modules/system/service/service.go
Normal file
26
modules/system/service/service.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"app/modules/system/dao"
|
||||
"app/modules/system/dto"
|
||||
"context"
|
||||
)
|
||||
|
||||
type SystemService interface {
|
||||
GetName(ctx context.Context) (dto.Name, error)
|
||||
}
|
||||
|
||||
type systemService struct {
|
||||
dao dao.Dao
|
||||
}
|
||||
|
||||
func NewSystemService(dao dao.Dao) SystemService {
|
||||
return &systemService{dao: dao}
|
||||
}
|
||||
|
||||
func (svc *systemService) GetName(ctx context.Context) (dto.Name, error) {
|
||||
if err := svc.dao.Release(ctx, 10, "Rogee"); err != nil {
|
||||
return dto.Name{}, err
|
||||
}
|
||||
return dto.Name{Name: "System.GetName"}, nil
|
||||
}
|
||||
69
modules/system/service_test.go
Normal file
69
modules/system/service_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "app/providers/config" // 这里的依赖需要被导入,否则会报错
|
||||
_ "app/providers/logger"
|
||||
_ "app/providers/mysql"
|
||||
_ "app/web/system/container"
|
||||
"app/web/system/service"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type InjectServiceParams struct {
|
||||
dig.In
|
||||
|
||||
Service service.SystemService // 注意这里注入的参数需要大写
|
||||
}
|
||||
|
||||
type TServiceSuite struct {
|
||||
suite.Suite
|
||||
|
||||
InjectServiceParams
|
||||
}
|
||||
|
||||
func Test_ServiceSuiteSuite(t *testing.T) {
|
||||
err := container.Container.Invoke(func(p InjectServiceParams) {
|
||||
s := &TServiceSuite{}
|
||||
s.InjectServiceParams = p
|
||||
|
||||
suite.Run(t, s)
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func (s *TServiceSuite) Test_GetName() {
|
||||
name, err := s.Service.GetName(context.Background())
|
||||
assert.NoError(s.T(), err)
|
||||
assert.Equal(s.T(), "System.GetName", name.Name)
|
||||
}
|
||||
|
||||
//////
|
||||
func (s *TServiceSuite) SetupSuite() {
|
||||
fmt.Println("SetupSuite")
|
||||
}
|
||||
func (s *TServiceSuite) SetupTest() {
|
||||
fmt.Println("SetupTest")
|
||||
}
|
||||
func (s *TServiceSuite) BeforeTest(suiteName, testName string) {
|
||||
fmt.Println("BeforeTest:", suiteName, testName)
|
||||
}
|
||||
func (s *TServiceSuite) AfterTest(suiteName, testName string) {
|
||||
fmt.Println("AfterTest:", suiteName, testName)
|
||||
}
|
||||
func (s *TServiceSuite) HandleStats(suiteName string, stats *suite.SuiteInformation) {
|
||||
fmt.Println("HandleStats:", suiteName, stats)
|
||||
}
|
||||
func (s *TServiceSuite) TearDownTest() {
|
||||
fmt.Println("TearDownTest")
|
||||
}
|
||||
func (s *TServiceSuite) TearDownSuite() {
|
||||
fmt.Println("TearDownSuite")
|
||||
}
|
||||
Reference in New Issue
Block a user