Skip to content

Commit

Permalink
test: add tests for page utils (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongwoo Lee authored Apr 30, 2021
1 parent 2046378 commit c4bf377
Showing 1 changed file with 61 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit c4bf377

Please sign in to comment.