feat(media): pagination
This commit is contained in:
1 parent
04dc151ed7
commit
63c1e7076e
65 files changed
+2960
-365
No files matched your search
@@ -7,7 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -35,9 +34,11 @@ func seedListingLibrary(env *mediaEnv) {
|
||||
}
|
||||
|
||||
type listBody struct {
|
||||
Items []map[string]any `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
Counts map[string]int `json:"counts"`
|
||||
Items []map[string]any `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
Counts map[string]int `json:"counts"`
|
||||
}
|
||||
|
||||
func (e *mediaEnv) list(t *testing.T, query string) (listBody, string) {
|
||||
@@ -155,11 +156,11 @@ func TestMediaListing(t *testing.T) {
|
||||
if !strings.Contains(raw, `"width":null`) || !strings.Contains(raw, `"height":null`) {
|
||||
t.Errorf("expected null width and height in the raw body, got %s", raw)
|
||||
}
|
||||
if got.NextCursor != nil {
|
||||
t.Errorf("expected next_cursor null, got %v", *got.NextCursor)
|
||||
if strings.Contains(raw, "next_cursor") {
|
||||
t.Errorf("expected no next_cursor in the raw body")
|
||||
}
|
||||
if !strings.Contains(raw, `"next_cursor":null`) {
|
||||
t.Errorf("expected next_cursor null in the raw body")
|
||||
if got.Total != 5 || got.Page != 1 || got.Size != 25 {
|
||||
t.Errorf("unexpected paging %d %d %d", got.Total, got.Page, got.Size)
|
||||
}
|
||||
if !reflect.DeepEqual(got.Counts, map[string]int{"all": 5, "unused": 3, "duplicates": 1}) {
|
||||
t.Errorf("unexpected counts %v", got.Counts)
|
||||
@@ -293,31 +294,26 @@ func TestMediaListing(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("limit and cursor page through the list", func(t *testing.T) {
|
||||
t.Run("page and size page through the list", func(t *testing.T) {
|
||||
env := newMediaEnv(t)
|
||||
seedListingLibrary(env)
|
||||
|
||||
first, _ := env.list(t, "?limit=2")
|
||||
expectIds(t, idsOf(first.Items), []uint32{5, 4})
|
||||
if first.NextCursor == nil || *first.NextCursor == "" {
|
||||
t.Fatalf("expected a next_cursor on page 1")
|
||||
for id := uint32(1); id <= 12; id++ {
|
||||
env.seedImage(id, "image.jpg", 10, "image/jpeg", 3, 2, hashA, 1)
|
||||
}
|
||||
|
||||
second, _ := env.list(t, "?limit=2&cursor="+url.QueryEscape(*first.NextCursor))
|
||||
expectIds(t, idsOf(second.Items), []uint32{3, 2})
|
||||
if second.NextCursor == nil || *second.NextCursor == "" {
|
||||
t.Fatalf("expected a next_cursor on page 2")
|
||||
}
|
||||
first, _ := env.list(t, "?size=5&page=1")
|
||||
second, _ := env.list(t, "?size=5&page=2")
|
||||
third, _ := env.list(t, "?size=5&page=3")
|
||||
|
||||
third, _ := env.list(t, "?limit=2&cursor="+url.QueryEscape(*second.NextCursor))
|
||||
expectIds(t, idsOf(third.Items), []uint32{1})
|
||||
if third.NextCursor != nil {
|
||||
t.Errorf("expected next_cursor null on the last page, got %v", *third.NextCursor)
|
||||
expectIds(t, idsOf(first.Items), []uint32{12, 11, 10, 9, 8})
|
||||
expectIds(t, idsOf(second.Items), []uint32{7, 6, 5, 4, 3})
|
||||
expectIds(t, idsOf(third.Items), []uint32{2, 1})
|
||||
if third.Total != 12 || third.Page != 3 || third.Size != 5 {
|
||||
t.Errorf("unexpected paging %d %d %d", third.Total, third.Page, third.Size)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("A malformed query answers 400", func(t *testing.T) {
|
||||
for _, query := range []string{"filter=bogus", "sort=bogus", "limit=0", "limit=101", "limit=abc", "cursor=%%%", "cursor=bogus", "hash=notahash"} {
|
||||
for _, query := range []string{"filter=bogus", "sort=bogus", "page=0", "page=abc", "size=10", "size=200", "size=", "hash=notahash"} {
|
||||
t.Run(query, func(t *testing.T) {
|
||||
env := newMediaEnv(t)
|
||||
seedListingLibrary(env)
|
||||
|
||||
Reference in new issue
Block a user