Add unit tests

This commit is contained in:
Abhishek Pandey 2023-12-01 21:34:14 -08:00
parent c2f6a87e69
commit 8025d4aa9a
2 changed files with 396 additions and 24 deletions

View File

@ -15,13 +15,6 @@ import (
"github.com/alcionai/corso/src/internal/common/str"
)
// TODO(pandeyabs): Remove this duplicate
// downloadUrlKeys is used to find the download URL in a DriveItem response.
var downloadURLKeys = []string{
"@microsoft.graph.downloadUrl",
"@content.downloadUrl",
}
// Replica of models.DriveItemable
type LiteDriveItemable interface {
GetId() *string
@ -63,7 +56,6 @@ type driveItem struct {
additionalData map[string]interface{}
}
// nolint
func (c *driveItem) GetId() *string {
return &c.id
}
@ -204,6 +196,13 @@ func (iu *itemUser) GetAdditionalData() map[string]interface{} {
return iu.additionalData
}
// TODO(pandeyabs): This is duplicated from collection/drive package.
// Move to common pkg
var downloadURLKeys = []string{
"@microsoft.graph.downloadUrl",
"@content.downloadUrl",
}
func ToLiteDriveItemable(item models.DriveItemable) LiteDriveItemable {
cdi := &driveItem{
id: strings.Clone(ptr.Val(item.GetId())),
@ -254,12 +253,11 @@ func ToLiteDriveItemable(item models.DriveItemable) LiteDriveItemable {
var s string
ed, ok := additionalData["email"]
if ok {
s = strings.Clone(ptr.Val(ed.(*string)))
if v, err := str.AnyValueToString("email", additionalData); err == nil {
s = strings.Clone(v)
ad["email"] = &s
} else if ed, ok = additionalData["displayName"]; ok {
s = strings.Clone(ptr.Val(ed.(*string)))
} else if v, err := str.AnyValueToString("userPrincipalName", additionalData); err == nil {
s = strings.Clone(v)
ad["displayName"] = &s
}
@ -270,20 +268,22 @@ func ToLiteDriveItemable(item models.DriveItemable) LiteDriveItemable {
}
}
// Hacky way to cache the download url. Thats all we use from additional data
// Otherwise, we'll hold a reference to the underlying store which will consume
// lot more memory.
if item.GetFile() != nil {
ad := make(map[string]interface{})
// We only use the download URL from additional data
var downloadURL, mapKey string
for _, key := range downloadURLKeys {
if v, err := str.AnyValueToString(key, item.GetAdditionalData()); err == nil {
ad[key] = strings.Clone(v)
break
}
for _, key := range downloadURLKeys {
if v, err := str.AnyValueToString(key, item.GetAdditionalData()); err == nil {
downloadURL = strings.Clone(v)
mapKey = key
break
}
}
cdi.additionalData = ad
if len(downloadURL) != 0 {
cdi.additionalData = map[string]interface{}{
mapKey: &downloadURL,
}
}
return cdi

View File

@ -0,0 +1,372 @@
// funcs as the original structs in the msgraph-sdk-go package, which do not
// follow some of the golint rules.
//
//nolint:revive
package custom
import (
"testing"
"time"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"gotest.tools/v3/assert"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/common/str"
"github.com/alcionai/corso/src/internal/tester"
)
type driveUnitSuite struct {
tester.Suite
}
func TestDriveUnitSuite(t *testing.T) {
suite.Run(t, &driveUnitSuite{
Suite: tester.NewUnitSuite(t),
})
}
func (suite *driveUnitSuite) TestToLiteDriveItemable() {
id := "itemID"
table := []struct {
name string
itemFunc func() models.DriveItemable
validateFunc func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable)
}{
{
name: "uninitialized values",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
assert.Equal(t, ptr.Val(got.GetId()), "")
assert.Equal(t, ptr.Val(got.GetName()), "")
assert.Equal(t, ptr.Val(got.GetSize()), int64(0))
assert.Equal(t, ptr.Val(got.GetCreatedDateTime()), time.Time{})
assert.Equal(t, ptr.Val(got.GetLastModifiedDateTime()), time.Time{})
require.Nil(t, got.GetFolder())
require.Nil(t, got.GetFile())
require.Nil(t, got.GetPackageEscaped())
require.Nil(t, got.GetShared())
require.Nil(t, got.GetMalware())
require.Nil(t, got.GetDeleted())
require.Nil(t, got.GetRoot())
require.Nil(t, got.GetCreatedBy())
require.Nil(t, got.GetParentReference())
assert.Equal(t, len(got.GetAdditionalData()), 0)
},
},
{
name: "ID, name, size, created, modified",
itemFunc: func() models.DriveItemable {
name := "itemName"
size := int64(123)
created := time.Now().Add(-time.Second).Truncate(time.Nanosecond)
modified := time.Now().Truncate(time.Nanosecond)
di := models.NewDriveItem()
di.SetId(&id)
di.SetName(&name)
di.SetSize(&size)
di.SetCreatedDateTime(&created)
di.SetLastModifiedDateTime(&modified)
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
assert.Equal(t, ptr.Val(got.GetName()), ptr.Val(expected.GetName()))
assert.Equal(t, ptr.Val(got.GetSize()), ptr.Val(expected.GetSize()))
require.True(
t,
got.GetCreatedDateTime().Equal(ptr.Val(expected.GetCreatedDateTime())))
require.True(
t,
got.GetLastModifiedDateTime().Equal(ptr.Val(expected.GetLastModifiedDateTime())))
},
},
{
name: "Folder item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetFolder(models.NewFolder())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetFolder())
require.Nil(t, got.GetFile())
require.Nil(t, got.GetPackageEscaped())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "Package item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetPackageEscaped(models.NewPackageEscaped())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetPackageEscaped())
require.Nil(t, got.GetFile())
require.Nil(t, got.GetFolder())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "File item",
itemFunc: func() models.DriveItemable {
mime := "mimeType"
di := models.NewDriveItem()
di.SetId(&id)
di.SetFile(models.NewFile())
di.GetFile().SetMimeType(&mime)
// additional data
di.SetAdditionalData(map[string]interface{}{
"@microsoft.graph.downloadUrl": "downloadURL",
})
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetFile())
require.Nil(t, got.GetFolder())
require.Nil(t, got.GetPackageEscaped())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
assert.Equal(
t,
ptr.Val(got.GetFile().GetMimeType()),
ptr.Val(expected.GetFile().GetMimeType()))
// additional data
urlExpected, err := str.AnyValueToString(
"@microsoft.graph.downloadUrl",
expected.GetAdditionalData())
require.NoError(t, err)
urlGot, err := str.AnyValueToString(
"@microsoft.graph.downloadUrl",
got.GetAdditionalData())
require.NoError(t, err)
assert.Equal(
t,
urlExpected,
urlGot)
},
},
{
name: "Shared item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetShared(models.NewShared())
di.SetFile(models.NewFile())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetShared())
require.NotNil(t, got.GetFile())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "Malware item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetMalware(models.NewMalware())
di.SetFile(models.NewFile())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetMalware())
require.NotNil(t, got.GetFile())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "Deleted item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetDeleted(models.NewDeleted())
di.SetFile(models.NewFile())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetDeleted())
require.NotNil(t, got.GetFile())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "Root item",
itemFunc: func() models.DriveItemable {
di := models.NewDriveItem()
di.SetId(&id)
di.SetRoot(models.NewRoot())
di.SetFolder(models.NewFolder())
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetRoot())
require.NotNil(t, got.GetFolder())
assert.Equal(t, ptr.Val(got.GetId()), ptr.Val(expected.GetId()))
},
},
{
name: "Parent reference",
itemFunc: func() models.DriveItemable {
parentID := "parentID"
parentPath := "/parentPath"
parentName := "parentName"
parentDriveID := "parentDriveID"
parentRef := models.NewItemReference()
parentRef.SetId(&parentID)
parentRef.SetPath(&parentPath)
parentRef.SetName(&parentName)
parentRef.SetDriveId(&parentDriveID)
di := models.NewDriveItem()
di.SetId(&id)
di.SetParentReference(parentRef)
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetParentReference())
assert.Equal(
t,
ptr.Val(got.GetParentReference().GetId()),
ptr.Val(expected.GetParentReference().GetId()))
assert.Equal(
t,
ptr.Val(got.GetParentReference().GetPath()),
ptr.Val(expected.GetParentReference().GetPath()))
assert.Equal(
t,
ptr.Val(got.GetParentReference().GetName()),
ptr.Val(expected.GetParentReference().GetName()))
assert.Equal(
t,
ptr.Val(got.GetParentReference().GetDriveId()),
ptr.Val(expected.GetParentReference().GetDriveId()))
},
},
{
name: "Created by",
itemFunc: func() models.DriveItemable {
createdBy := models.NewIdentitySet()
createdBy.SetUser(models.NewUser())
createdBy.GetUser().SetAdditionalData(map[string]interface{}{
"email": "email@me",
})
di := models.NewDriveItem()
di.SetId(&id)
di.SetCreatedBy(createdBy)
return di
},
validateFunc: func(
t *testing.T,
expected models.DriveItemable,
got LiteDriveItemable,
) {
require.NotNil(t, got.GetCreatedBy())
require.NotNil(t, got.GetCreatedBy().GetUser())
emailExpected, err := str.AnyValueToString(
"email",
expected.GetCreatedBy().GetUser().GetAdditionalData())
require.NoError(t, err)
emailGot, err := str.AnyValueToString(
"email",
got.GetCreatedBy().GetUser().GetAdditionalData())
require.NoError(t, err)
assert.Equal(t, emailExpected, emailGot)
},
},
}
for _, test := range table {
suite.Run(test.name, func() {
expected := test.itemFunc()
got := ToLiteDriveItemable(expected)
test.validateFunc(suite.T(), expected, got)
})
}
}