Skip to content

Commit

Permalink
对接案例测试
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Aug 20, 2024
1 parent e2e9957 commit c00afe8
Show file tree
Hide file tree
Showing 44 changed files with 1,483 additions and 970 deletions.
5 changes: 4 additions & 1 deletion internal/ai/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"github.com/gotomicro/ego/core/econf"
)

func InitHandlerFacade(common []handler.Builder, zhipu *zhipu.Handler) *biz.FacadeHandler {
func InitHandlerFacade(common []handler.Builder,
zhipu *zhipu.Handler) *biz.FacadeHandler {
que := InitQuestionExamineHandler(common, zhipu)
c := InitCaseExamineHandler(common, zhipu)
return biz.NewHandler(map[string]handler.Handler{
que.Biz(): que,
c.Biz(): c,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (c *CompositionHandler) Handle(ctx context.Context, req domain.LLMRequest)
return c.root.Handle(ctx, req)
}

func (c *CompositionHandler) Name() string {
return c.name
}

func (c *CompositionHandler) Biz() string {
return c.name
}
Expand Down
37 changes: 35 additions & 2 deletions internal/bff/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
return resMap, nil
}).AnyTimes()

caseExamSvc := casemocks.NewMockExamineService(ctrl)
caseExamSvc.EXPECT().GetResults(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, uid int64, ids []int64) (map[int64]cases.ExamineResult, error) {
res := make(map[int64]cases.ExamineResult, len(ids))
for _, id := range ids {
res[id] = cases.ExamineResult{
Cid: id,
// 偶数不通过,基数通过
Result: cases.ExamineResultEnum(id % 2),
}
}
return res, nil
}).AnyTimes()

caseSvc := casemocks.NewMockService(ctrl)
caseSvc.EXPECT().GetPubByIDs(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, ids []int64) ([]cases.Case, error) {
Expand All @@ -117,17 +131,27 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
}), nil
}).AnyTimes()
caseSetSvc := casemocks.NewMockCaseSetService(ctrl)
caseSetSvc.EXPECT().GetByIds(gomock.Any(), gomock.Any()).
caseSetSvc.EXPECT().GetByIdsWithCases(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, ids []int64) ([]cases.CaseSet, error) {
return slice.Map(ids, func(idx int, src int64) cases.CaseSet {
return cases.CaseSet{
ID: src,
Title: fmt.Sprintf("这是案例集%d", src),
Cases: []cases.Case{
{
Id: src*10 + 1,
Title: fmt.Sprintf("这是案例%d", src*10+1),
},
{
Id: src*10 + 2,
Title: fmt.Sprintf("这是案例%d", src*10+2),
},
},
}
}), nil
}).AnyTimes()
handler, _ := st.InitHandler(&interactive.Module{Svc: intrSvc},
&cases.Module{Svc: caseSvc, SetSvc: caseSetSvc},
&cases.Module{Svc: caseSvc, SetSvc: caseSetSvc, ExamineSvc: caseExamSvc},
&baguwen.Module{Svc: queSvc, SetSvc: queSetSvc, ExamSvc: examSvc})
econf.Set("server", map[string]any{"contextTimeout": "1s"})
server := egin.Load("server").Build()
Expand Down Expand Up @@ -208,6 +232,15 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
CaseSet: web.CaseSet{
ID: 5,
Title: "这是案例集5",
Cases: []web.Case{
{
ID: 51,
ExamineResult: 1,
},
{
ID: 52,
},
},
},
},
}, recorder.MustScan().Data)
Expand Down
2 changes: 1 addition & 1 deletion internal/bff/internal/integration/startup/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func InitHandler(intrModule *interactive.Module,
web.NewHandler,
wire.FieldsOf(new(*interactive.Module), "Svc"),
wire.FieldsOf(new(*baguwen.Module), "Svc", "SetSvc", "ExamSvc"),
wire.FieldsOf(new(*cases.Module), "Svc", "SetSvc"),
wire.FieldsOf(new(*cases.Module), "ExamineSvc", "Svc", "SetSvc"),
)
return new(web.Handler), nil
}
Expand Down
5 changes: 3 additions & 2 deletions internal/bff/internal/integration/startup/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 27 additions & 10 deletions internal/bff/internal/web/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func (h *Handler) CollectionRecords(ctx *ginx.Context, req CollectionInfoReq, se
return systemErrorResult, err
}
var (
eg errgroup.Group
csm map[int64]cases.Case
cssmap map[int64]cases.CaseSet
qsm map[int64]baguwen.Question
qssmap map[int64]baguwen.QuestionSet
examResMap map[int64]baguwen.ExamResult
eg errgroup.Group
csm map[int64]cases.Case
cssmap map[int64]cases.CaseSet
qsm map[int64]baguwen.Question
qssmap map[int64]baguwen.QuestionSet
queExamResMap map[int64]baguwen.ExamResult
caseExamResMap map[int64]cases.ExamineResult
)
var qids, cids, csids, qsids, qid2s []int64
for _, record := range records {
Expand Down Expand Up @@ -74,23 +75,39 @@ func (h *Handler) CollectionRecords(ctx *ginx.Context, req CollectionInfoReq, se
})

eg.Go(func() error {
csets, cserr := h.caseSetSvc.GetByIds(ctx, csids)
csets, cserr := h.caseSetSvc.GetByIdsWithCases(ctx, csids)
cssmap = slice.ToMap(csets, func(element cases.CaseSet) int64 {
return element.ID
})
for _, cs := range csets {
cids = append(cids, cs.Cids()...)
}
return cserr
})
if err = eg.Wait(); err != nil {
return systemErrorResult, err
}

eg = errgroup.Group{}
eg.Go(func() error {
var err1 error
queExamResMap, err1 = h.queExamSvc.GetResults(ctx, uid, qid2s)
return err1
})

