Skip to content

Commit

Permalink
Modify logic so that MemDB and MongoDB behave same for non-existent keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kokodak committed Jul 18, 2024
1 parent eb1425d commit d847567
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func (d *DB) FindDocInfosByKeys(
return nil, fmt.Errorf("find doc info by key: %w", err)
}
if info == nil {
return nil, fmt.Errorf("%s: %w", k, database.ErrDocumentNotFound)
continue
}

infos = append(infos, info.DeepCopy())
Expand Down
30 changes: 30 additions & 0 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ func RunFindDocInfosByKeysTest(
assert.ElementsMatch(t, docKeys, actualKeys)
assert.Len(t, infos, len(docKeys))
})

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())
assert.NoError(t, err)

// 01. Create documents
docKeys := []key.Key{
"exist-key1", "exist-key2", "exist-key3",
}
for _, docKey := range docKeys {
_, err := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.NoError(t, err)
}

// 02. append a key that does not exist
docKeysWithNonExistKey := append(docKeys, "non-exist-key")

// 03. Find documents
infos, err := db.FindDocInfosByKeys(ctx, projectID, docKeysWithNonExistKey)
assert.NoError(t, err)

actualKeys := make([]key.Key, len(infos))
for i, info := range infos {
actualKeys[i] = info.Key
}

assert.ElementsMatch(t, docKeys, actualKeys)
assert.Len(t, infos, len(docKeys))
})
}

// RunFindProjectInfoBySecretKeyTest runs the FindProjectInfoBySecretKey test for the given db.
Expand Down

0 comments on commit d847567

Please sign in to comment.