Mock: SharePoint: List objects expansion (#2019)

## Description
Adds the ability to create non-empty lists within mocks
<!-- Insert PR description-->

## Does this PR need a docs update or release note?

- [x]  No 

## Type of change


- [x] 🤖 Test
- [x] 💻 CI/Deployment

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
*closes to  #2018<issue>

## Test Plan


- [x]  Unit test
This commit is contained in:
Danny 2023-01-04 13:42:55 -05:00 committed by GitHub
parent 978f9304bf
commit 76984c23c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import (
"io"
"testing"
kioser "github.com/microsoft/kiota-serialization-json-go"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -176,7 +177,24 @@ func (suite *MockExchangeDataSuite) TestMockByteHydration() {
},
},
{
name: "SharePoint: List",
name: "SharePoint: List Empty",
transformation: func(t *testing.T) error {
emptyMap := make(map[string]string)
temp := mockconnector.GetMockList(subject, "Artist", emptyMap)
writer := kioser.NewJsonSerializationWriter()
err := writer.WriteObjectValue("", temp)
require.NoError(t, err)
bytes, err := writer.GetSerializedContent()
require.NoError(suite.T(), err)
_, err = support.CreateListFromBytes(bytes)
return err
},
},
{
name: "SharePoint: List 6 Items",
transformation: func(t *testing.T) error {
bytes, err := mockconnector.GetMockListBytes(subject)
require.NoError(suite.T(), err)

View File

@ -74,31 +74,26 @@ func (mld *MockListData) ToReader() io.ReadCloser {
return mld.Reader
}
// GetMockList returns a Listable object with generic
// information.
// GetMockList returns a Listable object with two columns.
// @param: Name of the displayable list
// @param: Column Name: Defines the 2nd Column Name of the created list the values from the map.
// The key values of the input map are used for the `Title` column.
// The values of the map are placed within the 2nd column.
// Source: https://learn.microsoft.com/en-us/graph/api/list-create?view=graph-rest-1.0&tabs=go
func GetMockList(title string) models.Listable {
func GetMockList(title, columnName string, items map[string]string) models.Listable {
requestBody := models.NewList()
requestBody.SetDisplayName(&title)
requestBody.SetName(&title)
columnDef := models.NewColumnDefinition()
name := "Author"
name := columnName
text := models.NewTextColumn()
columnDef.SetName(&name)
columnDef.SetText(text)
columnDef2 := models.NewColumnDefinition()
name2 := "PageCount"
number := models.NewNumberColumn()
columnDef2.SetName(&name2)
columnDef2.SetNumber(number)
columns := []models.ColumnDefinitionable{
columnDef,
columnDef2,
}
requestBody.SetColumns(columns)
@ -107,12 +102,38 @@ func GetMockList(title string) models.Listable {
aList.SetTemplate(&template)
requestBody.SetList(aList)
// item Creation
itms := make([]models.ListItemable, 0)
for k, v := range items {
entry := map[string]interface{}{
"Title": k,
columnName: v,
}
fields := models.NewFieldValueSet()
fields.SetAdditionalData(entry)
temp := models.NewListItem()
temp.SetFields(fields)
itms = append(itms, temp)
}
requestBody.SetItems(itms)
return requestBody
}
// GetMockListDefault returns a two-list column list of
// Music lbums and the associated artist.
func GetMockListDefault(title string) models.Listable {
return GetMockList(title, "Artist", getItems())
}
// GetMockListBytes returns the byte representation of GetMockList
func GetMockListBytes(title string) ([]byte, error) {
list := GetMockList(title)
list := GetMockListDefault(title)
objectWriter := kw.NewJsonSerializationWriter()
defer objectWriter.Close()
@ -127,7 +148,7 @@ func GetMockListBytes(title string) ([]byte, error) {
// GetMockListStream returns the data.Stream representation
// of the Mocked SharePoint List
func GetMockListStream(t *testing.T, title string) *MockListData {
func GetMockListStream(t *testing.T, title string, numOfItems int) *MockListData {
byteArray, err := GetMockListBytes(title)
require.NoError(t, err)
@ -139,3 +160,33 @@ func GetMockListStream(t *testing.T, title string) *MockListData {
return listData
}
// getItems returns a map where key values are albums
// and values are the artist.
// Source: https://github.com/Currie32/500-Greatest-Albums/blob/master/albumlist.csv
func getItems() map[string]string {
items := map[string]string{
"London Calling": "The Clash",
"Blonde on Blonde": "Bob Dylan",
"The Beatles '(The White Album)'": "The Beatles",
"The Sun Sessions": "Elvis Presley",
"Kind of Blue": "Miles Davis",
"The Velvet Underground & Nico": "The Velvet Underground",
"Abbey Road": "The Beatles",
"Are You Experienced": "The Jimi Hendrix Experience",
"Blood on the Tracks": "Bob Dylan",
"Nevermind": "Nirvana",
"Born to Run": "Bruce Springsteen",
"Astral Weeks": "Van Morrison",
"Thriller": "Michael Jackson",
"The Great Twenty_Eight": "Chuck Berry",
"The Complete Recordings": "Robert Johnson",
"John Lennon/Plastic Ono Band": "John Lennon / Plastic Ono Band",
"Innervisions": "Stevie Wonder",
"Live at the Apollo, 1962": "James Brown",
"Rumours": "Fleetwood Mac",
"The Joshua Tree": "U2",
}
return items
}

View File

@ -6,6 +6,7 @@ import (
"testing"
kw "github.com/microsoft/kiota-serialization-json-go"
"github.com/microsoftgraph/msgraph-sdk-go/sites"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
@ -13,6 +14,7 @@ import (
"github.com/alcionai/corso/src/internal/common"
"github.com/alcionai/corso/src/internal/connector/mockconnector"
"github.com/alcionai/corso/src/internal/connector/onedrive"
"github.com/alcionai/corso/src/internal/connector/support"
"github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/internal/tester"
"github.com/alcionai/corso/src/pkg/path"
@ -47,7 +49,10 @@ func (suite *SharePointCollectionSuite) TestSharePointListCollection() {
t := suite.T()
ow := kw.NewJsonSerializationWriter()
listing := mockconnector.GetMockList("Mock List")
artistAndAlbum := map[string]string{
"Our Love to Admire": "Interpol",
}
listing := mockconnector.GetMockList("Mock List", "Artist", artistAndAlbum)
testName := "MockListing"
listing.SetDisplayName(&testName)
@ -101,7 +106,7 @@ func (suite *SharePointCollectionSuite) TestRestoreList() {
siteID := tester.M365SiteID(t)
ow := kw.NewJsonSerializationWriter()
listing := mockconnector.GetMockList("Mock List")
listing := mockconnector.GetMockListDefault("Mock List")
testName := "MockListing"
listing.SetDisplayName(&testName)
@ -124,6 +129,39 @@ func (suite *SharePointCollectionSuite) TestRestoreList() {
deets, err := restoreListItem(ctx, service, listData, siteID, destName)
assert.NoError(t, err)
t.Logf("List created: %s\n", deets.SharePoint.ItemName)
// Clean-Up
var (
builder = service.Client().SitesById(siteID).Lists()
isFound bool
deleteID string
)
for {
resp, err := builder.Get(ctx, nil)
assert.NoError(t, err, "experienced query error during clean up. Details: "+support.ConnectorStackErrorTrace(err))
for _, temp := range resp.GetValue() {
if *temp.GetDisplayName() == deets.SharePoint.ItemName {
isFound = true
deleteID = *temp.GetId()
break
}
}
// Get Next Link
link := resp.GetOdataNextLink()
if link == nil {
break
}
builder = sites.NewItemListsRequestBuilder(*link, service.Adapter())
}
if isFound {
err := DeleteList(ctx, service, siteID, deleteID)
assert.NoError(t, err)
}
}
// TestRestoreLocation temporary test for greater restore operation