Skip to content

Commit

Permalink
(wip) implement ysk busniess logic and test
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Aug 5, 2024
1 parent abd3fcb commit 40b0659
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 62 deletions.
4 changes: 2 additions & 2 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,12 @@ components:
type: object
required:
- "label"
- "value"
- "progress"
properties:
label:
type: string
example: "Installing jellyfin"
value:
progress:
type: integer
example: 50

Expand Down
35 changes: 25 additions & 10 deletions pkg/ysk/adapter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ysk

import (
"database/sql/driver"
"encoding/json"
"errors"

"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
)
Expand Down Expand Up @@ -40,26 +42,39 @@ type YSKCard struct {
func (yskCard YSKCard) WithProgress(label string, progress int) YSKCard {
if yskCard.Content.BodyProgress != nil {
yskCard.Content.BodyProgress = &YSKCardProgress{
Label: label,
Value: progress,
Label: label,
Progress: progress,
}
return yskCard
}
return yskCard
}

type YSKCardContent struct {
TitleIcon string `json:"titleIcon"`
TitleText string `json:"titleText"`
BodyProgress *YSKCardProgress `json:"bodyProgress,omitempty"`
BodyIconWithText *YSKCardIconWithText `json:"bodyIconWithText,omitempty"`
BodyList []YSKCardListItem `json:"bodyList,omitempty"`
FooterActions []YSKCardFooterAction `json:"footerActions,omitempty"`
TitleIcon string `json:"titleIcon" gorm:"column:title_icon"`
TitleText string `json:"titleText" gorm:"column:title_text"`
BodyProgress *YSKCardProgress `json:"bodyProgress,omitempty" gorm:"serializer:json"`
BodyIconWithText *YSKCardIconWithText `json:"bodyIconWithText,omitempty" gorm:"serializer:json"`
BodyList []YSKCardListItem `json:"bodyList,omitempty" gorm:"serializer:json"`
FooterActions []YSKCardFooterAction `json:"footerActions,omitempty" gorm:"serializer:json"`
}

func (p YSKCardContent) Value() (driver.Value, error) {
return json.Marshal(p)
}

func (p *YSKCardContent) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}

return json.Unmarshal(b, &p)
}

type YSKCardProgress struct {
Label string
Value int
Label string
Progress int
}

