-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmissing_one_implementation_feature_list_test.go
More file actions
414 lines (386 loc) · 12.9 KB
/
missing_one_implementation_feature_list_test.go
File metadata and controls
414 lines (386 loc) · 12.9 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcpspanner
import (
"context"
"reflect"
"slices"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// nolint:dupl // WONTFIX
func loadDataForListMissingOneImplFeatureList(ctx context.Context, t *testing.T, client *Client) {
webFeatures := []WebFeature{
{FeatureKey: "FeatureX", Name: "Cool API"},
{FeatureKey: "FeatureY", Name: "Super API"},
{FeatureKey: "FeatureZ", Name: "Neat API"},
{FeatureKey: "FeatureW", Name: "Amazing API"},
}
for _, feature := range webFeatures {
_, err := client.UpsertWebFeature(ctx, feature)
if err != nil {
t.Errorf("unexpected error during insert of features. %s", err.Error())
}
}
browserReleases := []BrowserRelease{
// fooBrowser Releases
{BrowserName: "fooBrowser", BrowserVersion: "110", ReleaseDate: time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)},
{BrowserName: "fooBrowser", BrowserVersion: "111", ReleaseDate: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC)},
{BrowserName: "fooBrowser", BrowserVersion: "112", ReleaseDate: time.Date(2024, 3, 15, 0, 0, 0, 0, time.UTC)},
{BrowserName: "fooBrowser", BrowserVersion: "113", ReleaseDate: time.Date(2024, 4, 15, 0, 0, 0, 0, time.UTC)},
// barBrowser Releases
{BrowserName: "barBrowser", BrowserVersion: "113", ReleaseDate: time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)},
{BrowserName: "barBrowser", BrowserVersion: "114", ReleaseDate: time.Date(2024, 3, 28, 0, 0, 0, 0, time.UTC)},
{BrowserName: "barBrowser", BrowserVersion: "115", ReleaseDate: time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC)},
// bazBrowser Releases
{BrowserName: "bazBrowser", BrowserVersion: "16.4", ReleaseDate: time.Date(2024, 1, 25, 0, 0, 0, 0, time.UTC)},
{BrowserName: "bazBrowser", BrowserVersion: "16.5", ReleaseDate: time.Date(2024, 3, 5, 0, 0, 0, 0, time.UTC)},
{BrowserName: "bazBrowser", BrowserVersion: "17", ReleaseDate: time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC)},
}
for _, release := range browserReleases {
err := client.InsertBrowserRelease(ctx, release)
if err != nil {
t.Errorf("unexpected error during insert of releases. %s", err.Error())
}
}
browserFeatureAvailabilities := []struct {
FeatureKey string
BrowserFeatureAvailability
}{
// fooBrowser Availabilities
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "fooBrowser", BrowserVersion: "111"},
FeatureKey: "FeatureX",
}, // Available from fooBrowser 111
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "fooBrowser", BrowserVersion: "112"},
FeatureKey: "FeatureY",
}, // Available from fooBrowser 112
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "fooBrowser", BrowserVersion: "112"},
FeatureKey: "FeatureZ",
}, // Available from fooBrowser 112
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "fooBrowser", BrowserVersion: "113"},
FeatureKey: "FeatureW",
}, // Available from fooBrowser 113
// barBrowser Availabilities
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "barBrowser", BrowserVersion: "113"},
FeatureKey: "FeatureX",
}, // Available from barBrowser 113
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "barBrowser", BrowserVersion: "113"},
FeatureKey: "FeatureZ",
}, // Available from barBrowser 113
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "barBrowser", BrowserVersion: "114"},
FeatureKey: "FeatureY",
}, // Available from barBrowser 114
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "barBrowser", BrowserVersion: "115"},
FeatureKey: "FeatureW",
}, // Available from barBrowser 115
// bazBrowser Availabilities
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "bazBrowser", BrowserVersion: "16.4"},
FeatureKey: "FeatureX",
}, // Available from bazBrowser 16.4
{
BrowserFeatureAvailability: BrowserFeatureAvailability{BrowserName: "bazBrowser", BrowserVersion: "16.5"},
FeatureKey: "FeatureY",
}, // Available from bazBrowser 16.5
}
for _, availability := range browserFeatureAvailabilities {
err := client.InsertBrowserFeatureAvailability(ctx,
availability.FeatureKey, availability.BrowserFeatureAvailability)
if err != nil {
t.Errorf("unexpected error during insert. %s", err.Error())
}
}
err := spannerClient.PrecalculateBrowserFeatureSupportEvents(ctx,
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Errorf("unexpected error during pre-calculate. %s", err.Error())
}
}
// nolint:unparam // WONTFIX
func assertMissingOneImplFeatureList(ctx context.Context, t *testing.T, targetDate time.Time,
targetBrowser string, otherBrowsers []string, expectedPage *MissingOneImplFeatureListPage, token *string,
pageSize int) {
result, err := spannerClient.MissingOneImplFeatureList(
ctx,
targetBrowser,
otherBrowsers,
targetDate,
pageSize,
token,
)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(expectedPage.NextPageToken, result.NextPageToken) {
t.Errorf("unexpected result.\nExpected %+v\nReceived %+v", expectedPage, result)
}
if !assert.ElementsMatch(t, expectedPage.FeatureList, result.FeatureList) {
t.Errorf("unexpected result.\nExpected %+v\nReceived %+v", expectedPage, result)
}
}
func testMissingOneImplFeatureListSuite(
ctx context.Context,
t *testing.T,
) {
t.Run("Query bazBrowser without exclusions", func(t *testing.T) {
const targetBrowser = "bazBrowser"
otherBrowsers := []string{
"fooBrowser",
"barBrowser",
}
targetDate := time.Date(2024, 4, 15, 0, 0, 0, 0, time.UTC)
pageSize := 25
token := encodeMissingOneImplFeatureListCursor(0)
t.Run("simple successful query", func(t *testing.T) {
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: nil,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW, FeatureZ
{
WebFeatureID: "FeatureZ",
},
{
WebFeatureID: "FeatureW",
},
},
}
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
otherBrowsers,
expectedResult,
&token,
pageSize,
)
})
t.Run("empty query result", func(t *testing.T) {
emptyDate := time.Date(2024, 3, 5, 0, 0, 0, 0, time.UTC)
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: nil,
FeatureList: []MissingOneImplFeature{},
}
assertMissingOneImplFeatureList(
ctx,
t,
emptyDate,
targetBrowser,
otherBrowsers,
expectedResult,
&token,
pageSize,
)
})
t.Run("simple query at a smaller subset of otherBrowsers", func(t *testing.T) {
subsetBrowsers := []string{
"barBrowser",
}
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: nil,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW, FeatureZ
{
WebFeatureID: "FeatureZ",
},
{
WebFeatureID: "FeatureW",
},
},
}
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
subsetBrowsers,
expectedResult,
&token,
pageSize,
)
})
t.Run("simple successful query with pagination", func(t *testing.T) {
pageToken := encodeMissingOneImplFeatureListCursor(1)
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: nil,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW, FeatureZ
{
WebFeatureID: "FeatureZ",
},
},
}
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
otherBrowsers,
expectedResult,
&pageToken,
pageSize,
)
})
t.Run("Return a page token with page size 1", func(t *testing.T) {
onePerPage := 1
returnToken := encodeMissingOneImplFeatureListCursor(1)
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: &returnToken,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW, FeatureZ
{
WebFeatureID: "FeatureW",
},
},
}
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
otherBrowsers,
expectedResult,
&token,
onePerPage,
)
pageTwoToken := encodeMissingOneImplFeatureListCursor(2)
expectedResultPageTwo := &MissingOneImplFeatureListPage{
NextPageToken: &pageTwoToken,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW, FeatureZ
{
WebFeatureID: "FeatureZ",
},
},
}
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
otherBrowsers,
expectedResultPageTwo,
&returnToken,
onePerPage,
)
})
})
t.Run("with excluded/discouraged features", func(t *testing.T) {
// Exclude Feature X
excludedFeatures := []string{"FeatureX"}
for _, featureKey := range excludedFeatures {
err := spannerClient.InsertExcludedFeatureKey(ctx, featureKey)
if err != nil {
t.Fatalf("Failed to insert excluded feature key: %v", err)
}
}
// Discourage FeatureZ
discouragedFeatures := []string{"FeatureZ"}
for _, featureKey := range discouragedFeatures {
err := spannerClient.UpsertFeatureDiscouragedDetails(ctx, featureKey, FeatureDiscouragedDetails{
AccordingTo: nil,
Alternatives: nil,
})
if err != nil {
t.Fatalf("Failed to upsert feature discouraged details: %v", err)
}
}
// nolint:goconst // WONTFIX
t.Run("simple query", func(t *testing.T) {
targetBrowser := "bazBrowser"
otherBrowsers := []string{"fooBrowser", "barBrowser"}
targetDate := time.Date(2024, 4, 15, 0, 0, 0, 0, time.UTC)
pageSize := 25
token := encodeMissingOneImplFeatureListCursor(0)
expectedResult := &MissingOneImplFeatureListPage{
NextPageToken: nil,
FeatureList: []MissingOneImplFeature{
// fooBrowser 113 release
// Currently supported features:
// fooBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// barBrowser: FeatureX, FeatureZ, FeatureY, FeatureW
// bazBrowser: FeatureX, FeatureY
// Missing in on for bazBrowser: FeatureW (FeatureZ is excluded/discouraged)
{
WebFeatureID: "FeatureW",
},
},
}
// Assert with excluded/discouraged features
assertMissingOneImplFeatureList(
ctx,
t,
targetDate,
targetBrowser,
otherBrowsers,
expectedResult,
&token,
pageSize,
)
})
// Clear the excluded and discouraged features after the test
err := spannerClient.ClearExcludedFeatureKeys(ctx)
if err != nil {
t.Fatalf("Failed to clear excluded feature keys: %v", err)
}
err = spannerClient.ClearFeatureDiscouragedDetails(ctx)
if err != nil {
t.Fatalf("Failed to clear feature discouraged details: %v", err)
}
})
}
func TestListMissingOneImplFeatureList(t *testing.T) {
restartDatabaseContainer(t)
ctx := context.Background()
loadDataForListMissingOneImplFeatureList(ctx, t, spannerClient)
actualEvents := spannerClient.readAllBrowserFeatureSupportEvents(ctx, t)
slices.SortFunc(actualEvents, sortBrowserFeatureSupportEvents)
t.Run("MissingOneImplFeatureListQuery", func(t *testing.T) {
testMissingOneImplFeatureListSuite(ctx, t)
})
}