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: revision repo test
kumfoddb612fac79a
diff --git a/internal/entity/revision_entity.go b/internal/entity/revision_entity.goindex daf59a37..48740ef2 100644--- a/internal/entity/revision_entity.go+++ b/internal/entity/revision_entity.go@@ -13,7 +13,8 @@ type Revision struct {Title string `xorm:"not null default '' VARCHAR(255) title"`Content string `xorm:"not null TEXT content"`Log string `xorm:"VARCHAR(255) log"`- Status int `xorm:"not null default 1 INT(11) status"`+ // Status todo: this field is not used, will be removed in the future+ Status int `xorm:"not null default 1 INT(11) status"`}// TableName revision table namediff --git a/internal/repo/repo_test/revision_repo_test.go b/internal/repo/repo_test/revision_repo_test.gonew file mode 100644index 00000000..fb6e0561--- /dev/null+++ b/internal/repo/repo_test/revision_repo_test.go@@ -0,0 +1,99 @@+package repo_test++import (+ "context"+ "encoding/json"+ "github.com/answerdev/answer/internal/entity"+ "github.com/answerdev/answer/internal/repo/question"+ "github.com/answerdev/answer/internal/repo/revision"+ "github.com/answerdev/answer/internal/repo/unique"+ "github.com/stretchr/testify/assert"+ "testing"+)++var q = &entity.Question{+ ID: "",+ UserID: "1",+ Title: "test",+ OriginalText: "test",+ ParsedText: "test",+ Status: entity.QuestionStatusAvailable,+ ViewCount: 0,+ UniqueViewCount: 0,+ VoteCount: 0,+ AnswerCount: 0,+ CollectionCount: 0,+ FollowCount: 0,+ AcceptedAnswerID: "",+ LastAnswerID: "",+ RevisionID: "0",+}++func getRev(objID, title, content string) *entity.Revision {+ return &entity.Revision{+ ID: "",+ UserID: "1",+ ObjectID: objID,+ Title: title,+ Content: content,+ Log: "add rev",+ }+}++func Test_revisionRepo_AddRevision(t *testing.T) {+ var (+ uniqueIDRepo = unique.NewUniqueIDRepo(testDataSource)+ revisionRepo = revision.NewRevisionRepo(testDataSource, uniqueIDRepo)+ questionRepo = question.NewQuestionRepo(testDataSource, uniqueIDRepo)+ )++ // create question+ err := questionRepo.AddQuestion(context.TODO(), q)+ assert.NoError(t, err)+ assert.NotEqual(t, "", q.ID)++ content, err := json.Marshal(q)+ // auto update false+ rev := getRev(q.ID, q.Title, string(content))+ err = revisionRepo.AddRevision(context.TODO(), rev, false)+ assert.NoError(t, err)+ qr, _, _ := questionRepo.GetQuestion(context.TODO(), q.ID)+ assert.NotEqual(t, rev.ID, qr.RevisionID)++ // auto update false+ rev = getRev(q.ID, q.Title, string(content))+ err = revisionRepo.AddRevision(context.TODO(), rev, true)+ assert.NoError(t, err)+ qr, _, _ = questionRepo.GetQuestion(context.TODO(), q.ID)+ assert.Equal(t, rev.ID, qr.RevisionID)++ // recovery+ t.Cleanup(func() {+ err = questionRepo.RemoveQuestion(context.TODO(), q.ID)+ assert.NoError(t, err)+ })+}++func Test_revisionRepo_GetLastRevisionByObjectID(t *testing.T) {+ var (+ uniqueIDRepo = unique.NewUniqueIDRepo(testDataSource)+ revisionRepo = revision.NewRevisionRepo(testDataSource, uniqueIDRepo)+ )++ Test_revisionRepo_AddRevision(t)+ rev, exists, err := revisionRepo.GetLastRevisionByObjectID(context.TODO(), q.ID)+ assert.NoError(t, err)+ assert.True(t, exists)+ assert.NotNil(t, rev)+}++func Test_revisionRepo_GetRevisionList(t *testing.T) {+ var (+ uniqueIDRepo = unique.NewUniqueIDRepo(testDataSource)+ revisionRepo = revision.NewRevisionRepo(testDataSource, uniqueIDRepo)+ )+ Test_revisionRepo_AddRevision(t)+ revs, err := revisionRepo.GetRevisionList(context.TODO(), &entity.Revision{ObjectID: q.ID})+ assert.NoError(t, err)+ assert.GreaterOrEqual(t, len(revs), 1)+}diff --git a/internal/repo/revision/revision_repo.go b/internal/repo/revision/revision_repo.goindex 4529caed..f5b97dd0 100644--- a/internal/repo/revision/revision_repo.go+++ b/internal/repo/revision/revision_repo.go@@ -79,17 +79,6 @@ func (rr *revisionRepo) UpdateObjectRevisionId(ctx context.Context, revision *enreturn nil}-// GetRevision get revision one-func (rr *revisionRepo) GetRevision(ctx context.Context, id string) (- revision *entity.Revision, exist bool, err error) {- revision = &entity.Revision{}- exist, err = rr.data.DB.ID(id).Get(revision)- if err != nil {- return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()- }- return-}-// GetLastRevisionByObjectID get object's last revision by object TagIDfunc (rr *revisionRepo) GetLastRevisionByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error) {diff --git a/internal/service/revision/revision.go b/internal/service/revision/revision.goindex 333d8379..1d5d999c 100644--- a/internal/service/revision/revision.go+++ b/internal/service/revision/revision.go@@ -9,7 +9,6 @@ import (// RevisionRepo revision repositorytype RevisionRepo interface {AddRevision(ctx context.Context, revision *entity.Revision, autoUpdateRevisionID bool) (err error)- GetRevision(ctx context.Context, id string) (revision *entity.Revision, exist bool, err error)GetLastRevisionByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error)GetRevisionList(ctx context.Context, revision *entity.Revision) (revisionList []entity.Revision, err error)UpdateObjectRevisionId(ctx context.Context, revision *entity.Revision, session *xorm.Session) (err error)diff --git a/internal/service/revision_service.go b/internal/service/revision_service.goindex 1e9b21d6..bb5443a3 100644--- a/internal/service/revision_service.go+++ b/internal/service/revision_service.go@@ -5,14 +5,12 @@ import ("encoding/json""github.com/answerdev/answer/internal/base/constant"- "github.com/answerdev/answer/internal/base/reason""github.com/answerdev/answer/internal/entity""github.com/answerdev/answer/internal/schema"questioncommon "github.com/answerdev/answer/internal/service/question_common""github.com/answerdev/answer/internal/service/revision"usercommon "github.com/answerdev/answer/internal/service/user_common""github.com/jinzhu/copier"- "github.com/segmentfault/pacman/errors")// RevisionService user service@@ -36,31 +34,6 @@ func NewRevisionService(}}-// GetRevision get revision one-func (rs *RevisionService) GetRevision(ctx context.Context, id string) (resp schema.GetRevisionResp, err error) {- var (- rev *entity.Revision- exists bool- )-- resp = schema.GetRevisionResp{}-- rev, exists, err = rs.revisionRepo.GetRevision(ctx, id)- if err != nil {- return- }-- if !exists {- err = errors.BadRequest(reason.ObjectNotFound)- return- }-- _ = copier.Copy(&resp, rev)- rs.parseItem(ctx, &resp)-- return-}-// GetRevisionList get revision list allfunc (rs *RevisionService) GetRevisionList(ctx context.Context, req *schema.GetRevisionListReq) (resp []schema.GetRevisionResp, err error) {var (