-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhuwenliang
committed
Dec 9, 2024
1 parent
b237003
commit f2b7f6f
Showing
18 changed files
with
355 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
internal/cases/internal/integration/knowledge_base_handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ecodeclub/ekit/iox" | ||
"github.com/ecodeclub/ginx/session" | ||
"github.com/ecodeclub/webook/internal/ai" | ||
aimocks "github.com/ecodeclub/webook/internal/ai/mocks" | ||
"github.com/ecodeclub/webook/internal/cases/internal/domain" | ||
eveMocks "github.com/ecodeclub/webook/internal/cases/internal/event/mocks" | ||
"github.com/ecodeclub/webook/internal/cases/internal/integration/startup" | ||
"github.com/ecodeclub/webook/internal/cases/internal/repository/dao" | ||
"github.com/ecodeclub/webook/internal/cases/internal/service" | ||
"github.com/ecodeclub/webook/internal/interactive" | ||
intrmocks "github.com/ecodeclub/webook/internal/interactive/mocks" | ||
"github.com/ecodeclub/webook/internal/test" | ||
testioc "github.com/ecodeclub/webook/internal/test/ioc" | ||
"github.com/ego-component/egorm" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gotomicro/ego/core/econf" | ||
"github.com/gotomicro/ego/server/egin" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
type KnowledgeBaseTestSuite struct { | ||
suite.Suite | ||
db *egorm.Component | ||
caseSvc service.Service | ||
} | ||
|
||
func (k *KnowledgeBaseTestSuite) SetupSuite() { | ||
k.db = testioc.InitDB() | ||
} | ||
|
||
func (k *KnowledgeBaseTestSuite) getWantCase(id int64) domain.Case { | ||
que := domain.Case{ | ||
Id: id, | ||
Uid: 123, | ||
Labels: []string{"label"}, | ||
Introduction: fmt.Sprintf("intro %d", id), | ||
Title: fmt.Sprintf("标题%d", id), | ||
Content: fmt.Sprintf("内容%d", id), | ||
GiteeRepo: "gitee", | ||
GithubRepo: "github", | ||
Shorthand: "速记", | ||
Keywords: fmt.Sprintf("关键字 %d", id), | ||
Highlight: fmt.Sprintf("亮点 %d", id), | ||
Guidance: fmt.Sprintf("引导点 %d", id), | ||
Biz: domain.BizCase, | ||
BizId: id, | ||
Status: domain.PublishedStatus, | ||
} | ||
return que | ||
} | ||
|
||
func (k *KnowledgeBaseTestSuite) TestKnowledgeBaseSync() { | ||
ctrl := gomock.NewController(k.T()) | ||
svc := aimocks.NewMockRepositoryBaseSvc(ctrl) | ||
producer := eveMocks.NewMockSyncEventProducer(ctrl) | ||
producer.EXPECT().Produce(gomock.Any(), gomock.Any()).AnyTimes() | ||
svc.EXPECT().UploadFile(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, file ai.KnowledgeBaseFile) error { | ||
assert.Equal(k.T(), fmt.Sprintf("case_%d", file.BizID), file.Name) | ||
assert.Equal(k.T(), domain.BizCase, file.Biz) | ||
wantCase := k.getWantCase(file.BizID) | ||
var actualCa domain.Case | ||
err := json.Unmarshal(file.Data, &actualCa) | ||
if err != nil { | ||
return err | ||
} | ||
assert.Equal(k.T(), file.BizID, wantCase.Id) | ||
actualCa.Ctime = wantCase.Ctime | ||
actualCa.Utime = wantCase.Utime | ||
assert.Equal(k.T(), wantCase, actualCa) | ||
return nil | ||
}).AnyTimes() | ||
// 初始化数据 | ||
err := dao.InitTables(k.db) | ||
require.NoError(k.T(), err) | ||
|
||
require.NoError(k.T(), err) | ||
|
||
intrSvc := intrmocks.NewMockService(ctrl) | ||
intrModule := &interactive.Module{ | ||
Svc: intrSvc, | ||
} | ||
module, err := startup.InitModule(producer, &ai.Module{ | ||
KnowledgeBaseSvc: svc, | ||
}, intrModule) | ||
require.NoError(k.T(), err) | ||
k.caseSvc = module.Svc | ||
wantCa := k.getWantCase(1) | ||
_, err = k.caseSvc.Publish(context.Background(), wantCa) | ||
require.NoError(k.T(), err) | ||
|
||
econf.Set("server", map[string]any{"contextTimeout": "1s"}) | ||
server := egin.Load("server").Build() | ||
server.Use(func(ctx *gin.Context) { | ||
ctx.Set("_session", session.NewMemorySession(session.Claims{ | ||
Uid: 123, | ||
Data: map[string]string{ | ||
"creator": "true", | ||
"memberDDL": strconv.FormatInt(time.Now().Add(time.Hour).UnixMilli(), 10), | ||
}, | ||
})) | ||
}) | ||
module.KnowledgeBaseHandler.PrivateRoutes(server.Engine) | ||
|
||
req, err := http.NewRequest(http.MethodGet, | ||
"/case/knowledgeBase/syncAll", iox.NewJSONReader(nil)) | ||
req.Header.Set("content-type", "application/json") | ||
require.NoError(k.T(), err) | ||
recorder := test.NewJSONResponseRecorder[int64]() | ||
server.ServeHTTP(recorder, req) | ||
// 等待同步完成 | ||
time.Sleep(3 * time.Second) | ||
} | ||
|
||
func TestKnowledgeBaseTestSuite(t *testing.T) { | ||
suite.Run(t, new(KnowledgeBaseTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ecodeclub/webook/internal/ai" | ||
"github.com/ecodeclub/webook/internal/cases/internal/domain" | ||
"github.com/ecodeclub/webook/internal/cases/internal/repository" | ||
"github.com/gotomicro/ego/core/elog" | ||
) | ||
|
||
type KnowledgeBaseService interface { | ||
FullSync() | ||
} | ||
|
||
type knowledgeBaseSvc struct { | ||
caseRepo repository.CaseRepo | ||
knowledgeBaseSvc ai.KnowledgeBaseService | ||
logger *elog.Component | ||
knowledgeBaseId string | ||
} | ||
|
||
func NewKnowledgeBaseService(repo repository.CaseRepo, svc ai.KnowledgeBaseService, knowledgeBaseId string) KnowledgeBaseService { | ||
return &knowledgeBaseSvc{ | ||
caseRepo: repo, | ||
knowledgeBaseSvc: svc, | ||
logger: elog.DefaultLogger, | ||
knowledgeBaseId: knowledgeBaseId, | ||
} | ||
} | ||
|
||
func (k *knowledgeBaseSvc) FullSync() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
cids, err := k.caseRepo.Ids(ctx) | ||
cancel() | ||
if err != nil { | ||
k.logger.Error("查找案例列表失败", elog.FieldErr(err)) | ||
return | ||
} | ||
for _, cid := range cids { | ||
err = k.syncOne(cid) | ||
if err != nil { | ||
k.logger.Error(fmt.Sprintf("同步案例 %d失败", cid), elog.FieldErr(err)) | ||
} else { | ||
k.logger.Info(fmt.Sprintf("同步案例 %d成功", cid)) | ||
} | ||
} | ||
} | ||
|
||
func (k *knowledgeBaseSvc) syncOne(id int64) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
ca, err := k.caseRepo.GetById(ctx, id) | ||
if err != nil { | ||
return fmt.Errorf("获取案例id列表失败 %w", err) | ||
} | ||
data, err := json.Marshal(ca) | ||
if err != nil { | ||
return fmt.Errorf("序列化案例数据失败 %w", err) | ||
} | ||
err = k.knowledgeBaseSvc.UploadFile(ctx, ai.KnowledgeBaseFile{ | ||
Biz: domain.BizCase, | ||
BizID: ca.Id, | ||
Name: fmt.Sprintf("case_%d", ca.Id), | ||
Data: data, | ||
Type: ai.RepositoryBaseTypeRetrieval, | ||
KnowledgeBaseID: k.knowledgeBaseId, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("上传到ai的知识库失败 %w", err) | ||
} | ||
return err | ||
} |
Oops, something went wrong.