type YSKCardIconWithText struct {
Expand Down
24 changes: 11 additions & 13 deletions pkg/ysk/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package ysk
import (
"context"
"encoding/json"

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

const (
Expand All @@ -15,7 +13,7 @@ func DefineCard(ctx context.Context, cardID string) YSKCard {
return YSKCard{}
}

func NewYSKCard(ctx context.Context, YSKCard codegen.YSKCard, publish func(context.Context, string, string, map[string]string)) error {
func NewYSKCard(ctx context.Context, YSKCard YSKCard, publish func(context.Context, string, string, map[string]string)) error {
// do something
yskCardBodyJSON, _ := json.Marshal(YSKCard)
publish(ctx, SERVICENAME, "ysk:card:create", map[string]string{"body": string(yskCardBodyJSON)})
Expand All @@ -28,13 +26,13 @@ func DeleteCard(ctx context.Context, cardID string, publish func(context.Context
return nil
}

func TaskWithProgress(card codegen.YSKCard, label string, progress int) codegen.YSKCard {
if card.Content.BodyProgress != nil {
card.Content.BodyProgress = &codegen.YSKCardProgress{
Label: label,
Value: progress,
}
return card
}
return card
}
// func TaskWithProgress(card codegen.YSKCard, label string, progress int) codegen.YSKCard {
// if card.Content.BodyProgress != nil {
// card.Content.BodyProgress = &codegen.YSKCardProgress{
// Label: label,
// Progress: progress,
// }
// return card
// }
// return card
// }
16 changes: 8 additions & 8 deletions pkg/ysk/ysk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import (

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

"gotest.tools/assert"
)

func mockPublish(context.Context, string, string, map[string]string) {
}

func TestUpdateProgress(t *testing.T) {

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

}

func TestNoticeDiskInsert(t *testing.T) {
err := ysk.NewYSKCard(context.Background(), utils.DiskInsertNotice, nil)
err := ysk.NewYSKCard(context.Background(), utils.DiskInsertNotice, mockPublish)
assert.NilError(t, err)
}
6 changes: 3 additions & 3 deletions repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package repository

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

type Repository interface {
Expand All @@ -16,8 +16,8 @@ type Repository interface {
GetActionTypesBySourceID(sourceID string) ([]model.ActionType, error)
GetActionType(sourceID string, name string) (*model.ActionType, error)

GetYSKCardList() ([]codegen.YSKCard, error)
UpsertYSKCard(card codegen.YSKCard) error
GetYSKCardList() ([]ysk.YSKCard, error)
UpsertYSKCard(card ysk.YSKCard) error

Close()
}
13 changes: 8 additions & 5 deletions repository/repository_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"time"

"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/model"
"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"github.com/glebarez/sqlite"
"go.uber.org/zap"
"gorm.io/gorm"
Expand All @@ -16,15 +16,15 @@ type DatabaseRepository struct {
db *gorm.DB
}

func (r *DatabaseRepository) GetYSKCardList() ([]codegen.YSKCard, error) {
var cardList []codegen.YSKCard
func (r *DatabaseRepository) GetYSKCardList() ([]ysk.YSKCard, error) {
var cardList []ysk.YSKCard
if err := r.db.Find(&cardList).Error; err != nil {
return nil, err
}
return cardList, nil
}

func (r *DatabaseRepository) UpsertYSKCard(card codegen.YSKCard) error {
func (r *DatabaseRepository) UpsertYSKCard(card ysk.YSKCard) error {
return r.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
UpdateAll: true,
Expand Down Expand Up @@ -148,7 +148,10 @@ func NewDatabaseRepository(databaseFilePath string) (Repository, error) {
c.SetMaxOpenConns(1)
c.SetConnMaxIdleTime(1000 * time.Second)

if err := db.AutoMigrate(&model.EventType{}, &model.ActionType{}, &model.PropertyType{}); err != nil {
if err := db.AutoMigrate(
&model.EventType{}, &model.ActionType{}, &model.PropertyType{},
&ysk.YSKCard{},
); err != nil {
return nil, err
}

Expand Down
10 changes: 9 additions & 1 deletion route/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"github.com/labstack/echo/v4"
"github.com/samber/lo"
)
Expand All @@ -27,7 +28,14 @@ func (r *APIRoute) GetYskCard(ctx echo.Context) error {
Message: lo.ToPtr(err.Error()),
})
}

return ctx.JSON(http.StatusOK, codegen.ResponseGetYSKCardListOK{
Data: lo.ToPtr(cardList),
Data: lo.ToPtr(lo.Map(cardList, func(yskCard ysk.YSKCard, _ int) codegen.YSKCard {
card, err := ysk.ToCodegenYSKCard(yskCard)
if err != nil {
return codegen.YSKCard{}
}
return card
})),
})
}
12 changes: 8 additions & 4 deletions service/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"context"

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

Expand All @@ -17,15 +17,19 @@ func NewYSKService(repository *repository.Repository) *YSKService {
}
}

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

func (s *YSKService) UpsertYSKCard(ctx context.Context, yskCard codegen.YSKCard) error {
func (s *YSKService) UpsertYSKCard(ctx context.Context, yskCard ysk.YSKCard) error {
// don't store short notice cards
if yskCard.CardType == ysk.CardTypeShortNote {
return nil
}
err := (*s.repository).UpsertYSKCard(yskCard)
return err
}
Expand Down
50 changes: 47 additions & 3 deletions service/ysk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,74 @@ import (
"context"
"testing"

"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"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) {
func setup(t *testing.T) *service.YSKService {
repository, err := repository.NewDatabaseRepositoryInMemory()
assert.NilError(t, err)
defer repository.Close()

yskService := service.NewYSKService(&repository)
return yskService
}

func TestInsertAndGetCardList(t *testing.T) {
yskService := setup(t)

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

err = yskService.UpsertYSKCard(context.Background(), utils.ApplicationInstallProgress)
cardInsertQueue := []ysk.YSKCard{
utils.ApplicationInstallProgress, utils.DiskInsertNotice,
}

for _, card := range cardInsertQueue {
err = yskService.UpsertYSKCard(context.Background(), card)
assert.NilError(t, err)
}

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

err = yskService.UpsertYSKCard(context.Background(), utils.DiskInsertNotice)
for _, card := range cardList {
if card.Id == utils.ApplicationInstallProgress.Id {
assert.DeepEqual(t, card, utils.ApplicationInstallProgress)
} else if card.Id == utils.DiskInsertNotice.Id {
assert.DeepEqual(t, card, utils.DiskInsertNotice)
} else {
t.Errorf("unexpected card: %v", card)
}
}
}

func TestInsertAllTypeCardList(t *testing.T) {
yskService := setup(t)

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

cardInsertQueue := []ysk.YSKCard{
utils.ApplicationInstallProgress, utils.DiskInsertNotice, utils.ApplicationUpdateNotice,
utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 50), utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 55),
utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 80), utils.ApplicationInstallProgress.WithProgress("Installing LinuxServer/Jellyfin", 99),
utils.ApplicationUpdateNotice,
}

for _, card := range cardInsertQueue {
err = yskService.UpsertYSKCard(context.Background(), card)
assert.NilError(t, err)
}

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

}
Loading

0 comments on commit 40b0659

Please sign in to comment.