Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(unit-test): Add site info common service unit test.

LinkinStarb111f812e492
diff --git a/go.mod b/go.mod
index 8262bf31..ce792597 100644
--- a/go.mod
+++ b/go.mod
@@ -14,6 +14,7 @@ require (
github.com/go-playground/validator/v10 v10.11.1
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.9.11
+ github.com/golang/mock v1.4.4
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/gosimple/slug v1.13.1
diff --git a/go.sum b/go.sum
index ff917061..d36b4d64 100644
--- a/go.sum
+++ b/go.sum
@@ -235,6 +235,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
+github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
diff --git a/internal/service/mock/siteinfo_repo_mock.go b/internal/service/mock/siteinfo_repo_mock.go
new file mode 100644
index 00000000..772d36fe
--- /dev/null
+++ b/internal/service/mock/siteinfo_repo_mock.go
@@ -0,0 +1,66 @@
+// Code generated by MockGen. DO NOT EDIT.
+// Source: ./siteinfo_service.go
+
+// Package mock is a generated GoMock package.
+package mock
+
+import (
+ context "context"
+ reflect "reflect"
+
+ entity "github.com/answerdev/answer/internal/entity"
+ gomock "github.com/golang/mock/gomock"
+)
+
+// MockSiteInfoRepo is a mock of SiteInfoRepo interface.
+type MockSiteInfoRepo struct {
+ ctrl *gomock.Controller
+ recorder *MockSiteInfoRepoMockRecorder
+}
+
+// MockSiteInfoRepoMockRecorder is the mock recorder for MockSiteInfoRepo.
+type MockSiteInfoRepoMockRecorder struct {
+ mock *MockSiteInfoRepo
+}
+
+// NewMockSiteInfoRepo creates a new mock instance.
+func NewMockSiteInfoRepo(ctrl *gomock.Controller) *MockSiteInfoRepo {
+ mock := &MockSiteInfoRepo{ctrl: ctrl}
+ mock.recorder = &MockSiteInfoRepoMockRecorder{mock}
+ return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use.
+func (m *MockSiteInfoRepo) EXPECT() *MockSiteInfoRepoMockRecorder {
+ return m.recorder
+}
+
+// GetByType mocks base method.
+func (m *MockSiteInfoRepo) GetByType(ctx context.Context, siteType string) (*entity.SiteInfo, bool, error) {
+ m.ctrl.T.Helper()
+ ret := m.ctrl.Call(m, "GetByType", ctx, siteType)
+ ret0, _ := ret[0].(*entity.SiteInfo)
+ ret1, _ := ret[1].(bool)
+ ret2, _ := ret[2].(error)
+ return ret0, ret1, ret2
+}
+
+// GetByType indicates an expected call of GetByType.
+func (mr *MockSiteInfoRepoMockRecorder) GetByType(ctx, siteType interface{}) *gomock.Call {
+ mr.mock.ctrl.T.Helper()
+ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetByType", reflect.TypeOf((*MockSiteInfoRepo)(nil).GetByType), ctx, siteType)
+}
+
+// SaveByType mocks base method.
+func (m *MockSiteInfoRepo) SaveByType(ctx context.Context, siteType string, data *entity.SiteInfo) error {
+ m.ctrl.T.Helper()
+ ret := m.ctrl.Call(m, "SaveByType", ctx, siteType, data)
+ ret0, _ := ret[0].(error)
+ return ret0
+}
+
+// SaveByType indicates an expected call of SaveByType.
+func (mr *MockSiteInfoRepoMockRecorder) SaveByType(ctx, siteType, data interface{}) *gomock.Call {
+ mr.mock.ctrl.T.Helper()
+ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SaveByType", reflect.TypeOf((*MockSiteInfoRepo)(nil).SaveByType), ctx, siteType, data)
+}
diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.go
index 0690097b..b61e59ff 100644
--- a/internal/service/siteinfo_common/siteinfo_service.go
+++ b/internal/service/siteinfo_common/siteinfo_service.go
@@ -9,6 +9,7 @@ import (
"github.com/answerdev/answer/internal/schema"
)
+//go:generate mockgen -source=./siteinfo_service.go -destination=../mock/siteinfo_repo_mock.go -package=mock
type SiteInfoRepo interface {
SaveByType(ctx context.Context, siteType string, data *entity.SiteInfo) (err error)
GetByType(ctx context.Context, siteType string) (siteInfo *entity.SiteInfo, exist bool, err error)
diff --git a/internal/service/siteinfo_common/siteinfo_service_test.go b/internal/service/siteinfo_common/siteinfo_service_test.go
new file mode 100644
index 00000000..b3e15a22
--- /dev/null
+++ b/internal/service/siteinfo_common/siteinfo_service_test.go
@@ -0,0 +1,31 @@
+package siteinfo_common
+
+import (
+ "testing"
+
+ "github.com/answerdev/answer/internal/base/constant"
+ "github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/internal/service/mock"
+ "github.com/golang/mock/gomock"
+ "github.com/stretchr/testify/assert"
+)
+
+var (
+ mockSiteInfoRepo *mock.MockSiteInfoRepo
+)
+
+func mockInit(ctl *gomock.Controller) {
+ mockSiteInfoRepo = mock.NewMockSiteInfoRepo(ctl)
+ mockSiteInfoRepo.EXPECT().GetByType(gomock.Any(), constant.SiteTypeGeneral).
+ Return(&entity.SiteInfo{Content: `{"name":"name"}`}, true, nil)
+}
+
+func TestSiteInfoCommonService_GetSiteGeneral(t *testing.T) {
+ ctl := gomock.NewController(t)
+ defer ctl.Finish()
+ mockInit(ctl)
+ siteInfoCommonService := NewSiteInfoCommonService(mockSiteInfoRepo)
+ resp, err := siteInfoCommonService.GetSiteGeneral(nil)
+ assert.NoError(t, err)
+ assert.Equal(t, resp.Name, "name")
+}