Skip to content

Commit

Permalink
Fix FindDocInfosByKeys when keys is empty (#945)
Browse files Browse the repository at this point in the history
Fix the mongodb query keys slice to return an empty slice if it is
empty. Currently, FindDocInfosByKeys throws a query error when passing
an empty slice of keys.

---------

Co-authored-by: Youngteac Hong <[email protected]>
  • Loading branch information
blurfx and hackerwins authored Jul 26, 2024
1 parent b1f9e00 commit bcb246b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ func (c *Client) FindDocInfosByKeys(
projectID types.ID,
docKeys []key.Key,
) ([]*database.DocInfo, error) {
if len(docKeys) == 0 {
return nil, nil
}
filter := bson.M{
"project_id": projectID,
"key": bson.M{
Expand Down
21 changes: 21 additions & 0 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ func RunFindDocInfosByKeysTest(
assert.Len(t, infos, len(docKeys))
})

t.Run("find docInfos by empty key slice test", func(t *testing.T) {
ctx := context.Background()
clientInfo, err := db.ActivateClient(ctx, projectID, t.Name())
assert.NoError(t, err)

// 01. Create documents
docKeys := []key.Key{
"test", "test$3", "test123", "test$0",
"search$test", "abcde", "test abc",
}
for _, docKey := range docKeys {
_, err := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.NoError(t, err)
}

// 02. Find documents
infos, err := db.FindDocInfosByKeys(ctx, projectID, []key.Key{})
assert.NoError(t, err)
assert.Len(t, infos, 0)
})

t.Run("find docInfos by keys where some keys are not found test", func(t *testing.T) {
ctx := context.Background()
clientInfo, err := db.ActivateClient(ctx, projectID, t.Name())
Expand Down

0 comments on commit bcb246b

Please sign in to comment.