Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for page utils #161

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions client/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package client_test

import (
"strconv"
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

"github.com/line/lbm-sdk/v2/client"
"github.com/line/lbm-sdk/v2/client/flags"
)

func TestPaginate(t *testing.T) {
Expand Down Expand Up @@ -75,3 +78,61 @@ func TestPaginate(t *testing.T) {
})
}
}

func TestReadPageRequest(t *testing.T) {

testCases := []struct {
name string
pageKey string
offset, limit, page int
countTotal bool
ok bool
}{
{
"use page ok",
"page key",
0, 100, 10,
true,
true,
},
{
"use offset ok",
"page key",
10, 100, 0,
true,
true,
},
{
"page and offset cannot be used together",
"page key",
100, 100, 10,
true,
false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
flagSet := pflag.NewFlagSet("test flag set", pflag.ContinueOnError)
flagSet.String(flags.FlagPageKey, "default page key", "page key")
flagSet.Uint64(flags.FlagOffset, 0, "offset")
flagSet.Uint64(flags.FlagLimit, 0, "limit")
flagSet.Uint64(flags.FlagPage, 0, "page")
flagSet.Bool(flags.FlagCountTotal, false, "count total")

err := flagSet.Set(flags.FlagPageKey, tc.pageKey)
err = flagSet.Set(flags.FlagOffset, strconv.Itoa(tc.offset))
err = flagSet.Set(flags.FlagLimit, strconv.Itoa(tc.limit))
err = flagSet.Set(flags.FlagPage, strconv.Itoa(tc.page))
err = flagSet.Set(flags.FlagCountTotal, strconv.FormatBool(tc.countTotal))

pr, err := client.ReadPageRequest(flagSet)
if tc.ok {
require.NoError(t, err)
require.NotNil(t, pr)
} else {
require.Error(t, err)
}
})
}
}