mirror of https://gitee.com/answerdev/answer.git
feat: revision repo test
This commit is contained in:
parent
dcf14df50a
commit
ddb612fac7
|
@ -13,6 +13,7 @@ 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 todo: this field is not used, will be removed in the future
|
||||
Status int `xorm:"not null default 1 INT(11) status"`
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -79,17 +79,6 @@ func (rr *revisionRepo) UpdateObjectRevisionId(ctx context.Context, revision *en
|
|||
return 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 TagID
|
||||
func (rr *revisionRepo) GetLastRevisionByObjectID(ctx context.Context, objectID string) (
|
||||
revision *entity.Revision, exist bool, err error) {
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
// RevisionRepo revision repository
|
||||
type 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)
|
||||
|
|
|
@ -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 all
|
||||
func (rs *RevisionService) GetRevisionList(ctx context.Context, req *schema.GetRevisionListReq) (resp []schema.GetRevisionResp, err error) {
|
||||
var (
|
||||
|
|
Loading…
Reference in New Issue