corso/src/pkg/path/drive_test.go
ryanfkeepers dfd486cd41 distrubutes ServiceResource throughout path
Adds utilization of the ServiceResource struct to all
of the path package functionality.  In general, this
takes most instances where a service and resource
are requested, and replaces those values with a
slice of ServiceResource tuples.

To keep the review smaller, this change does not update
any packages outside of path.  Therefore the tests and
build are guaranteed to fail.  The next PR, which
updates all other packages with the changes, is
necessary for both PRs to move forward.
2023-08-10 14:57:04 -06:00

123 lines
2.8 KiB
Go

package path_test
import (
"strings"
"testing"
"github.com/alcionai/clues"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
odConsts "github.com/alcionai/corso/src/internal/m365/service/onedrive/consts"
"github.com/alcionai/corso/src/internal/tester"
"github.com/alcionai/corso/src/pkg/path"
)
type OneDrivePathSuite struct {
tester.Suite
}
func TestOneDrivePathSuite(t *testing.T) {
suite.Run(t, &OneDrivePathSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *OneDrivePathSuite) Test_ToOneDrivePath() {
tests := []struct {
name string
pathElements []string
expected *path.DrivePath
errCheck assert.ErrorAssertionFunc
}{
{
name: "Not enough path elements",
pathElements: []string{odConsts.DrivesPathDir, "driveID"},
errCheck: assert.Error,
},
{
name: "Root path",
pathElements: []string{odConsts.DrivesPathDir, "driveID", odConsts.RootPathDir},
expected: &path.DrivePath{
DriveID: "driveID",
Root: odConsts.RootPathDir,
Folders: []string{},
},
errCheck: assert.NoError,
},
{
name: "Deeper path",
pathElements: []string{odConsts.DrivesPathDir, "driveID", odConsts.RootPathDir, "folder1", "folder2"},
expected: &path.DrivePath{
DriveID: "driveID",
Root: odConsts.RootPathDir,
Folders: []string{"folder1", "folder2"},
},
errCheck: assert.NoError,
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
t := suite.T()
srs, err := path.NewServiceResources(path.OneDriveService, "user")
require.NoError(t, err, clues.ToCore(err))
p, err := path.Build("tenant", srs, path.FilesCategory, false, tt.pathElements...)
require.NoError(suite.T(), err, clues.ToCore(err))
got, err := path.ToDrivePath(p)
tt.errCheck(t, err)
if err != nil {
return
}
assert.Equal(suite.T(), tt.expected, got)
})
}
}
func (suite *OneDrivePathSuite) TestFormatDriveFolders() {
const (
driveID = "some-drive-id"
drivePrefix = "drives/" + driveID
)
table := []struct {
name string
input []string
expected string
}{
{
name: "normal",
input: []string{
"root:",
"foo",
"bar",
},
expected: strings.Join(
append([]string{drivePrefix}, "root:", "foo", "bar"),
"/"),
},
{
name: "has character that would be escaped",
input: []string{
"root:",
"foo/",
"bar",
},
// Element "foo/" should end up escaped in the string output.
expected: strings.Join(
append([]string{drivePrefix}, "root:", `foo\/`, "bar"),
"/"),
},
}
for _, test := range table {
suite.Run(test.name, func() {
assert.Equal(
suite.T(),
test.expected,
path.BuildDriveLocation(driveID, test.input...).String())
})
}
}