add tests for paging
parent
08c3d0916e
commit
4216eba683
|
@ -0,0 +1,213 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/influxdata/platform"
|
||||||
|
"github.com/influxdata/platform/mock"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPaging_decodeFindOptions(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
queryParams map[string]string
|
||||||
|
}
|
||||||
|
type wants struct {
|
||||||
|
opts platform.FindOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wants wants
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "decode FindOptions",
|
||||||
|
args: args{
|
||||||
|
map[string]string{
|
||||||
|
"offset": "10",
|
||||||
|
"limit": "10",
|
||||||
|
"sortBy": "updateTime",
|
||||||
|
"descending": "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wants: wants{
|
||||||
|
opts: platform.FindOptions{
|
||||||
|
Offset: 10,
|
||||||
|
Limit: 10,
|
||||||
|
SortBy: "updateTime",
|
||||||
|
Descending: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "decode FindOptions with default values",
|
||||||
|
args: args{
|
||||||
|
map[string]string{
|
||||||
|
"limit": "10",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wants: wants{
|
||||||
|
opts: platform.FindOptions{
|
||||||
|
Offset: 0,
|
||||||
|
Limit: 10,
|
||||||
|
SortBy: "",
|
||||||
|
Descending: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
r := httptest.NewRequest("GET", "http://any.url", nil)
|
||||||
|
qp := r.URL.Query()
|
||||||
|
for k, v := range tt.args.queryParams {
|
||||||
|
qp.Set(k, v)
|
||||||
|
}
|
||||||
|
r.URL.RawQuery = qp.Encode()
|
||||||
|
|
||||||
|
opts, err := decodeFindOptions(context.Background(), r)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%q failed, err: %s", tt.name, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Offset != tt.wants.opts.Offset {
|
||||||
|
t.Errorf("%q. decodeFindOptions() = %v, want %v", tt.name, opts.Offset, tt.wants.opts.Offset)
|
||||||
|
}
|
||||||
|
if opts.Limit != tt.wants.opts.Limit {
|
||||||
|
t.Errorf("%q. decodeFindOptions() = %v, want %v", tt.name, opts.Limit, tt.wants.opts.Limit)
|
||||||
|
}
|
||||||
|
if opts.SortBy != tt.wants.opts.SortBy {
|
||||||
|
t.Errorf("%q. decodeFindOptions() = %v, want %v", tt.name, opts.SortBy, tt.wants.opts.SortBy)
|
||||||
|
}
|
||||||
|
if opts.Descending != tt.wants.opts.Descending {
|
||||||
|
t.Errorf("%q. decodeFindOptions() = %v, want %v", tt.name, opts.Descending, tt.wants.opts.Descending)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPaging_newPagingLinks(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
basePath string
|
||||||
|
num int
|
||||||
|
opts platform.FindOptions
|
||||||
|
filter mock.PagingFilter
|
||||||
|
}
|
||||||
|
type wants struct {
|
||||||
|
links platform.PagingLinks
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wants wants
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "new PagingLinks",
|
||||||
|
args: args{
|
||||||
|
basePath: "/api/v2/buckets",
|
||||||
|
num: 50,
|
||||||
|
opts: platform.FindOptions{
|
||||||
|
Offset: 10,
|
||||||
|
Limit: 10,
|
||||||
|
Descending: true,
|
||||||
|
},
|
||||||
|
filter: mock.PagingFilter{
|
||||||
|
Name: "name",
|
||||||
|
Type: []string{"type1", "type2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wants: wants{
|
||||||
|
links: platform.PagingLinks{
|
||||||
|
Prev: map[string]string{
|
||||||
|
"prev": "/api/v2/buckets?descending=true&limit=10&name=name&offset=0&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
Self: map[string]string{
|
||||||
|
"self": "/api/v2/buckets?descending=true&limit=10&name=name&offset=10&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
Next: map[string]string{
|
||||||
|
"next": "/api/v2/buckets?descending=true&limit=10&name=name&offset=20&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "new PagingLinks with empty prev link",
|
||||||
|
args: args{
|
||||||
|
basePath: "/api/v2/buckets",
|
||||||
|
num: 50,
|
||||||
|
opts: platform.FindOptions{
|
||||||
|
Offset: 0,
|
||||||
|
Limit: 10,
|
||||||
|
Descending: true,
|
||||||
|
},
|
||||||
|
filter: mock.PagingFilter{
|
||||||
|
Name: "name",
|
||||||
|
Type: []string{"type1", "type2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wants: wants{
|
||||||
|
links: platform.PagingLinks{
|
||||||
|
Prev: map[string]string{
|
||||||
|
"prev": "",
|
||||||
|
},
|
||||||
|
Self: map[string]string{
|
||||||
|
"self": "/api/v2/buckets?descending=true&limit=10&name=name&offset=0&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
Next: map[string]string{
|
||||||
|
"next": "/api/v2/buckets?descending=true&limit=10&name=name&offset=10&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "new PagingLinks with empty next link",
|
||||||
|
args: args{
|
||||||
|
basePath: "/api/v2/buckets",
|
||||||
|
num: 5,
|
||||||
|
opts: platform.FindOptions{
|
||||||
|
Offset: 10,
|
||||||
|
Limit: 10,
|
||||||
|
Descending: true,
|
||||||
|
},
|
||||||
|
filter: mock.PagingFilter{
|
||||||
|
Name: "name",
|
||||||
|
Type: []string{"type1", "type2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wants: wants{
|
||||||
|
links: platform.PagingLinks{
|
||||||
|
Prev: map[string]string{
|
||||||
|
"prev": "/api/v2/buckets?descending=true&limit=10&name=name&offset=0&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
Self: map[string]string{
|
||||||
|
"self": "/api/v2/buckets?descending=true&limit=10&name=name&offset=10&type=type1&type=type2",
|
||||||
|
},
|
||||||
|
Next: map[string]string{
|
||||||
|
"next": "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
links := newPagingLinks(tt.args.basePath, tt.args.opts, tt.args.filter, tt.args.num)
|
||||||
|
|
||||||
|
if links.Prev["prev"] != tt.wants.links.Prev["prev"] {
|
||||||
|
t.Errorf("%q. newPagingLinks() = %v, want %v", tt.name, links.Prev["prev"], tt.wants.links.Prev["prev"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if links.Self["self"] != tt.wants.links.Self["self"] {
|
||||||
|
t.Errorf("%q. newPagingLinks() = %v, want %v", tt.name, links.Self["self"], tt.wants.links.Self["self"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if links.Next["next"] != tt.wants.links.Next["next"] {
|
||||||
|
t.Errorf("%q. newPagingLinks() = %v, want %v", tt.name, links.Next["next"], tt.wants.links.Next["next"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package mock
|
||||||
|
|
||||||
|
type PagingFilter struct {
|
||||||
|
Name string
|
||||||
|
Type []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f PagingFilter) QueryParams() map[string][]string {
|
||||||
|
qp := map[string][]string{}
|
||||||
|
qp["name"] = []string{f.Name}
|
||||||
|
qp["type"] = f.Type
|
||||||
|
return qp
|
||||||
|
}
|
Loading…
Reference in New Issue