Skip to content

Commit

Permalink
调整 case 的代码 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash authored Mar 25, 2024
1 parent a0661d6 commit ad84e1b
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 182 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ config/local.yaml
/internal/question/internal/integration/logs/
cover.out
/internal/label/internal/integration/logs/
/internal/product/internal/integration/logs/
4 changes: 2 additions & 2 deletions .script/integration_test_compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
mysql8:
image: mysql:8.0
image: mysql:8.0.29
command: --default_authentication_plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
Expand All @@ -12,7 +12,7 @@ services:
# 映射 13316端口
- "13316:3306"
redis:
image: 'bitnami/redis:7.2'
image: 'redis:7.2.4-alpine'
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
- question - 02
- cos - 03
- product - 04
- case - 05
10 changes: 3 additions & 7 deletions internal/cases/internal/domain/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ type Case struct {
Title string
Content string
CodeRepo string
Summary Summary
Ctime time.Time
Utime time.Time
}

// 案例的总结摘要
type Summary struct {
// 关键字,辅助记忆,提取重点
Keywords string
// 速记,口诀
Expand All @@ -27,4 +20,7 @@ type Summary struct {
Highlight string
// 引导点
Guidance string

Ctime time.Time
Utime time.Time
}
2 changes: 1 addition & 1 deletion internal/cases/internal/errs/code.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package errs

var (
SystemError = ErrorCode{Code: 502001, Msg: "系统错误"}
SystemError = ErrorCode{Code: 505001, Msg: "系统错误"}
)

type ErrorCode struct {
Expand Down
184 changes: 84 additions & 100 deletions internal/cases/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ type HandlerTestSuite struct {
}

func (s *HandlerTestSuite) TearDownSuite() {
err := s.db.Exec("DROP TABLE `case`").Error
err := s.db.Exec("DROP TABLE `cases`").Error
require.NoError(s.T(), err)
err = s.db.Exec("DROP TABLE `publish_case`").Error
err = s.db.Exec("DROP TABLE `publish_cases`").Error
require.NoError(s.T(), err)
}

func (s *HandlerTestSuite) TearDownTest() {
err := s.db.Exec("TRUNCATE TABLE `case`").Error
err := s.db.Exec("TRUNCATE TABLE `cases`").Error
require.NoError(s.T(), err)
err = s.db.Exec("TRUNCATE TABLE `publish_case`").Error
err = s.db.Exec("TRUNCATE TABLE `publish_cases`").Error
require.NoError(s.T(), err)
}

Expand All @@ -59,10 +59,11 @@ func (s *HandlerTestSuite) SetupSuite() {
server.Use(func(ctx *gin.Context) {
ctx.Set("_session", session.NewMemorySession(session.Claims{
Uid: uid,
Data: map[string]string{"admin": "true"},
Data: map[string]string{"creator": "true"},
}))
})
handler.PrivateRoutes(server.Engine)
handler.PublicRoutes(server.Engine)
s.server = server
s.db = testioc.InitDB()
err = dao.InitTables(s.db)
Expand Down Expand Up @@ -107,16 +108,14 @@ func (s *HandlerTestSuite) TestSave() {
},
req: web.SaveReq{
Case: web.Case{
Title: "案例1",
Content: "案例1内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Summary: web.Summary{
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
Title: "案例1",
Content: "案例1内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
},
wantCode: 200,
Expand Down Expand Up @@ -170,17 +169,15 @@ func (s *HandlerTestSuite) TestSave() {
},
req: web.SaveReq{
Case: web.Case{
Id: 2,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Summary: web.Summary{
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
Id: 2,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
},
wantCode: 200,
Expand All @@ -202,9 +199,9 @@ func (s *HandlerTestSuite) TestSave() {
assert.Equal(t, tc.wantResp, recorder.MustScan())
tc.after(t)
// 清理掉 123 的数据
err = s.db.Exec("TRUNCATE table `case`").Error
err = s.db.Exec("TRUNCATE table `cases`").Error
require.NoError(t, err)
err = s.db.Exec("TRUNCATE table `publish_case`").Error
err = s.db.Exec("TRUNCATE table `publish_cases`").Error
require.NoError(t, err)
})
}
Expand Down Expand Up @@ -325,23 +322,21 @@ func (s *HandlerTestSuite) TestDetail() {
{
name: "查询到了数据",
req: web.CaseId{
CaseId: 3,
Cid: 3,
},
wantCode: 200,
wantResp: test.Result[web.Case]{
Data: web.Case{
Id: 3,
Labels: []string{"Redis"},
Title: "redis案例标题",
Content: "redis案例内容",
CodeRepo: "redis仓库",
Summary: web.Summary{
Keywords: "redis_keywords",
Shorthand: "redis_shorthand",
Highlight: "redis_highlight",
Guidance: "redis_guidance",
},
Utime: time.UnixMilli(12).Format(time.DateTime),
Id: 3,
Labels: []string{"Redis"},
Title: "redis案例标题",
Content: "redis案例内容",
CodeRepo: "redis仓库",
Keywords: "redis_keywords",
Shorthand: "redis_shorthand",
Highlight: "redis_highlight",
Guidance: "redis_guidance",
Utime: time.UnixMilli(12).Format(time.DateTime),
},
},
},
Expand Down Expand Up @@ -401,16 +396,14 @@ func (s *HandlerTestSuite) TestPublish() {
},
req: web.SaveReq{
Case: web.Case{
Title: "案例1",
Content: "案例1内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Summary: web.Summary{
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
Title: "案例1",
Content: "案例1内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
},
wantCode: 200,
Expand Down Expand Up @@ -470,17 +463,15 @@ func (s *HandlerTestSuite) TestPublish() {
},
req: web.SaveReq{
Case: web.Case{
Id: 2,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Summary: web.Summary{
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
Id: 2,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
},
wantCode: 200,
Expand Down Expand Up @@ -558,17 +549,15 @@ func (s *HandlerTestSuite) TestPublish() {
},
req: web.SaveReq{
Case: web.Case{
Id: 3,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Summary: web.Summary{
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
Id: 3,
Title: "案例2",
Content: "案例2内容",
Labels: []string{"MySQL"},
CodeRepo: "www.github.com",
Keywords: "mysql_keywords",
Shorthand: "mysql_shorthand",
Highlight: "mysql_highlight",
Guidance: "mysql_guidance",
},
},
wantCode: 200,
Expand All @@ -589,9 +578,9 @@ func (s *HandlerTestSuite) TestPublish() {
require.Equal(t, tc.wantCode, recorder.Code)
assert.Equal(t, tc.wantResp, recorder.MustScan())
tc.after(t)
err = s.db.Exec("TRUNCATE table `case`").Error
err = s.db.Exec("TRUNCATE table `cases`").Error
require.NoError(t, err)
err = s.db.Exec("TRUNCATE table `publish_case`").Error
err = s.db.Exec("TRUNCATE table `publish_cases`").Error
require.NoError(t, err)
})
}
Expand Down Expand Up @@ -627,16 +616,14 @@ func (s *HandlerTestSuite) TestPublist() {
Total: 100,
Cases: []web.Case{
{
Id: 100,
Title: "这是发布的案例标题 99",
Content: "这是发布的案例内容 99",
Utime: time.UnixMilli(0).Format(time.DateTime),
Id: 100,
Title: "这是发布的案例标题 99",
Utime: time.UnixMilli(0).Format(time.DateTime),
},
{
Id: 99,
Title: "这是发布的案例标题 98",
Content: "这是发布的案例内容 98",
Utime: time.UnixMilli(0).Format(time.DateTime),
Id: 99,
Title: "这是发布的案例标题 98",
Utime: time.UnixMilli(0).Format(time.DateTime),
},
},
},
Expand All @@ -654,10 +641,9 @@ func (s *HandlerTestSuite) TestPublist() {
Total: 100,
Cases: []web.Case{
{
Id: 1,
Title: "这是发布的案例标题 0",
Content: "这是发布的案例内容 0",
Utime: time.UnixMilli(0).Format(time.DateTime),
Id: 1,
Title: "这是发布的案例标题 0",
Utime: time.UnixMilli(0).Format(time.DateTime),
},
},
},
Expand Down Expand Up @@ -711,23 +697,21 @@ func (s *HandlerTestSuite) TestPubDetail() {
{
name: "查询到了数据",
req: web.CaseId{
CaseId: 3,
Cid: 3,
},
wantCode: 200,
wantResp: test.Result[web.Case]{
Data: web.Case{
Id: 3,
Labels: []string{"Redis"},
Title: "redis案例标题",
Content: "redis案例内容",
CodeRepo: "redis仓库",
Summary: web.Summary{
Keywords: "redis_keywords",
Shorthand: "redis_shorthand",
Highlight: "redis_highlight",
Guidance: "redis_guidance",
},
Utime: time.UnixMilli(13).Format(time.DateTime),
Id: 3,
Labels: []string{"Redis"},
Title: "redis案例标题",
Content: "redis案例内容",
CodeRepo: "redis仓库",
Keywords: "redis_keywords",
Shorthand: "redis_shorthand",
Highlight: "redis_highlight",
Guidance: "redis_guidance",
Utime: time.UnixMilli(13).Format(time.DateTime),
},
},
},
Expand Down
Loading

0 comments on commit ad84e1b

Please sign in to comment.