Skip to content

Commit

Permalink
(wip) add service layer test
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Aug 5, 2024
1 parent 9ea92f4 commit bf70092
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 50 deletions.
48 changes: 5 additions & 43 deletions pkg/ysk/ysk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,26 @@ import (
"context"
"testing"

"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"github.com/IceWhaleTech/CasaOS-MessageBus/utils"

"gotest.tools/assert"
)

func TestUpdateProgress(t *testing.T) {
ApplicationInstallProgress := codegen.YSKCard{
Id: "task:application:install",
CardType: codegen.YSKCardCardTypeTask,
RenderType: codegen.YSKCardRenderTypeTask,
Content: codegen.YSKCardContent{
TitleIcon: "jellyfin logo",
TitleText: "APP Installing",
BodyProgress: &codegen.YSKCardProgress{},
BodyIconWithText: nil,
BodyList: nil,
FooterActions: nil,
},
}

err := ysk.NewYSKCard(context.Background(), ysk.TaskWithProgress(
ApplicationInstallProgress,
utils.ApplicationInstallProgress,
"Installing LinuxServer/Jellyfin",
50,
), nil)
assert.NilError(t, err)
err = ysk.DeleteCard(context.Background(), ApplicationInstallProgress.Id, nil)
err = ysk.DeleteCard(context.Background(), utils.ApplicationInstallProgress.Id, nil)
assert.NilError(t, err)

}

func TestNoticeDiskInsert(t *testing.T) {
DiskInsertNotice := codegen.YSKCard{
Id: "long-notice:disk:insert",
CardType: codegen.YSKCardCardTypeLongNotice,
RenderType: codegen.YSKCardRenderTypeListNotice,
Content: codegen.YSKCardContent{
TitleIcon: "ZimaOS-Logo",
TitleText: "创建数据工作站",
BodyProgress: nil,
BodyIconWithText: &codegen.YSKCardIconWithText{
Icon: "disk",
Description: "通过添加硬盘驱动器或固态硬盘来增强你的个人主机,并建立自己的个人数据中心。",
},
BodyList: nil,
FooterActions: &[]codegen.YSKCardFooterAction{
{
Side: "Right",
Style: "primary",
Text: "创建数据工作站",
MessageBus: codegen.YSKCardMessageBusAction{
Key: "open:disk:insert",
Payload: "{'type':'disk'}",
},
},
},
},
}
err := ysk.NewYSKCard(context.Background(), DiskInsertNotice, nil)
err := ysk.NewYSKCard(context.Background(), utils.DiskInsertNotice, nil)
assert.NilError(t, err)
}
2 changes: 1 addition & 1 deletion service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func NewServices(repository *repository.Repository) Services {

ActionTypeService: actionTypeService,
ActionServiceWS: NewActionServiceWS(actionTypeService),
YSKService: NewYSKService(),
YSKService: NewYSKService(repository),
}
}
21 changes: 15 additions & 6 deletions service/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import (
"context"

"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/repository"
)

type YSKService struct{}
type YSKService struct {
repository *repository.Repository
}

func NewYSKService() *YSKService {
return &YSKService{}
func NewYSKService(repository *repository.Repository) *YSKService {
return &YSKService{
repository: repository,
}
}

func (s *YSKService) YskCardList(ctx context.Context) (codegen.YSKCardList, error) {
return nil, nil
cardList, err := (*s.repository).GetYSKCardList()
if err != nil {
return codegen.YSKCardList{}, err
}
return cardList, nil
}

func (s *YSKService) UpsertYSKCard(ctx context.Context, yskCard codegen.YSKCard) error {
// check card
return nil
err := (*s.repository).UpsertYSKCard(yskCard)
return err
}

func (s *YSKService) DeleteYSKCard(ctx context.Context, id string) error {
Expand Down
33 changes: 33 additions & 0 deletions service/ysk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package service_test

import (
"context"
"testing"

"github.com/IceWhaleTech/CasaOS-MessageBus/repository"
"github.com/IceWhaleTech/CasaOS-MessageBus/service"
"github.com/IceWhaleTech/CasaOS-MessageBus/utils"
"gotest.tools/assert"
)

func TestInsertAndGetCardList(t *testing.T) {
repository, err := repository.NewDatabaseRepositoryInMemory()
assert.NilError(t, err)
defer repository.Close()

yskService := service.NewYSKService(&repository)

cardList, err := yskService.YskCardList(context.Background())
assert.NilError(t, err)
assert.Equal(t, len(cardList), 0)

err = yskService.UpsertYSKCard(context.Background(), utils.ApplicationInstallProgress)
assert.NilError(t, err)

err = yskService.UpsertYSKCard(context.Background(), utils.DiskInsertNotice)
assert.NilError(t, err)

cardList, err = yskService.YskCardList(context.Background())
assert.NilError(t, err)
assert.Equal(t, len(cardList), 2)
}
48 changes: 48 additions & 0 deletions utils/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
)

var (
ApplicationInstallProgress = codegen.YSKCard{
Id: "task:application:install",
CardType: codegen.YSKCardCardTypeTask,
RenderType: codegen.YSKCardRenderTypeTask,
Content: codegen.YSKCardContent{
TitleIcon: "jellyfin logo",
TitleText: "APP Installing",
BodyProgress: &codegen.YSKCardProgress{},
BodyIconWithText: nil,
BodyList: nil,
FooterActions: nil,
},
}

DiskInsertNotice = codegen.YSKCard{
Id: "long-notice:disk:insert",
CardType: codegen.YSKCardCardTypeLongNotice,
RenderType: codegen.YSKCardRenderTypeListNotice,
Content: codegen.YSKCardContent{
TitleIcon: "ZimaOS-Logo",
TitleText: "创建数据工作站",
BodyProgress: nil,
BodyIconWithText: &codegen.YSKCardIconWithText{
Icon: "disk",
Description: "通过添加硬盘驱动器或固态硬盘来增强你的个人主机,并建立自己的个人数据中心。",
},
BodyList: nil,
FooterActions: &[]codegen.YSKCardFooterAction{
{
Side: "Right",
Style: "primary",
Text: "创建数据工作站",
MessageBus: codegen.YSKCardMessageBusAction{
Key: "open:disk:insert",
Payload: "{'type':'disk'}",
},
},
},
},
}
)

0 comments on commit bf70092

Please sign in to comment.