Skip to content

Commit

Permalink
fix insert duplicate users/items/feedback (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz authored May 25, 2022
1 parent c25ebfe commit d3551a2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 249 deletions.
13 changes: 6 additions & 7 deletions storage/data/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,16 @@ import (
var (
ErrUserNotExist = errors.NotFoundf("user")
ErrItemNotExist = errors.NotFoundf("item")
ErrUnsupported = errors.NotSupportedf("interface")
ErrNoDatabase = errors.NotAssignedf("database")
)

// Item stores meta data about item.
type Item struct {
ItemId string
ItemId string `gorm:"primaryKey"`
IsHidden bool
Categories []string `gorm:"-"`
Categories []string `gorm:"serializer:json"`
Timestamp time.Time
Labels []string `gorm:"-"`
Labels []string `gorm:"serializer:json"`
Comment string
}

Expand All @@ -64,9 +63,9 @@ type ItemPatch struct {

// User stores meta data about user.
type User struct {
UserId string
Labels []string `gorm:"-"`
Subscribe []string `gorm:"-"`
UserId string `gorm:"primaryKey"`
Labels []string `gorm:"serializer:json"`
Subscribe []string `gorm:"serializer:json"`
Comment string
}

Expand Down
15 changes: 15 additions & 0 deletions storage/data/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func testUsers(t *testing.T, db Database) {
// test insert empty
err = db.BatchInsertUsers(nil)
assert.NoError(t, err)

// insert duplicate users
err = db.BatchInsertUsers([]User{{UserId: "1"}, {UserId: "1"}})
assert.NoError(t, err)
}

func testFeedback(t *testing.T, db Database) {
Expand Down Expand Up @@ -333,6 +337,13 @@ func testFeedback(t *testing.T, db Database) {
{FeedbackKey: FeedbackKey{"a", "100", "200"}},
}, false, false, false)
assert.NoError(t, err)

// insert duplicate feedback
err = db.BatchInsertFeedback([]Feedback{
{FeedbackKey: FeedbackKey{"a", "0", "0"}},
{FeedbackKey: FeedbackKey{"a", "0", "0"}},
}, true, true, true)
assert.NoError(t, err)
}

func testItems(t *testing.T, db Database) {
Expand Down Expand Up @@ -435,6 +446,10 @@ func testItems(t *testing.T, db Database) {
items, err = db.BatchGetItems(nil)
assert.NoError(t, err)
assert.Empty(t, items)

// test insert duplicate items
err = db.BatchInsertItems([]Item{{ItemId: "1"}, {ItemId: "1"}})
assert.NoError(t, err)
}

func testDeleteUser(t *testing.T, db Database) {
Expand Down
Loading

0 comments on commit d3551a2

Please sign in to comment.