eg.Go(func() error {
var err1 error
caseExamResMap, err1 = h.caseExamSvc.GetResults(ctx, uid, cids)
return err1
})
// 获取进度
examResMap, err = h.examSvc.GetResults(ctx, uid, qid2s)
if err != nil {

if err = eg.Wait(); err != nil {
return systemErrorResult, err
}

res := slice.Map(records, func(idx int, src interactive.CollectionRecord) CollectionRecord {
return newCollectionRecord(src, csm, cssmap, qsm, qssmap, examResMap)
return newCollectionRecord(src, csm, cssmap, qsm, qssmap, queExamResMap, caseExamResMap)
})
return ginx.Result{
Data: res,
Expand Down
29 changes: 16 additions & 13 deletions internal/bff/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ import (
)

type Handler struct {
intrSvc interactive.Service
caseSvc cases.Service
caseSetSvc cases.SetService
queSvc baguwen.Service
queSetSvc baguwen.QuestionSetService
examSvc baguwen.ExamService
intrSvc interactive.Service
caseSvc cases.Service
caseSetSvc cases.SetService
caseExamSvc cases.ExamineService
queSvc baguwen.Service
queSetSvc baguwen.QuestionSetService
queExamSvc baguwen.ExamService
}

func NewHandler(
intrSvc interactive.Service,
caseSvc cases.Service,
caseSetSvc cases.SetService,
caseExamineSvc cases.ExamineService,
queSvc baguwen.Service,
queSetSvc baguwen.QuestionSetService,
examSvc baguwen.ExamService,
queExamSvc baguwen.ExamService,
) *Handler {
return &Handler{
intrSvc: intrSvc,
caseSvc: caseSvc,
queSvc: queSvc,
queSetSvc: queSetSvc,
examSvc: examSvc,
caseSetSvc: caseSetSvc,
intrSvc: intrSvc,
caseSvc: caseSvc,
queSvc: queSvc,
queSetSvc: queSetSvc,
queExamSvc: queExamSvc,
caseSetSvc: caseSetSvc,
caseExamSvc: caseExamineSvc,
}
}

Expand Down
27 changes: 20 additions & 7 deletions internal/bff/internal/web/vo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"github.com/ecodeclub/ekit/slice"
"github.com/ecodeclub/webook/internal/cases"
"github.com/ecodeclub/webook/internal/interactive"
baguwen "github.com/ecodeclub/webook/internal/question"
Expand All @@ -21,8 +22,9 @@ type CollectionRecord struct {
}

type Case struct {
ID int64 `json:"id"`
Title string `json:"title"`
ID int64 `json:"id"`
Title string `json:"title"`
ExamineResult uint8 `json:"examineResult"`
}

type Question struct {
Expand All @@ -48,7 +50,8 @@ func newCollectionRecord(record interactive.CollectionRecord,
csm map[int64]cases.CaseSet,
qm map[int64]baguwen.Question,
qsm map[int64]baguwen.QuestionSet,
examMap map[int64]baguwen.ExamResult,
queExamMap map[int64]baguwen.ExamResult,
caseExamMap map[int64]cases.ExamineResult,
) CollectionRecord {
res := CollectionRecord{
Id: record.Id,
Expand All @@ -57,20 +60,30 @@ func newCollectionRecord(record interactive.CollectionRecord,
case CaseBiz:
res.Case = setCases(record, cm)
case QuestionBiz:
res.Question = setQuestion(record, qm, examMap)
res.Question = setQuestion(record, qm, queExamMap)
case QuestionSetBiz:
res.QuestionSet = setQuestionSet(record, qsm, examMap)
res.QuestionSet = setQuestionSet(record, qsm, queExamMap)
case CaseSetBiz:
res.CaseSet = setCaseSet(record, csm)
res.CaseSet = setCaseSet(record, csm, caseExamMap)
}
return res
}

func setCaseSet(ca interactive.CollectionRecord, csm map[int64]cases.CaseSet) CaseSet {
func setCaseSet(
ca interactive.CollectionRecord,
csm map[int64]cases.CaseSet,
caseExamMap map[int64]cases.ExamineResult,
) CaseSet {
cs := csm[ca.CaseSet]
return CaseSet{
ID: cs.ID,
Title: cs.Title,
Cases: slice.Map(cs.Cases, func(idx int, src cases.Case) Case {
return Case{
ID: src.Id,
ExamineResult: caseExamMap[src.Id].Result.ToUint8(),
}
}),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/bff/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func InitModule(intrModule *interactive.Module,
web.NewHandler,
wire.FieldsOf(new(*baguwen.Module), "Svc", "SetSvc", "ExamSvc"),
wire.FieldsOf(new(*interactive.Module), "Svc"),
wire.FieldsOf(new(*cases.Module), "SetSvc", "Svc"),
wire.FieldsOf(new(*cases.Module), "SetSvc", "Svc", "ExamineSvc"),
wire.Struct(new(Module), "*"),
)
return new(Module), nil
Expand Down
5 changes: 3 additions & 2 deletions internal/bff/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions internal/cases/internal/domain/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ func (r CaseResult) ToUint8() uint8 {
const (
// ResultFailed 完全没通过,或者完全没有考过,我们不需要区别这两种状态
ResultFailed CaseResult = iota
// ResultBasic 只回答出来了 15K 的部分
ResultBasic
// ResultIntermediate 回答了 25K 部分
ResultIntermediate
// ResultAdvanced 回答出来了 35K 部分
ResultAdvanced
// ResultPassed 回答通过
// 注意案例这边只有符合或者不符合,没有级别的评判
ResultPassed
)
Loading

0 comments on commit c00afe8

Please sign in to comment.