Skip to content

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: add notification repo unit test

LinkinStarf74c46802183
diff --git a/internal/repo/meta/meta_repo.go b/internal/repo/meta/meta_repo.go
index 8a3ea7a4..415ad428 100644
--- a/internal/repo/meta/meta_repo.go
+++ b/internal/repo/meta/meta_repo.go
@@ -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
-}
diff --git a/internal/repo/repo_test/notification_repo_test.go b/internal/repo/repo_test/notification_repo_test.go
new file mode 100644
index 00000000..e07255a5
--- /dev/null
+++ b/internal/repo/repo_test/notification_repo_test.go
@@ -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)
+}
diff --git a/internal/repo/unique/uniqid_repo.go b/internal/repo/unique/uniqid_repo.go
index 5af8e028..c460d904 100644
--- a/internal/repo/unique/uniqid_repo.go
+++ b/internal/repo/unique/uniqid_repo.go
@@ -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) {
diff --git a/internal/service/unique/uniqid_service.go b/internal/service/unique/uniqid_service.go
index a8bdc02c..bca5e209 100644
--- a/internal/service/unique/uniqid_service.go
+++ b/internal/service/unique/uniqid_service.go
@@ -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)
}