aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/middleware/ordered_group_test.go
blob: 706b14904f578805b1d9ffd8677eb24ca1e19372 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package middleware

import (
	"reflect"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestOrderedIDsAdd(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Add(&mockIder{"second"}, After))
	noError(t, o.Add(&mockIder{"third"}, After))
	noError(t, o.Add(&mockIder{"real-first"}, Before))

	if err := o.Add(&mockIder{""}, After); err == nil {
		t.Errorf("expect error adding empty ID, got none")
	}
	if err := o.Add(&mockIder{"second"}, After); err == nil {
		t.Errorf("expect error adding duplicate, got none")
	}
	if err := o.Add(&mockIder{"unique"}, 123); err == nil {
		t.Errorf("expect error add unknown relative position, got none")
	}

	expectIDs := []string{"real-first", "first", "second", "third"}

	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}
}

func TestOrderedIDsInsert(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Insert(&mockIder{"third"}, "first", After))
	noError(t, o.Insert(&mockIder{"second"}, "third", Before))
	noError(t, o.Insert(&mockIder{"real-first"}, "first", Before))
	noError(t, o.Insert(&mockIder{"not-yet-last"}, "second", After))
	noError(t, o.Insert(&mockIder{"last"}, "third", After))

	if err := o.Insert(&mockIder{""}, "first", After); err == nil {
		t.Errorf("expect error insert empty ID, got none")
	}
	if err := o.Insert(&mockIder{"second"}, "", After); err == nil {
		t.Errorf("expect error insert with empty relative ID, got none")
	}
	if err := o.Insert(&mockIder{"second"}, "third", After); err == nil {
		t.Errorf("expect error insert duplicate, got none")
	}
	if err := o.Insert(&mockIder{"unique"}, "not-found", After); err == nil {
		t.Errorf("expect error insert not found relative ID, got none")
	}
	if err := o.Insert(&mockIder{"unique"}, "first", 123); err == nil {
		t.Errorf("expect error insert unknown relative position, got none")
	}

	expectIDs := []string{"real-first", "first", "second", "not-yet-last", "third", "last"}
	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}
}

func TestOrderedIDsGet(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Add(&mockIder{"second"}, After))

	f, ok := o.Get("not-found")
	if ok || f != nil {
		t.Fatalf("expect id not to be found, but was")
	}

	f, ok = o.Get("first")
	if !ok {
		t.Fatalf("expect id to be found, was not")
	}
	if e, a := "first", f.ID(); e != a {
		t.Errorf("expect %v id, got %v", e, a)
	}
}

func TestOrderedIDsSwap(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Add(&mockIder{"second"}, After))
	noError(t, o.Add(&mockIder{"third"}, After))

	if _, err := o.Swap("first", &mockIder{""}); err == nil {
		t.Errorf("expect error swap empty ID, got none")
	}
	if _, err := o.Swap("", &mockIder{"second"}); err == nil {
		t.Errorf("expect error swap with empty relative ID, got none")
	}

	if _, err := o.Swap("not-exists", &mockIder{"last"}); err == nil {
		t.Errorf("expect error swap not-exists ID, got none")
	}
	if _, err := o.Swap("second", &mockIder{"first"}); err == nil {
		t.Errorf("expect error swap to existing ID, got none")
	}

	r, err := o.Swap("second", &mockIder{"otherSecond"})
	noError(t, err)
	if r == nil {
		t.Fatalf("expect removed item to be returned")
	}
	if e, a := "second", r.ID(); e != a {
		t.Errorf("expect %v removed ider, got %v", e, a)
	}

	expectIDs := []string{"first", "otherSecond", "third"}
	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}
}

func TestOrderedIDsRemove(t *testing.T) {
	o := newOrderedIDs()
	firstIder := &mockIder{"first"}
	noError(t, o.Add(firstIder, After))
	noError(t, o.Insert(&mockIder{"third"}, "first", After))
	if removed, err := o.Remove("first"); err != nil {
		t.Errorf("expect no error, got %v", err)
	} else if removed != firstIder {
		t.Error("removed ider did not match expected")
	}
	noError(t, o.Insert(&mockIder{"last"}, "third", After))

	if _, err := o.Remove(""); err == nil {
		t.Errorf("expect error remove empty ID, got none")
	}
	if _, err := o.Remove("not-exists"); err == nil {
		t.Errorf("expect error remove not exists ID, got none")
	}

	expectIDs := []string{"third", "last"}
	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}
}

func TestOrderedIDsClear(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Add(&mockIder{"second"}, After))

	o.Clear()

	noError(t, o.Add(&mockIder{"third"}, After))
	noError(t, o.Add(&mockIder{"fourth"}, After))

	expectIDs := []string{"third", "fourth"}
	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}
}

func TestOrderedIDsGetOrder(t *testing.T) {
	o := newOrderedIDs()

	noError(t, o.Add(&mockIder{"first"}, After))
	noError(t, o.Add(&mockIder{"second"}, After))
	noError(t, o.Add(&mockIder{"third"}, After))
	noError(t, o.Add(&mockIder{"real-first"}, Before))

	expectIDs := []string{"real-first", "first", "second", "third"}
	if e, a := expectIDs, o.List(); !reflect.DeepEqual(e, a) {
		t.Errorf("expect %v order, got %v", e, a)
	}

	actualOrder := o.GetOrder()

	if e, a := len(expectIDs), len(actualOrder); e != a {
		t.Errorf("expect %v IDs, got %v", e, a)
	}

	compareGetOrder(t, expectIDs, actualOrder)
}

func TestRelativeOrder_insert(t *testing.T) {
	var ro relativeOrder

	if err := ro.insert(0, After); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(0, Before); err != nil {
		t.Errorf("expect no error, got %v", err)
	}

	if len(ro.order) > 0 {
		t.Errorf("expect slice to be empty")
	}

	if err := ro.insert(0, After, "foo"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(0, After, "bar"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(1, After, "baz"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(1, Before, "fob"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(3, Before, "bas"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(3, After, "bat"); err != nil {
		t.Errorf("expect no error, got %v", err)
	}
	if err := ro.insert(0, 1024, "zzz"); err == nil {
		t.Error("expect error, got nil")
	}

	if diff := cmp.Diff([]string{"foo", "fob", "bar", "bas", "bat", "baz"}, ro.order); len(diff) > 0 {
		t.Error(diff)
	}
}

func compareGetOrder(t *testing.T, expected []string, actual []interface{}) {
	t.Helper()
	for _, eID := range expected {
		var found bool
		for _, aIder := range actual {
			if e, a := eID, aIder.(ider).ID(); e == a {
				if found {
					t.Errorf("expect only one %v, got more", e)
				}
				found = true
			}
		}
		if !found {
			t.Errorf("expect to find %v, did not", eID)
		}
	}
}