diff --git a/src/internal/connector/onedrive/collection.go b/src/internal/connector/onedrive/collection.go index 8e4e2e3f0..1f985162b 100644 --- a/src/internal/connector/onedrive/collection.go +++ b/src/internal/connector/onedrive/collection.go @@ -49,7 +49,7 @@ type Collection struct { // represents folderPath path.Path // M365 IDs of file items within this collection - driveItems []models.DriveItemable + driveItems map[string]models.DriveItemable // M365 ID of the drive this collection was created from driveID string source driveSource @@ -79,6 +79,7 @@ func NewCollection( ) *Collection { c := &Collection{ folderPath: folderPath, + driveItems: map[string]models.DriveItemable{}, driveID: driveID, source: source, service: service, @@ -101,7 +102,7 @@ func NewCollection( // Adds an itemID to the collection // This will make it eligible to be populated func (oc *Collection) Add(item models.DriveItemable) { - oc.driveItems = append(oc.driveItems, item) + oc.driveItems[*item.GetId()] = item } // Items() returns the channel containing M365 Exchange objects diff --git a/src/internal/connector/onedrive/collection_test.go b/src/internal/connector/onedrive/collection_test.go index a5c815f14..e55675618 100644 --- a/src/internal/connector/onedrive/collection_test.go +++ b/src/internal/connector/onedrive/collection_test.go @@ -61,14 +61,16 @@ func (suite *CollectionUnitTestSuite) TestCollection() { ) table := []struct { - name string - source driveSource - itemReader itemReaderFunc - infoFrom func(*testing.T, details.ItemInfo) (string, string) + name string + numInstances int + source driveSource + itemReader itemReaderFunc + infoFrom func(*testing.T, details.ItemInfo) (string, string) }{ { - name: "oneDrive", - source: OneDriveSource, + name: "oneDrive, no duplicates", + numInstances: 1, + source: OneDriveSource, itemReader: func(context.Context, models.DriveItemable) (details.ItemInfo, io.ReadCloser, error) { return details.ItemInfo{OneDrive: &details.OneDriveInfo{ItemName: testItemName}}, io.NopCloser(bytes.NewReader(testItemData)), @@ -80,8 +82,37 @@ func (suite *CollectionUnitTestSuite) TestCollection() { }, }, { - name: "sharePoint", - source: SharePointSource, + name: "oneDrive, duplicates", + numInstances: 3, + source: OneDriveSource, + itemReader: func(context.Context, models.DriveItemable) (details.ItemInfo, io.ReadCloser, error) { + return details.ItemInfo{OneDrive: &details.OneDriveInfo{ItemName: testItemName}}, + io.NopCloser(bytes.NewReader(testItemData)), + nil + }, + infoFrom: func(t *testing.T, dii details.ItemInfo) (string, string) { + require.NotNil(t, dii.OneDrive) + return dii.OneDrive.ItemName, dii.OneDrive.ParentPath + }, + }, + { + name: "sharePoint, no duplicates", + numInstances: 1, + source: SharePointSource, + itemReader: func(context.Context, models.DriveItemable) (details.ItemInfo, io.ReadCloser, error) { + return details.ItemInfo{SharePoint: &details.SharePointInfo{ItemName: testItemName}}, + io.NopCloser(bytes.NewReader(testItemData)), + nil + }, + infoFrom: func(t *testing.T, dii details.ItemInfo) (string, string) { + require.NotNil(t, dii.SharePoint) + return dii.SharePoint.ItemName, dii.SharePoint.ParentPath + }, + }, + { + name: "sharePoint, duplicates", + numInstances: 3, + source: SharePointSource, itemReader: func(context.Context, models.DriveItemable) (details.ItemInfo, io.ReadCloser, error) { return details.ItemInfo{SharePoint: &details.SharePointInfo{ItemName: testItemName}}, io.NopCloser(bytes.NewReader(testItemData)), @@ -119,7 +150,11 @@ func (suite *CollectionUnitTestSuite) TestCollection() { // Set a item reader, add an item and validate we get the item back mockItem := models.NewDriveItem() mockItem.SetId(&testItemID) - coll.Add(mockItem) + + for i := 0; i < test.numInstances; i++ { + coll.Add(mockItem) + } + coll.itemReader = test.itemReader // Read items from the collection