corso/src/pkg/selectors/helpers_test.go
Keepers c73ca6a4e5
correct paths in selectors (#789)
The path package changed the standard format
of fullPath and repoRef design.  This should have
failed tests before being pushed to main, but was
able to slip in falsely while github actions were
configured to pass all tests until failed.
2022-09-07 14:14:51 -06:00

157 lines
4.0 KiB
Go

package selectors
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/alcionai/corso/internal/path"
"github.com/alcionai/corso/pkg/backup/details"
"github.com/alcionai/corso/pkg/filters"
)
// ---------------------------------------------------------------------------
// categorizers
// ---------------------------------------------------------------------------
// categorizer
type mockCategorizer string
const (
unknownCatStub mockCategorizer = ""
rootCatStub mockCategorizer = "rootCatStub"
leafCatStub mockCategorizer = "leafCatStub"
)
var _ categorizer = unknownCatStub
func (mc mockCategorizer) String() string {
return string(mc)
}
func (mc mockCategorizer) leafCat() categorizer {
return mc
}
func (mc mockCategorizer) rootCat() categorizer {
return rootCatStub
}
func (mc mockCategorizer) unknownCat() categorizer {
return unknownCatStub
}
func (mc mockCategorizer) pathValues(pth []string) map[categorizer]string {
return map[categorizer]string{rootCatStub: "stub"}
}
func (mc mockCategorizer) pathKeys() []categorizer {
return []categorizer{rootCatStub, leafCatStub}
}
func stubPathValues() map[categorizer]string {
return map[categorizer]string{
rootCatStub: rootCatStub.String(),
leafCatStub: leafCatStub.String(),
}
}
// ---------------------------------------------------------------------------
// scopers
// ---------------------------------------------------------------------------
// scoper
type mockScope scope
var _ scoper = &mockScope{}
func (ms mockScope) categorizer() categorizer {
switch ms[scopeKeyCategory].Target {
case rootCatStub.String():
return rootCatStub
case leafCatStub.String():
return leafCatStub
}
return unknownCatStub
}
func (ms mockScope) matchesEntry(
cat categorizer,
pathValues map[categorizer]string,
entry details.DetailsEntry,
) bool {
return ms[shouldMatch].Target == "true"
}
func (ms mockScope) setDefaults() {}
const (
shouldMatch = "should-match-entry"
stubResource = "stubResource"
)
// helper funcs
func stubScope(match string) mockScope {
sm := "true"
if len(match) > 0 {
sm = match
}
return mockScope{
rootCatStub.String(): passAny,
scopeKeyCategory: filters.Identity(rootCatStub.String()),
scopeKeyGranularity: filters.Identity(Item),
scopeKeyResource: filters.Identity(stubResource),
scopeKeyDataType: filters.Identity(rootCatStub.String()),
shouldMatch: filters.Identity(sm),
}
}
// ---------------------------------------------------------------------------
// selectors
// ---------------------------------------------------------------------------
func stubSelector() Selector {
return Selector{
Service: ServiceExchange,
Excludes: []scope{scope(stubScope(""))},
Filters: []scope{scope(stubScope(""))},
Includes: []scope{scope(stubScope(""))},
}
}
// ---------------------------------------------------------------------------
// helper funcs
// ---------------------------------------------------------------------------
func setScopesToDefault[T scopeT](ts []T) []T {
for _, s := range ts {
s.setDefaults()
}
return ts
}
// calls assert.Equal(t, getCatValue(sc, k)[0], v) on each k:v pair in the map
func scopeMustHave[T scopeT](t *testing.T, sc T, m map[categorizer]string) {
for k, v := range m {
t.Run(k.String(), func(t *testing.T) {
assert.Equal(t, getCatValue(sc, k), split(v), "Key: %s", k)
})
}
}
// stubPath ensures test path production matches that of fullPath design,
// stubbing out static values where necessary.
func stubPath(service path.ServiceType, data path.CategoryType, resourceOwner, folders, item string) []string {
return []string{"tid", service.String(), resourceOwner, data.String(), folders, item}
}
// stubRepoRef ensures test path production matches that of repoRef design,
// stubbing out static values where necessary.
func stubRepoRef(service path.ServiceType, data path.CategoryType, resourceOwner, folders, item string) string {
return strings.Join([]string{"tid", service.String(), resourceOwner, data.String(), folders, item}, "/")
}