Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test for tag store #186

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions builder/store/database/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"log/slog"
"slices"

"github.com/uptrace/bun"
"opencsg.com/csghub-server/common/types"
Expand Down Expand Up @@ -34,7 +33,6 @@ type TagStore interface {
AllSpaceCategories(ctx context.Context) ([]TagCategory, error)
CreateTag(ctx context.Context, category, name, group string, scope TagScope) (Tag, error)
SaveTags(ctx context.Context, tags []*Tag) error
UpsertTags(ctx context.Context, tagScope TagScope, categoryTagMap map[string][]string) ([]Tag, error)
// SetMetaTags will delete existing tags and create new ones
SetMetaTags(ctx context.Context, repoType types.RepositoryType, namespace, name string, tags []*Tag) (repoTags []*RepositoryTag, err error)
SetLibraryTag(ctx context.Context, repoType types.RepositoryType, namespace, name string, newTag, oldTag *Tag) (err error)
Expand Down Expand Up @@ -65,8 +63,6 @@ const (
PromptTagScope TagScope = "prompt"
)

const defaultTagGroup = ""

type Tag struct {
ID int64 `bun:",pk,autoincrement" json:"id"`
Name string `bun:",notnull" json:"name" yaml:"name"`
Expand Down Expand Up @@ -204,35 +200,6 @@ func (ts *tagStoreImpl) SaveTags(ctx context.Context, tags []*Tag) error {
return nil
}

func (ts *tagStoreImpl) UpsertTags(ctx context.Context, tagScope TagScope, categoryTagMap map[string][]string) ([]Tag, error) {
var tags []Tag
for category, tagNames := range categoryTagMap {
ctags := make([]Tag, 0)
err := ts.db.Operator.Core.NewSelect().Model(&ctags).
Where("caregory = ? and scope = ?", category, tagScope).
Scan(ctx)
if err != nil {
slog.Error("Failed to select tags", slog.String("category", category),
slog.Any("scope", tagScope), slog.Any("error", err.Error()))
return nil, fmt.Errorf("failed to select tags, cause: %w", err)
}
tags = append(tags, ctags...)
for _, tagName := range tagNames {
if !slices.ContainsFunc(tags, func(t Tag) bool { return t.Name == tagName }) {
newTag, err := ts.CreateTag(ctx, category, tagName, defaultTagGroup, tagScope)
if err != nil {
slog.Error("Failed to create new tag", slog.String("category", category), slog.String("tagName", tagName),
slog.Any("scope", tagScope), slog.Any("error", err.Error))
return nil, fmt.Errorf("failed to create new tag, cause: %w", err)
}
tags = append(tags, newTag)
}
}
}

return tags, nil
}

// SetMetaTags will delete existing tags and create new ones
func (ts *tagStoreImpl) SetMetaTags(ctx context.Context, repoType types.RepositoryType, namespace, name string, tags []*Tag) (repoTags []*RepositoryTag, err error) {
repo := new(Repository)
Expand Down Expand Up @@ -376,13 +343,10 @@ func (ts *tagStoreImpl) RemoveRepoTags(ctx context.Context, repoID int64, tagIDs
if len(tagIDs) == 0 {
return nil
}
err = ts.db.Operator.Core.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
_, err = tx.NewDelete().
Model(&RepositoryTag{}).
Where("repository_id =? and tag_id in (?)", repoID, bun.In(tagIDs)).
Exec(ctx)
return err
})
_, err = ts.db.Operator.Core.NewDelete().
Model(&RepositoryTag{}).
Where("repository_id =? and tag_id in (?)", repoID, bun.In(tagIDs)).
Exec(ctx)

return err
}
Expand All @@ -402,5 +366,5 @@ func (ts *tagStoreImpl) FindOrCreate(ctx context.Context, tag Tag) (*Tag, error)
if err != nil {
return nil, err
}
return &resTag, err
return &tag, err
Rader marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading