Skip to content

Commit

Permalink
fix: using metacontext in academic module (#163)
Browse files Browse the repository at this point in the history
* fix: using metacontext in academic module

* fix ci

* fix ci test

* fix ci agin
  • Loading branch information
jiuxia211 authored Jan 20, 2025
1 parent 4a380e9 commit 12991c0
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 1,230 deletions.
46 changes: 4 additions & 42 deletions api/handler/api/academic_service.go

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

1 change: 0 additions & 1 deletion cmd/user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func main() {
if err != nil {
logger.Fatalf("User: resolve tcp addr failed, err: %v", err)
}

svr := userservice.NewServer(
user.NewUserService(clientSet),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
Expand Down
8 changes: 0 additions & 8 deletions idl/academic.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ namespace go academic
include "model.thrift"

struct GetScoresRequest {
1: required string id
2: required string cookies
}

struct GetScoresResponse {
Expand All @@ -12,8 +10,6 @@ struct GetScoresResponse {
}

struct GetGPARequest {
1: required string id
2: required string cookies
}

struct GetGPAResponse {
Expand All @@ -22,8 +18,6 @@ struct GetGPAResponse {
}

struct GetCreditRequest {
1: required string id
2: required string cookies
}

struct GetCreditResponse {
Expand All @@ -32,8 +26,6 @@ struct GetCreditResponse {
}

struct GetUnifiedExamRequest {
1: required string id
2: required string cookies
}

struct GetUnifiedExamResponse {
Expand Down
8 changes: 4 additions & 4 deletions internal/academic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *AcademicServiceImpl) GetScores(ctx context.Context, req *academic.GetSc
var scores []*jwch.Mark
l := service.NewAcademicService(ctx)

scores, err = l.GetScores(req)
scores, err = l.GetScores()
if err != nil {
logger.Infof("Academic.GetScores: GetScores failed, err: %v", err)
resp.Base = base.BuildBaseResp(err)
Expand All @@ -54,7 +54,7 @@ func (s *AcademicServiceImpl) GetGPA(ctx context.Context, req *academic.GetGPARe
var gpa *jwch.GPABean
l := service.NewAcademicService(ctx)

gpa, err = l.GetGPA(req)
gpa, err = l.GetGPA()
if err != nil {
logger.Infof("Academic.GetGPA: GetGPA failed, err: %v", err)
resp.Base = base.BuildBaseResp(err)
Expand All @@ -71,7 +71,7 @@ func (s *AcademicServiceImpl) GetCredit(ctx context.Context, req *academic.GetCr
var credit []*jwch.CreditStatistics
l := service.NewAcademicService(ctx)

credit, err = l.GetCredit(req)
credit, err = l.GetCredit()
if err != nil {
logger.Infof("Academic.GetCredit: GetCredit failed, err: %v", err)
resp.Base = base.BuildBaseResp(err)
Expand All @@ -89,7 +89,7 @@ func (s *AcademicServiceImpl) GetUnifiedExam(ctx context.Context, req *academic.
var unifiedExam []*jwch.UnifiedExam
l := service.NewAcademicService(ctx)

unifiedExam, err = l.GetUnifiedExam(req)
unifiedExam, err = l.GetUnifiedExam()
if err != nil {
logger.Infof("Academic.GetUnifiedExam: GetUnifiedExam failed, err: %v", err)
resp.Base = base.BuildBaseResp(err)
Expand Down
12 changes: 8 additions & 4 deletions internal/academic/service/get_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ package service
import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetCredit(req *academic.GetCreditRequest) ([]*jwch.CreditStatistics, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
func (s *AcademicService) GetCredit() ([]*jwch.CreditStatistics, error) {
loginData, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, fmt.Errorf("service.GetCredit: Get login data fail %w", err)
}
stu := jwch.NewStudent().WithLoginData(loginData.Id, utils.ParseCookies(loginData.Cookies))
credit, err := stu.GetCredit()
if err = base.HandleJwchError(err); err != nil {
return nil, fmt.Errorf("service.Credit: Get credit info fail %w", err)
return nil, fmt.Errorf("service.GetCredit: Get credit info fail %w", err)
}

return credit, nil
Expand Down
15 changes: 9 additions & 6 deletions internal/academic/service/get_credit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/jwch"
)

Expand Down Expand Up @@ -61,16 +62,18 @@ func TestAcademicService_GetCredit(t *testing.T) {
},
}

req := &academic.GetCreditRequest{
Id: "102301517",
Cookies: "cookie1=value1; cookie2=value2",
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock((*jwch.Student).GetCredit).Return(tc.mockReturn, tc.mockError).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
academicService := NewAcademicService(context.Background())
result, err := academicService.GetCredit(req)
result, err := academicService.GetCredit()
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
Expand Down
10 changes: 7 additions & 3 deletions internal/academic/service/get_gpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package service
import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetGPA(req *academic.GetGPARequest) (*jwch.GPABean, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
func (s *AcademicService) GetGPA() (*jwch.GPABean, error) {
loginData, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, fmt.Errorf("service.GetGPA: Get login data fail %w", err)
}
stu := jwch.NewStudent().WithLoginData(loginData.Id, utils.ParseCookies(loginData.Cookies))
gpa, err := stu.GetGPA()
if err = base.HandleJwchError(err); err != nil {
return nil, fmt.Errorf("service.GetGPA: Get gpa info fail %w", err)
Expand Down
15 changes: 9 additions & 6 deletions internal/academic/service/get_gpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/jwch"
)

Expand Down Expand Up @@ -67,16 +68,18 @@ func TestAcademicService_GetGPA(t *testing.T) {
},
}

req := &academic.GetGPARequest{
Id: "102301517",
Cookies: "cookie1=value1; cookie2=value2",
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock((*jwch.Student).GetGPA).Return(tc.mockReturn, tc.mockError).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
academicService := NewAcademicService(context.Background())
result, err := academicService.GetGPA(req)
result, err := academicService.GetGPA()
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
Expand Down
10 changes: 7 additions & 3 deletions internal/academic/service/get_scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package service
import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetScores(req *academic.GetScoresRequest) ([]*jwch.Mark, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
func (s *AcademicService) GetScores() ([]*jwch.Mark, error) {
loginData, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, fmt.Errorf("service.GetScores: Get login data fail %w", err)
}
stu := jwch.NewStudent().WithLoginData(loginData.Id, utils.ParseCookies(loginData.Cookies))
scores, err := stu.GetMarks()
if err = base.HandleJwchError(err); err != nil {
return nil, fmt.Errorf("service.GetScores: Get scores info fail %w", err)
Expand Down
15 changes: 9 additions & 6 deletions internal/academic/service/get_scores_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/jwch"
)

Expand Down Expand Up @@ -68,17 +69,19 @@ func TestAcademicService_GetScores(t *testing.T) {
},
}

req := &academic.GetScoresRequest{
Id: "102301517",
Cookies: "cookie1=value1; cookie2=value2",
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock((*jwch.Student).GetMarks).Return(tc.mockReturn, tc.mockError).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
academicService := NewAcademicService(context.Background())
result, err := academicService.GetScores(req)
result, err := academicService.GetScores()
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
Expand Down
10 changes: 7 additions & 3 deletions internal/academic/service/get_unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package service
import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetUnifiedExam(req *academic.GetUnifiedExamRequest) ([]*jwch.UnifiedExam, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
func (s *AcademicService) GetUnifiedExam() ([]*jwch.UnifiedExam, error) {
loginData, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, fmt.Errorf("service.GetUnifiedExam: Get login data fail %w", err)
}
stu := jwch.NewStudent().WithLoginData(loginData.Id, utils.ParseCookies(loginData.Cookies))
cet, err := stu.GetCET()
if err = base.HandleJwchError(err); err != nil {
return nil, fmt.Errorf("service.GetUnifiedExam: Get cet info fail %w", err)
Expand Down
16 changes: 9 additions & 7 deletions internal/academic/service/get_unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
meta "github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/jwch"
)

Expand Down Expand Up @@ -83,19 +84,20 @@ func TestAcademicService_GetUnifiedExam(t *testing.T) {
},
}

req := &academic.GetUnifiedExamRequest{
Id: "102301517",
Cookies: "cookie1=value1; cookie2=value2",
}

defer mockey.UnPatchAll()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock((*jwch.Student).GetCET).Return(tc.mockCETReturn, tc.mockError).Build()
mockey.Mock((*jwch.Student).GetJS).Return(tc.mockJSReturn, tc.mockJSError).Build()
mockey.Mock(meta.GetLoginData).To(func(ctx context.Context) (*model.LoginData, error) {
return &model.LoginData{
Id: "1111111111111111111111111111111111",
Cookies: "",
}, nil
}).Build()
academicService := NewAcademicService(context.Background())
result, err := academicService.GetUnifiedExam(req)
result, err := academicService.GetUnifiedExam()
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
Expand Down
Loading

0 comments on commit 12991c0

Please sign in to comment.