File: handwritten_paginators_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 662,428 kB
  • sloc: java: 16,875; makefile: 432; sh: 175
file content (147 lines) | stat: -rw-r--r-- 4,859 bytes parent folder | download | duplicates (4)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package route53

import (
	"context"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/route53/types"
	"testing"
)

type mockListResourceRecordSetsClient struct {
	outputs []*ListResourceRecordSetsOutput
	inputs  []*ListResourceRecordSetsInput
	t       *testing.T
}

func (c *mockListResourceRecordSetsClient) ListResourceRecordSets(ctx context.Context, input *ListResourceRecordSetsInput, optFns ...func(*Options)) (*ListResourceRecordSetsOutput, error) {
	c.inputs = append(c.inputs, input)
	requestCnt := len(c.inputs)
	if *input.MaxItems != *c.outputs[requestCnt-1].MaxItems {
		c.t.Errorf("Expect page limit to be %d, got %d", *c.outputs[requestCnt-1].MaxItems, *input.MaxItems)
	}
	if outputLen := len(c.outputs); requestCnt > outputLen {
		c.t.Errorf("Paginator calls client more than expected %d times", outputLen)
	}
	return c.outputs[requestCnt-1], nil
}

type listRRSTestCase struct {
	limit                  int32
	requestCnt             int
	stopOnDuplicationToken bool
	outputs                []*ListResourceRecordSetsOutput
}

func TestListResourceRecordSetsPaginator(t *testing.T) {
	cases := map[string]listRRSTestCase{
		"page limit 5": {
			limit:      5,
			requestCnt: 3,
			outputs: []*ListResourceRecordSetsOutput{
				{
					MaxItems:             aws.Int32(5),
					NextRecordName:       aws.String("testRecord1"),
					NextRecordIdentifier: aws.String("testID1"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(5),
					NextRecordName:       aws.String("testRecord2"),
					NextRecordIdentifier: aws.String("testID2"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(5),
					NextRecordName:       aws.String("testRecord3"),
					NextRecordIdentifier: aws.String("testID3"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          false,
				},
			},
		},
		"page limit 10 with duplicate record": {
			limit:                  10,
			requestCnt:             4,
			stopOnDuplicationToken: true,
			outputs: []*ListResourceRecordSetsOutput{
				{
					MaxItems:             aws.Int32(10),
					NextRecordName:       aws.String("testRecord1"),
					NextRecordIdentifier: aws.String("testID1"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(10),
					NextRecordName:       aws.String("testRecord2"),
					NextRecordIdentifier: aws.String("testID2"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(10),
					NextRecordName:       aws.String("testRecord3"),
					NextRecordIdentifier: aws.String("testID3"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(10),
					NextRecordName:       aws.String("testRecord3"),
					NextRecordIdentifier: aws.String("testID3"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          true,
				},
				{
					MaxItems:             aws.Int32(10),
					NextRecordName:       aws.String("testRecord5"),
					NextRecordIdentifier: aws.String("testID5"),
					NextRecordType:       types.RRTypeA,
					IsTruncated:          false,
				},
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			client := mockListResourceRecordSetsClient{
				inputs:  []*ListResourceRecordSetsInput{},
				outputs: c.outputs,
				t:       t,
			}

			paginator := NewListResourceRecordSetsPaginator(&client, &ListResourceRecordSetsInput{}, func(options *ListResourceRecordSetsPaginatorOptions) {
				options.Limit = c.limit
				options.StopOnDuplicateToken = c.stopOnDuplicationToken
			})
			for paginator.HasMorePages() {
				_, err := paginator.NextPage(context.TODO())
				if err != nil {
					t.Errorf("error: %v", err)
				}
			}

			inputLen := len(client.inputs)
			if inputLen != c.requestCnt {
				t.Errorf("Expect total request number to be %d, got %d", c.requestCnt, inputLen)
			}
			for i := 1; i < inputLen; i++ {
				if *client.inputs[i].StartRecordName != *c.outputs[i-1].NextRecordName {
					t.Errorf("Expect next input's RecordName to be eaqul to %s, got %s",
						*c.outputs[i-1].NextRecordName, *client.inputs[i].StartRecordName)
				}
				if *client.inputs[i].StartRecordIdentifier != *c.outputs[i-1].NextRecordIdentifier {
					t.Errorf("Expect next input's RecordIdentifier to be eaqul to %s, got %s",
						*c.outputs[i-1].NextRecordIdentifier, *client.inputs[i].StartRecordIdentifier)
				}
				if client.inputs[i].StartRecordType != c.outputs[i-1].NextRecordType {
					t.Errorf("Expect next input's RecordType to be eaqul to %s, got %s",
						c.outputs[i-1].NextRecordType, client.inputs[i].StartRecordType)
				}
			}
		})
	}
}