sourcegraph/internal/codeintel/codenav/service_test.go
Varun Gandhi 99f7d97dc6
chore: Switch over to fake RepoStore in codenav tests (#64284)
Reduce the exposed surface area with a smaller interface
minimalRepoStore, and make sure we're using fakes in the
tests which better document the intent, instead of using a
mock repo store. The fake makes sure that the methods
are mutually consistent, which is easier to do when working
with a smaller interface.
2024-08-06 22:09:43 +08:00

57 lines
1.5 KiB
Go

package codenav
import (
"context"
"fmt"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/database"
internaltypes "github.com/sourcegraph/sourcegraph/internal/types"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type AllPresentFakeRepoStore struct{}
var _ minimalRepoStore = AllPresentFakeRepoStore{}
func (s AllPresentFakeRepoStore) Get(_ context.Context, id api.RepoID) (*internaltypes.Repo, error) {
return &internaltypes.Repo{ID: id, Name: api.RepoName(fmt.Sprintf("r%d", id))}, nil
}
func (s AllPresentFakeRepoStore) GetReposSetByIDs(ctx context.Context, ids ...api.RepoID) (map[api.RepoID]*internaltypes.Repo, error) {
out := map[api.RepoID]*internaltypes.Repo{}
for _, id := range ids {
r, _ := s.Get(ctx, id) // Get doesn't error so this is OK
out[id] = r
}
return out, nil
}
type FakeMinimalRepoStore struct {
data map[api.RepoID]*internaltypes.Repo
}
var _ minimalRepoStore = FakeMinimalRepoStore{}
func (f FakeMinimalRepoStore) Get(ctx context.Context, id api.RepoID) (*internaltypes.Repo, error) {
if r, ok := f.data[id]; ok {
return r, nil
}
return nil, &database.RepoNotFoundErr{ID: id}
}
func (f FakeMinimalRepoStore) GetReposSetByIDs(ctx context.Context, ids ...api.RepoID) (map[api.RepoID]*internaltypes.Repo, error) {
out := map[api.RepoID]*internaltypes.Repo{}
for _, id := range ids {
r, err := f.Get(ctx, id)
if err != nil {
if errors.Is(err, &database.RepoNotFoundErr{}) {
continue
}
return nil, err
}
out[id] = r
}
return out, nil
}