feat: add tag common repo unit test

This commit is contained in:
LinkinStar 2022-11-18 20:11:19 +08:00
parent 432f8c3309
commit 7868adcd7e
3 changed files with 29 additions and 23 deletions

View File

@ -26,7 +26,7 @@ COPY . ${BUILD_DIR}
WORKDIR ${BUILD_DIR} WORKDIR ${BUILD_DIR}
COPY --from=node-builder /tmp/build ${BUILD_DIR}/ui/build COPY --from=node-builder /tmp/build ${BUILD_DIR}/ui/build
RUN apk --no-cache add build-base git \ RUN apk --no-cache add build-base git \
&& make clean build \ && make clean test build \
&& cp answer /usr/bin/answer && cp answer /usr/bin/answer
RUN mkdir -p /data/uploads && chmod 777 /data/uploads \ RUN mkdir -p /data/uploads && chmod 777 /data/uploads \

View File

@ -26,7 +26,7 @@ generate:
go mod tidy go mod tidy
test: test:
@$(GO) test ./... @$(GO) test ./internal/repo/repo_test
# clean all build result # clean all build result
clean: clean:

View File

@ -8,6 +8,7 @@ import (
"github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/repo/tag" "github.com/answerdev/answer/internal/repo/tag"
"github.com/answerdev/answer/internal/repo/tag_common"
"github.com/answerdev/answer/internal/repo/unique" "github.com/answerdev/answer/internal/repo/unique"
"github.com/answerdev/answer/pkg/converter" "github.com/answerdev/answer/pkg/converter"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -42,8 +43,8 @@ var (
func addTagList() { func addTagList() {
uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource) uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, uniqueIDRepo)
err := tagRepo.AddTagList(context.TODO(), testTagList) err := tagCommonRepo.AddTagList(context.TODO(), testTagList)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -51,9 +52,9 @@ func addTagList() {
func Test_tagRepo_GetTagByID(t *testing.T) { func Test_tagRepo_GetTagByID(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID) gotTag, exist, err := tagCommonRepo.GetTagByID(context.TODO(), testTagList[0].ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName) assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName)
@ -61,9 +62,9 @@ func Test_tagRepo_GetTagByID(t *testing.T) {
func Test_tagRepo_GetTagBySlugName(t *testing.T) { func Test_tagRepo_GetTagBySlugName(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTag, exist, err := tagRepo.GetTagBySlugName(context.TODO(), testTagList[0].SlugName) gotTag, exist, err := tagCommonRepo.GetTagBySlugName(context.TODO(), testTagList[0].SlugName)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName) assert.Equal(t, testTagList[0].SlugName, gotTag.SlugName)
@ -80,36 +81,36 @@ func Test_tagRepo_GetTagList(t *testing.T) {
func Test_tagRepo_GetTagListByIDs(t *testing.T) { func Test_tagRepo_GetTagListByIDs(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTags, err := tagRepo.GetTagListByIDs(context.TODO(), []string{testTagList[0].ID}) gotTags, err := tagCommonRepo.GetTagListByIDs(context.TODO(), []string{testTagList[0].ID})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName) assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
} }
func Test_tagRepo_GetTagListByName(t *testing.T) { func Test_tagRepo_GetTagListByName(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTags, err := tagRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, 1) gotTags, err := tagCommonRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, 1, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName) assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
} }
func Test_tagRepo_GetTagListByNames(t *testing.T) { func Test_tagRepo_GetTagListByNames(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTags, err := tagRepo.GetTagListByNames(context.TODO(), []string{testTagList[0].SlugName}) gotTags, err := tagCommonRepo.GetTagListByNames(context.TODO(), []string{testTagList[0].SlugName})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName) assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
} }
func Test_tagRepo_GetTagPage(t *testing.T) { func Test_tagRepo_GetTagPage(t *testing.T) {
tagOnce.Do(addTagList) tagOnce.Do(addTagList)
tagRepo := tag.NewTagRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTags, _, err := tagRepo.GetTagPage(context.TODO(), 1, 1, &entity.Tag{SlugName: testTagList[0].SlugName}, "") gotTags, _, err := tagCommonRepo.GetTagPage(context.TODO(), 1, 1, &entity.Tag{SlugName: testTagList[0].SlugName}, "")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName) assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
} }
@ -121,7 +122,9 @@ func Test_tagRepo_RemoveTag(t *testing.T) {
err := tagRepo.RemoveTag(context.TODO(), testTagList[1].ID) err := tagRepo.RemoveTag(context.TODO(), testTagList[1].ID)
assert.NoError(t, err) assert.NoError(t, err)
_, exist, err := tagRepo.GetTagBySlugName(context.TODO(), testTagList[1].SlugName) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
_, exist, err := tagCommonRepo.GetTagBySlugName(context.TODO(), testTagList[1].SlugName)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, exist) assert.False(t, exist)
} }
@ -134,21 +137,22 @@ func Test_tagRepo_UpdateTag(t *testing.T) {
err := tagRepo.UpdateTag(context.TODO(), testTagList[0]) err := tagRepo.UpdateTag(context.TODO(), testTagList[0])
assert.NoError(t, err) assert.NoError(t, err)
gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTag, exist, err := tagCommonRepo.GetTagByID(context.TODO(), testTagList[0].ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
assert.Equal(t, testTagList[0].DisplayName, gotTag.DisplayName) assert.Equal(t, testTagList[0].DisplayName, gotTag.DisplayName)
} }
func Test_tagRepo_UpdateTagQuestionCount(t *testing.T) { func Test_tagRepo_UpdateTagQuestionCount(t *testing.T) {
uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo)
testTagList[0].DisplayName = "golang" testTagList[0].DisplayName = "golang"
err := tagRepo.UpdateTagQuestionCount(context.TODO(), testTagList[0].ID, 100) err := tagCommonRepo.UpdateTagQuestionCount(context.TODO(), testTagList[0].ID, 100)
assert.NoError(t, err) assert.NoError(t, err)
gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[0].ID) gotTag, exist, err := tagCommonRepo.GetTagByID(context.TODO(), testTagList[0].ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
assert.Equal(t, 100, gotTag.QuestionCount) assert.Equal(t, 100, gotTag.QuestionCount)
@ -166,7 +170,9 @@ func Test_tagRepo_UpdateTagSynonym(t *testing.T) {
converter.StringToInt64(testTagList[0].ID), testTagList[0].SlugName) converter.StringToInt64(testTagList[0].ID), testTagList[0].SlugName)
assert.NoError(t, err) assert.NoError(t, err)
gotTag, exist, err := tagRepo.GetTagByID(context.TODO(), testTagList[2].ID) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
gotTag, exist, err := tagCommonRepo.GetTagByID(context.TODO(), testTagList[2].ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
assert.Equal(t, testTagList[0].ID, fmt.Sprintf("%d", gotTag.MainTagID)) assert.Equal(t, testTagList[0].ID, fmt.Sprintf("%d", gotTag.MainTagID))