feat: add notification repo unit test

This commit is contained in:
LinkinStar 2022-10-26 18:01:01 +08:00
parent 863a9d91ca
commit f74c468021
4 changed files with 104 additions and 27 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/meta"
@ -71,13 +70,3 @@ func (mr *metaRepo) GetMetaList(ctx context.Context, meta *entity.Meta) (metaLis
}
return
}
// GetMetaPage get meta page
func (mr *metaRepo) GetMetaPage(ctx context.Context, page, pageSize int, meta *entity.Meta) (metaList []*entity.Meta, total int64, err error) {
metaList = make([]*entity.Meta, 0)
total, err = pager.Help(page, pageSize, metaList, meta, mr.data.DB.NewSession())
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}

View File

@ -0,0 +1,104 @@
package repo_test
import (
"context"
"testing"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/repo/notification"
"github.com/answerdev/answer/internal/schema"
"github.com/stretchr/testify/assert"
)
func buildNotificationEntity() *entity.Notification {
return &entity.Notification{
UserID: "1",
ObjectID: "1",
Content: "1",
Type: schema.NotificationTypeInbox,
IsRead: schema.NotificationNotRead,
Status: schema.NotificationStatusNormal,
}
}
func Test_notificationRepo_ClearIDUnRead(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
err = notificationRepo.ClearIDUnRead(context.TODO(), ent.UserID, ent.ID)
assert.NoError(t, err)
got, exists, err := notificationRepo.GetById(context.TODO(), ent.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, schema.NotificationRead, got.IsRead)
}
func Test_notificationRepo_ClearUnRead(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
err = notificationRepo.ClearUnRead(context.TODO(), ent.UserID, ent.Type)
assert.NoError(t, err)
got, exists, err := notificationRepo.GetById(context.TODO(), ent.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, schema.NotificationRead, got.IsRead)
}
func Test_notificationRepo_GetById(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
got, exists, err := notificationRepo.GetById(context.TODO(), ent.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, got.ID, ent.ID)
}
func Test_notificationRepo_GetByUserIdObjectIdTypeId(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
got, exists, err := notificationRepo.GetByUserIdObjectIdTypeId(context.TODO(), ent.UserID, ent.ObjectID, ent.Type)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, got.ObjectID, ent.ObjectID)
}
func Test_notificationRepo_GetNotificationPage(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
notificationPage, total, err := notificationRepo.GetNotificationPage(context.TODO(), &schema.NotificationSearch{UserID: userID})
assert.NoError(t, err)
assert.True(t, total > 0)
assert.Equal(t, notificationPage[0].UserID, ent.UserID)
}
func Test_notificationRepo_UpdateNotificationContent(t *testing.T) {
notificationRepo := notification.NewNotificationRepo(testDataSource)
ent := buildNotificationEntity()
err := notificationRepo.AddNotification(context.TODO(), ent)
assert.NoError(t, err)
ent.Content = "test"
err = notificationRepo.UpdateNotificationContent(context.TODO(), ent)
assert.NoError(t, err)
got, exists, err := notificationRepo.GetById(context.TODO(), ent.ID)
assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, got.Content, ent.Content)
}

View File

@ -3,7 +3,6 @@ package unique
import (
"context"
"fmt"
"strconv"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
@ -25,20 +24,6 @@ func NewUniqueIDRepo(data *data.Data) unique.UniqueIDRepo {
}
}
// GenUniqueID generate unique id
// 1 + 00x(objectType) + 000000000000x(id)
func (ur *uniqueIDRepo) GenUniqueID(ctx context.Context, key string) (uniqueID int64, err error) {
idStr, err := ur.GenUniqueIDStr(ctx, key)
if err != nil {
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
uniqueID, err = strconv.ParseInt(idStr, 10, 64)
if err != nil {
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return uniqueID, nil
}
// GenUniqueIDStr generate unique id string
// 1 + 00x(objectType) + 000000000000x(id)
func (ur *uniqueIDRepo) GenUniqueIDStr(ctx context.Context, key string) (uniqueID string, err error) {

View File

@ -6,6 +6,5 @@ import (
// UniqueIDRepo unique id repository
type UniqueIDRepo interface {
GenUniqueID(ctx context.Context, key string) (uniqueID int64, err error)
GenUniqueIDStr(ctx context.Context, key string) (uniqueID string, err error)
}