-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add orderby name and lastModifiedDateTime (#3201)
* add orderby name and lastModifiedDateTime * add unit test for sort * fix orderby name * add more tests * exclude test files from duplication analyzer * remove flaky test * order by name if no date is set * refactor * add test for wrong parameter name
- Loading branch information
Showing
6 changed files
with
321 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Enhancement: Add sorting to list Spaces | ||
|
||
We added the OData query param "orderBy" for listing spaces. We can now order by Space Name and LastModifiedDateTime. | ||
|
||
Example 1: https://localhost:9200/graph/v1.0/me/drives/?$orderby=lastModifiedDateTime desc | ||
Example 2: https://localhost:9200/graph/v1.0/me/drives/?$orderby=name asc | ||
|
||
https://github.com/owncloud/ocis/issues/3200 | ||
https://github.com/owncloud/ocis/pull/3201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package svc | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/CiscoM31/godata" | ||
libregraph "github.com/owncloud/libre-graph-api-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type sortTest struct { | ||
Drives []*libregraph.Drive | ||
Query godata.GoDataRequest | ||
DrivesSorted []*libregraph.Drive | ||
} | ||
|
||
var time1 = time.Date(2022, 02, 02, 15, 00, 00, 00, time.UTC) | ||
var time2 = time.Date(2022, 02, 03, 15, 00, 00, 00, time.UTC) | ||
var time3, time5, time6 *time.Time | ||
var time4 = time.Date(2022, 02, 05, 15, 00, 00, 00, time.UTC) | ||
var drives = []*libregraph.Drive{ | ||
drive("3", "project", "Admin", time3), | ||
drive("1", "project", "Einstein", &time1), | ||
drive("2", "project", "Marie", &time2), | ||
drive("4", "project", "Richard", &time4), | ||
} | ||
var drivesLong = append(drives, []*libregraph.Drive{ | ||
drive("5", "project", "Bob", time5), | ||
drive("6", "project", "Alice", time6), | ||
}...) | ||
|
||
var sortTests = []sortTest{ | ||
{ | ||
Drives: drives, | ||
Query: godata.GoDataRequest{ | ||
Query: &godata.GoDataQuery{ | ||
OrderBy: &godata.GoDataOrderByQuery{ | ||
OrderByItems: []*godata.OrderByItem{ | ||
{Field: &godata.Token{Value: "name"}, Order: "asc"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DrivesSorted: []*libregraph.Drive{ | ||
drive("3", "project", "Admin", time3), | ||
drive("1", "project", "Einstein", &time1), | ||
drive("2", "project", "Marie", &time2), | ||
drive("4", "project", "Richard", &time4), | ||
}, | ||
}, | ||
{ | ||
Drives: drives, | ||
Query: godata.GoDataRequest{ | ||
Query: &godata.GoDataQuery{ | ||
OrderBy: &godata.GoDataOrderByQuery{ | ||
OrderByItems: []*godata.OrderByItem{ | ||
{Field: &godata.Token{Value: "name"}, Order: "desc"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DrivesSorted: []*libregraph.Drive{ | ||
drive("4", "project", "Richard", &time4), | ||
drive("2", "project", "Marie", &time2), | ||
drive("1", "project", "Einstein", &time1), | ||
drive("3", "project", "Admin", time3), | ||
}, | ||
}, | ||
{ | ||
Drives: drivesLong, | ||
Query: godata.GoDataRequest{ | ||
Query: &godata.GoDataQuery{ | ||
OrderBy: &godata.GoDataOrderByQuery{ | ||
OrderByItems: []*godata.OrderByItem{ | ||
{Field: &godata.Token{Value: "lastModifiedDateTime"}, Order: "asc"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DrivesSorted: []*libregraph.Drive{ | ||
drive("3", "project", "Admin", time3), | ||
drive("6", "project", "Alice", time6), | ||
drive("5", "project", "Bob", time5), | ||
drive("1", "project", "Einstein", &time1), | ||
drive("2", "project", "Marie", &time2), | ||
drive("4", "project", "Richard", &time4), | ||
}, | ||
}, | ||
{ | ||
Drives: drivesLong, | ||
Query: godata.GoDataRequest{ | ||
Query: &godata.GoDataQuery{ | ||
OrderBy: &godata.GoDataOrderByQuery{ | ||
OrderByItems: []*godata.OrderByItem{ | ||
{Field: &godata.Token{Value: "lastModifiedDateTime"}, Order: "desc"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DrivesSorted: []*libregraph.Drive{ | ||
drive("4", "project", "Richard", &time4), | ||
drive("2", "project", "Marie", &time2), | ||
drive("1", "project", "Einstein", &time1), | ||
drive("5", "project", "Bob", time5), | ||
drive("6", "project", "Alice", time6), | ||
drive("3", "project", "Admin", time3), | ||
}, | ||
}, | ||
} | ||
|
||
func drive(ID string, dType string, name string, lastModified *time.Time) *libregraph.Drive { | ||
return &libregraph.Drive{Id: libregraph.PtrString(ID), DriveType: libregraph.PtrString(dType), Name: libregraph.PtrString(name), LastModifiedDateTime: lastModified} | ||
} | ||
|
||
// TestSort tests the available orderby queries | ||
func TestSort(t *testing.T) { | ||
for _, test := range sortTests { | ||
sorted, err := sortSpaces(&test.Query, test.Drives) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.DrivesSorted, sorted) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package svc | ||
|
||
import ( | ||
libregraph "github.com/owncloud/libre-graph-api-go" | ||
) | ||
|
||
type spacesSlice []*libregraph.Drive | ||
|
||
// Len is the number of elements in the collection. | ||
func (d spacesSlice) Len() int { return len(d) } | ||
|
||
// Swap swaps the elements with indexes i and j. | ||
func (d spacesSlice) Swap(i, j int) { d[i], d[j] = d[j], d[i] } | ||
|
||
type spacesByName struct { | ||
spacesSlice | ||
} | ||
type spacesByLastModifiedDateTime struct { | ||
spacesSlice | ||
} | ||
|
||
// Less reports whether the element with index i | ||
// must sort before the element with index j. | ||
func (s spacesByName) Less(i, j int) bool { | ||
return *s.spacesSlice[i].Name > *s.spacesSlice[j].Name | ||
} | ||
|
||
// Less reports whether the element with index i | ||
// must sort before the element with index j. | ||
func (s spacesByLastModifiedDateTime) Less(i, j int) bool { | ||
// compare the items when both dates are set | ||
if s.spacesSlice[i].LastModifiedDateTime != nil && s.spacesSlice[j].LastModifiedDateTime != nil { | ||
return s.spacesSlice[i].LastModifiedDateTime.After(*s.spacesSlice[j].LastModifiedDateTime) | ||
} | ||
// move left item down if it has no value | ||
if s.spacesSlice[i].LastModifiedDateTime == nil && s.spacesSlice[j].LastModifiedDateTime != nil { | ||
return false | ||
} | ||
// move right item down if it has no value | ||
if s.spacesSlice[i].LastModifiedDateTime != nil && s.spacesSlice[j].LastModifiedDateTime == nil { | ||
return true | ||
} | ||
// fallback to name if no dateTime is set on both items | ||
return *s.spacesSlice[i].Name > *s.spacesSlice[j].Name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters