From 7868adcd7ebcd4e3b970a604fde78ac2d3f21c18 Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Fri, 18 Nov 2022 20:11:19 +0800 Subject: [PATCH] feat: add tag common repo unit test --- Dockerfile | 2 +- Makefile | 2 +- internal/repo/repo_test/tag_repo_test.go | 48 +++++++++++++----------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac06839a..e0f1eb3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,7 @@ COPY . ${BUILD_DIR} WORKDIR ${BUILD_DIR} COPY --from=node-builder /tmp/build ${BUILD_DIR}/ui/build RUN apk --no-cache add build-base git \ - && make clean build \ + && make clean test build \ && cp answer /usr/bin/answer RUN mkdir -p /data/uploads && chmod 777 /data/uploads \ diff --git a/Makefile b/Makefile index f98526b7..1ecff1c9 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ generate: go mod tidy test: - @$(GO) test ./... + @$(GO) test ./internal/repo/repo_test # clean all build result clean: diff --git a/internal/repo/repo_test/tag_repo_test.go b/internal/repo/repo_test/tag_repo_test.go index 9c2e012a..d659651c 100644 --- a/internal/repo/repo_test/tag_repo_test.go +++ b/internal/repo/repo_test/tag_repo_test.go @@ -8,6 +8,7 @@ import ( "github.com/answerdev/answer/internal/entity" "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/pkg/converter" "github.com/stretchr/testify/assert" @@ -42,8 +43,8 @@ var ( func addTagList() { uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource) - tagRepo := tag.NewTagRepo(testDataSource, uniqueIDRepo) - err := tagRepo.AddTagList(context.TODO(), testTagList) + tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, uniqueIDRepo) + err := tagCommonRepo.AddTagList(context.TODO(), testTagList) if err != nil { panic(err) } @@ -51,9 +52,9 @@ func addTagList() { func Test_tagRepo_GetTagByID(t *testing.T) { 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.True(t, exist) 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) { 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.True(t, exist) 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) { 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.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)) + 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.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)) + 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.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)) + 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.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) 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.False(t, exist) } @@ -134,21 +137,22 @@ func Test_tagRepo_UpdateTag(t *testing.T) { err := tagRepo.UpdateTag(context.TODO(), testTagList[0]) 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.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) + tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) 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) - 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.True(t, exist) 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) 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.True(t, exist) assert.Equal(t, testTagList[0].ID, fmt.Sprintf("%d", gotTag.MainTagID))