Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: add tag repo unit test

LinkinStardcf14df50a37
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index d2d0a5db..7d5331f2 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -132,7 +132,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
followController := controller.NewFollowController(followService)
collectionRepo := collection.NewCollectionRepo(dataData, uniqueIDRepo)
collectionGroupRepo := collection.NewCollectionGroupRepo(dataData)
- tagRelRepo := tag.NewTagListRepo(dataData)
+ tagRelRepo := tag.NewTagRelRepo(dataData)
tagCommonService := tagcommon.NewTagCommonService(tagRepo, tagRelRepo, revisionService)
collectionCommon := collectioncommon.NewCollectionCommon(collectionRepo)
answerCommon := answercommon.NewAnswerCommon(answerRepo)
diff --git a/internal/repo/provider.go b/internal/repo/provider.go
index 6dcbaa6a..54d43c10 100644
--- a/internal/repo/provider.go
+++ b/internal/repo/provider.go
@@ -53,7 +53,7 @@ var ProviderSetRepo = wire.NewSet(
activity.NewQuestionActivityRepo,
activity.NewUserActiveActivityRepo,
tag.NewTagRepo,
- tag.NewTagListRepo,
+ tag.NewTagRelRepo,
collection.NewCollectionRepo,
collection.NewCollectionGroupRepo,
auth.NewAuthRepo,
diff --git a/internal/repo/repo_test/tag_rel_repo_test.go b/internal/repo/repo_test/tag_rel_repo_test.go
new file mode 100644
index 00000000..4df8138a
--- /dev/null
+++ b/internal/repo/repo_test/tag_rel_repo_test.go
@@ -0,0 +1,32 @@
+package repo_test
+
+import (
+ "testing"
+)
+
+func Test_tagListRepo_AddTagRelList(t *testing.T) {
+}
+
+func Test_tagListRepo_BatchGetObjectTagRelList(t *testing.T) {
+
+}
+
+func Test_tagListRepo_CountTagRelByTagID(t *testing.T) {
+
+}
+
+func Test_tagListRepo_EnableTagRelByIDs(t *testing.T) {
+
+}
+
+func Test_tagListRepo_GetObjectTagRelList(t *testing.T) {
+
+}
+
+func Test_tagListRepo_GetObjectTagRelWithoutStatus(t *testing.T) {
+
+}
+
+func Test_tagListRepo_RemoveTagRelListByIDs(t *testing.T) {
+
+}
diff --git a/internal/repo/repo_test/tag_repo_test.go b/internal/repo/repo_test/tag_repo_test.go
new file mode 100644
index 00000000..9c2e012a
--- /dev/null
+++ b/internal/repo/repo_test/tag_repo_test.go
@@ -0,0 +1,173 @@
+package repo_test
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "testing"
+
+ "github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/internal/repo/tag"
+ "github.com/answerdev/answer/internal/repo/unique"
+ "github.com/answerdev/answer/pkg/converter"
+ "github.com/stretchr/testify/assert"
+)
+
+var (
+ tagOnce sync.Once
+ testTagList = []*entity.Tag{
+ {
+ SlugName: "go",
+ DisplayName: "Golang",
+ OriginalText: "golang",
+ ParsedText: "<p>golang</p>",
+ Status: entity.TagStatusAvailable,
+ },
+ {
+ SlugName: "js",
+ DisplayName: "javascript",
+ OriginalText: "javascript",
+ ParsedText: "<p>javascript</p>",
+ Status: entity.TagStatusAvailable,
+ },
+ {
+ SlugName: "go2",
+ DisplayName: "Golang2",
+ OriginalText: "golang2",
+ ParsedText: "<p>golang2</p>",
+ Status: entity.TagStatusAvailable,
+ },
+ }
+)
+
+func addTagList() {
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
+ err := tagRepo.AddTagList(context.TODO(), testTagList)
+ if err != nil {
+ panic(err)
+ }
+}
+
+func Test_tagRepo_GetTagByID(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID)
+ assert.NoError(t, err)
+ assert.True(t, exist)
+ assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName)
+}
+
+func Test_tagRepo_GetTagBySlugName(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTag, exist, err := tagRepo.GetTagBySlugName(context.TODO(), testTagList[0].SlugName)
+ assert.NoError(t, err)
+ assert.True(t, exist)
+ assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName)
+}
+
+func Test_tagRepo_GetTagList(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTags, err := tagRepo.GetTagList(context.TODO(), &entity.Tag{ID: testTagList[0].ID})
+ assert.NoError(t, err)
+ assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
+}
+
+func Test_tagRepo_GetTagListByIDs(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTags, err := tagRepo.GetTagListByIDs(context.TODO(), []string{testTagList[0].ID})
+ assert.NoError(t, err)
+ assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
+}
+
+func Test_tagRepo_GetTagListByName(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTags, err := tagRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, 1)
+ assert.NoError(t, err)
+ assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
+}
+
+func Test_tagRepo_GetTagListByNames(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTags, err := tagRepo.GetTagListByNames(context.TODO(), []string{testTagList[0].SlugName})
+ assert.NoError(t, err)
+ assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
+}
+
+func Test_tagRepo_GetTagPage(t *testing.T) {
+ tagOnce.Do(addTagList)
+ tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
+
+ gotTags, _, err := tagRepo.GetTagPage(context.TODO(), 1, 1, &entity.Tag{SlugName: testTagList[0].SlugName}, "")
+ assert.NoError(t, err)
+ assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
+}
+
+func Test_tagRepo_RemoveTag(t *testing.T) {
+ tagOnce.Do(addTagList)
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
+ err := tagRepo.RemoveTag(context.TODO(), testTagList[1].ID)
+ assert.NoError(t, err)
+
+ _, exist, err := tagRepo.GetTagBySlugName(context.TODO(), testTagList[1].SlugName)
+ assert.NoError(t, err)
+ assert.False(t, exist)
+}
+
+func Test_tagRepo_UpdateTag(t *testing.T) {
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
+
+ testTagList[0].DisplayName = "golang"
+ err := tagRepo.UpdateTag(context.TODO(), testTagList[0])
+ assert.NoError(t, err)
+
+ gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID)
+ assert.NoError(t, err)
+ assert.True(t, exist)
+ assert.Equal(t, testTagList[0].DisplayName, gotTag.DisplayName)
+}
+
+func Test_tagRepo_UpdateTagQuestionCount(t *testing.T) {
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
+
+ testTagList[0].DisplayName = "golang"
+ err := tagRepo.UpdateTagQuestionCount(context.TODO(), testTagList[0].ID, 100)
+ assert.NoError(t, err)
+
+ gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID)
+ assert.NoError(t, err)
+ assert.True(t, exist)
+ assert.Equal(t, 100, gotTag.QuestionCount)
+}
+
+func Test_tagRepo_UpdateTagSynonym(t *testing.T) {
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
+
+ testTagList[0].DisplayName = "golang"
+ err := tagRepo.UpdateTag(context.TODO(), testTagList[0])
+ assert.NoError(t, err)
+
+ err = tagRepo.UpdateTagSynonym(context.TODO(), []string{testTagList[2].SlugName},
+ converter.StringToInt64(testTagList[0].ID), testTagList[0].SlugName)
+ assert.NoError(t, err)
+
+ gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[2].ID)
+ assert.NoError(t, err)
+ assert.True(t, exist)
+ assert.Equal(t, testTagList[0].ID, fmt.Sprintf("%d", gotTag.MainTagID))
+}
diff --git a/internal/repo/tag/tag_rel_repo.go b/internal/repo/tag/tag_rel_repo.go
index c54bf8c0..0996f7f6 100644
--- a/internal/repo/tag/tag_rel_repo.go
+++ b/internal/repo/tag/tag_rel_repo.go
@@ -10,20 +10,20 @@ import (
"github.com/segmentfault/pacman/errors"
)
-// tagListRepo tagList repository
-type tagListRepo struct {
+// tagRelRepo tag rel repository
+type tagRelRepo struct {
data *data.Data
}
-// NewTagListRepo new repository
-func NewTagListRepo(data *data.Data) tagcommon.TagRelRepo {
- return &tagListRepo{
+// NewTagRelRepo new repository
+func NewTagRelRepo(data *data.Data) tagcommon.TagRelRepo {
+ return &tagRelRepo{
data: data,
}
}
// AddTagRelList add tag list
-func (tr *tagListRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) {
+func (tr *tagRelRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) {
_, err = tr.data.DB.Insert(tagList)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -31,17 +31,8 @@ func (tr *tagListRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagR
return
}
-// RemoveTagRelListByObjectID delete tag list
-func (tr *tagListRepo) RemoveTagRelListByObjectID(ctx context.Context, objectId string) (err error) {
- _, err = tr.data.DB.Where("object_id = ?", objectId).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
- if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- }
- return
-}
-
// RemoveTagRelListByIDs delete tag list
-func (tr *tagListRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) {
+func (tr *tagRelRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) {
_, err = tr.data.DB.In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -50,7 +41,7 @@ func (tr *tagListRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (
}
// GetObjectTagRelWithoutStatus get object tag relation no matter status
-func (tr *tagListRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectId, tagID string) (
+func (tr *tagRelRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectId, tagID string) (
tagRel *entity.TagRel, exist bool, err error) {
tagRel = &entity.TagRel{}
session := tr.data.DB.Where("object_id = ?", objectId).And("tag_id = ?", tagID)
@@ -62,7 +53,7 @@ func (tr *tagListRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectI
}
// EnableTagRelByIDs update tag status to available
-func (tr *tagListRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err error) {
+func (tr *tagRelRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err error) {
_, err = tr.data.DB.In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusAvailable})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -71,7 +62,7 @@ func (tr *tagListRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err
}
// GetObjectTagRelList get object tag relation list all
-func (tr *tagListRepo) GetObjectTagRelList(ctx context.Context, objectId string) (tagListList []*entity.TagRel, err error) {
+func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectId string) (tagListList []*entity.TagRel, err error) {
tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.Where("object_id = ?", objectId)
session.Where("status = ?", entity.TagRelStatusAvailable)
@@ -83,7 +74,7 @@ func (tr *tagListRepo) GetObjectTagRelList(ctx context.Context, objectId string)
}
// BatchGetObjectTagRelList get object tag relation list all
-func (tr *tagListRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []string) (tagListList []*entity.TagRel, err error) {
+func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []string) (tagListList []*entity.TagRel, err error) {
tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.In("object_id", objectIds)
session.Where("status = ?", entity.TagRelStatusAvailable)
@@ -95,7 +86,7 @@ func (tr *tagListRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds [
}
// CountTagRelByTagID count tag relation
-func (tr *tagListRepo) CountTagRelByTagID(ctx context.Context, tagID string) (count int64, err error) {
+func (tr *tagRelRepo) CountTagRelByTagID(ctx context.Context, tagID string) (count int64, err error) {
count, err = tr.data.DB.Count(&entity.TagRel{TagID: tagID, Status: entity.AnswerStatusAvailable})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go
index dd8353eb..14e438ff 100644
--- a/internal/service/tag_common/tag_common.go
+++ b/internal/service/tag_common/tag_common.go
@@ -30,7 +30,6 @@ type TagRepo interface {
type TagRelRepo interface {
AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error)
RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error)
- RemoveTagRelListByObjectID(ctx context.Context, objectId string) (err error)
EnableTagRelByIDs(ctx context.Context, ids []int64) (err error)
GetObjectTagRelWithoutStatus(ctx context.Context, objectId, tagID string) (tagRel *entity.TagRel, exist bool, err error)
GetObjectTagRelList(ctx context.Context, objectId string) (tagListList []*entity.TagRel, err error)