Skip to content

Commit

Permalink
add namespace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz committed Oct 11, 2024
1 parent d606f6b commit 50ed56e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
38 changes: 28 additions & 10 deletions storage/data/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,6 @@ func (suite *baseTestSuite) TestItems() {
Labels: []any{"b"},
Comment: "comment 8",
},
// Items with different namespace
{
Namespace: "xxx",
ItemId: "0",
IsHidden: true,
Categories: []string{"a"},
Timestamp: time.Date(1996, 3, 15, 0, 0, 0, 0, time.UTC),
Labels: []any{"a"},
Comment: "comment 0",
},
}
// Insert item
err := suite.Database.BatchInsertItems(ctx, items)
Expand Down Expand Up @@ -749,6 +739,34 @@ func (suite *baseTestSuite) TestPurge() {
suite.NoError(err)
}

func (suite *baseTestSuite) TestNamespace() {
// insert items
items := []Item{
{Namespace: "namespace1", ItemId: "0"},
{Namespace: "namespace1", ItemId: "1"},
{Namespace: "namespace2", ItemId: "0"},
}
err := suite.Database.BatchInsertItems(context.Background(), items)
suite.NoError(err)
// get items
ret := suite.getItems(context.Background(), 3)
suite.Equal(items, ret)

// insert feedbacks
feedbacks := []Feedback{
{FeedbackKey: FeedbackKey{"namespace1", "type1", "0", "0"}},
{FeedbackKey: FeedbackKey{"namespace1", "type1", "0", "1"}},
{FeedbackKey: FeedbackKey{"namespace2", "type1", "0", "0"}},
{FeedbackKey: FeedbackKey{"namespace3", "type1", "0", "0"}},
{FeedbackKey: FeedbackKey{"namespace4", "type1", "0", "0"}},
}
err = suite.Database.BatchInsertFeedback(context.Background(), feedbacks, true, true, true)
suite.NoError(err)
// get feedback
retFeedbacks := suite.getFeedback(context.Background(), 3, nil, lo.ToPtr(time.Now()))
suite.Equal(feedbacks, retFeedbacks)
}

func TestSortFeedbacks(t *testing.T) {
feedback := []Feedback{
{FeedbackKey: FeedbackKey{"namespace", "star", "1", "1"}, Timestamp: time.Date(2000, 10, 1, 0, 0, 0, 0, time.UTC)},
Expand Down
5 changes: 3 additions & 2 deletions storage/data/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ func (d *SQLDatabase) GetFeedback(ctx context.Context, cursor string, n int, beg
if err := jsonutil.Unmarshal(buf, &cursorKey); err != nil {
return "", nil, err
}
tx.Where("(feedback_type, user_id, item_id) >= (?,?,?)", cursorKey.FeedbackType, cursorKey.UserId, cursorKey.ItemId)
tx.Where("(namespace, feedback_type, user_id, item_id) >= (?,?,?,?)",
cursorKey.Namespace, cursorKey.FeedbackType, cursorKey.UserId, cursorKey.ItemId)
}
if len(feedbackTypes) > 0 {
tx.Where("feedback_type IN ?", feedbackTypes)
Expand All @@ -750,7 +751,7 @@ func (d *SQLDatabase) GetFeedback(ctx context.Context, cursor string, n int, beg
if endTime != nil {
tx.Where("time_stamp <= ?", d.convertTimeZone(endTime))
}
tx.Order("feedback_type, user_id, item_id").Limit(n + 1)
tx.Order("namespace, feedback_type, user_id, item_id").Limit(n + 1)
result, err := tx.Rows()
if err != nil {
return "", nil, errors.Trace(err)
Expand Down

0 comments on commit 50ed56e

Please sign in to comment.