-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathgrpc_query_noc_root_certificates_test.go
126 lines (119 loc) · 3.57 KB
/
grpc_query_noc_root_certificates_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package keeper_test
import (
"strconv"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
keepertest "github.com/zigbee-alliance/distributed-compliance-ledger/testutil/keeper"
"github.com/zigbee-alliance/distributed-compliance-ledger/testutil/nullify"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)
// Prevent strconv unused error.
var _ = strconv.IntSize
func TestNocRootCertificatesQuerySingle(t *testing.T) {
keeper, ctx := keepertest.PkiKeeper(t, nil)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNNocRootCertificates(keeper, ctx, 2)
for _, tc := range []struct {
desc string
request *types.QueryGetNocRootCertificatesRequest
response *types.QueryGetNocRootCertificatesResponse
err error
}{
{
desc: "First",
request: &types.QueryGetNocRootCertificatesRequest{
Vid: msgs[0].Vid,
},
response: &types.QueryGetNocRootCertificatesResponse{NocRootCertificates: msgs[0]},
},
{
desc: "Second",
request: &types.QueryGetNocRootCertificatesRequest{
Vid: msgs[1].Vid,
},
response: &types.QueryGetNocRootCertificatesResponse{NocRootCertificates: msgs[1]},
},
{
desc: "KeyNotFound",
request: &types.QueryGetNocRootCertificatesRequest{
Vid: 100000,
},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "InvalidRequest",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.NocRootCertificates(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.Equal(t,
nullify.Fill(tc.response),
nullify.Fill(response),
)
}
})
}
}
func TestNocRootCertificatesQueryPaginated(t *testing.T) {
keeper, ctx := keepertest.PkiKeeper(t, nil)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNNocRootCertificates(keeper, ctx, 5)
request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllNocRootCertificatesRequest {
return &types.QueryAllNocRootCertificatesRequest{
Pagination: &query.PageRequest{
Key: next,
Offset: offset,
Limit: limit,
CountTotal: total,
},
}
}
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.NocRootCertificatesAll(wctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.NocRootCertificates), step)
require.Subset(t,
nullify.Fill(msgs),
nullify.Fill(resp.NocRootCertificates),
)
}
})
t.Run("ByKey", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.NocRootCertificatesAll(wctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.NocRootCertificates), step)
require.Subset(t,
nullify.Fill(msgs),
nullify.Fill(resp.NocRootCertificates),
)
next = resp.Pagination.NextKey
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.NocRootCertificatesAll(wctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
nullify.Fill(msgs),
nullify.Fill(resp.NocRootCertificates),
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.NocRootCertificatesAll(wctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}