github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/shared/test_run_query_medium_test.go (about) 1 // +build medium 2 3 package shared_test 4 5 import ( 6 "strconv" 7 "strings" 8 "testing" 9 "time" 10 11 mapset "github.com/deckarep/golang-set" 12 "github.com/web-platform-tests/wpt.fyi/shared" 13 "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestLoadTestRuns(t *testing.T) { 19 testRun := shared.TestRun{ 20 ProductAtRevision: shared.ProductAtRevision{ 21 Product: shared.Product{ 22 BrowserName: "chrome", 23 BrowserVersion: "63.0", 24 OSName: "linux", 25 }, 26 Revision: "1234567890", 27 }, 28 ResultsURL: "/static/chrome-63.0-linux-summary_v2.json.gz", 29 CreatedAt: time.Now(), 30 } 31 32 ctx, done, err := sharedtest.NewAEContext(true) 33 assert.Nil(t, err) 34 defer done() 35 store := shared.NewAppEngineDatastore(ctx, false) 36 key := store.NewIncompleteKey("TestRun") 37 key, _ = store.Put(key, &testRun) 38 39 chrome, _ := shared.ParseProductSpec("chrome") 40 loaded, err := store.TestRunQuery().LoadTestRuns(shared.ProductSpecs{chrome}, nil, nil, nil, nil, nil, nil) 41 allRuns := loaded.AllRuns() 42 assert.Nil(t, err) 43 assert.Equal(t, 1, len(allRuns)) 44 assert.Equalf(t, key.IntID(), allRuns[0].ID, "ID field should be populated.") 45 } 46 47 func TestLoadTestRunsBySHAs(t *testing.T) { 48 testRun := shared.TestRun{} 49 testRun.BrowserName = "chrome" 50 testRun.BrowserVersion = "63.0" 51 testRun.OSName = "linux" 52 testRun.TimeStart = time.Now() 53 54 ctx, done, err := sharedtest.NewAEContext(true) 55 assert.Nil(t, err) 56 defer done() 57 58 store := shared.NewAppEngineDatastore(ctx, false) 59 for i := 0; i < 5; i++ { 60 testRun.FullRevisionHash = strings.Repeat(strconv.Itoa(i), 40) 61 testRun.Revision = testRun.FullRevisionHash[:10] 62 testRun.TimeStart = time.Now().AddDate(0, 0, -i) 63 key := store.NewIncompleteKey("TestRun") 64 store.Put(key, &testRun) 65 } 66 67 q := store.TestRunQuery() 68 runsByProduct, err := q.LoadTestRuns(shared.GetDefaultProducts(), nil, shared.SHAs{"1111111111", "333333333"}, nil, nil, nil, nil) 69 runs := runsByProduct.AllRuns() 70 assert.Nil(t, err) 71 assert.Len(t, runs, 2) 72 for _, run := range runs { 73 assert.True(t, run.ID > 0, "ID field should be populated.") 74 } 75 assert.Equal(t, "1111111111", runs[0].Revision) 76 assert.Equal(t, "3333333333", runs[1].Revision) 77 78 runsByProduct, err = q.LoadTestRuns(shared.GetDefaultProducts(), nil, shared.SHAs{"11111", "33333"}, nil, nil, nil, nil) 79 runs = runsByProduct.AllRuns() 80 assert.Nil(t, err) 81 assert.Len(t, runs, 2) 82 for _, run := range runs { 83 assert.True(t, run.ID > 0, "ID field should be populated.") 84 } 85 assert.Equal(t, "1111111111", runs[0].Revision) 86 assert.Equal(t, "3333333333", runs[1].Revision) 87 } 88 89 func TestLoadTestRuns_Experimental_Only(t *testing.T) { 90 testRuns := shared.TestRuns{ 91 shared.TestRun{ 92 ProductAtRevision: shared.ProductAtRevision{ 93 Product: shared.Product{ 94 BrowserName: "chrome", 95 BrowserVersion: "63.0", 96 OSName: "linux", 97 }, 98 Revision: "1234567890", 99 }, 100 ResultsURL: "/static/chrome-63.0-linux-summary_v2.json.gz", 101 CreatedAt: time.Now(), 102 }, 103 shared.TestRun{ 104 // We no longer have runs like this (with the "-experimental" suffix but without 105 // the "experimental" label; and it should no longer be considered experimental. 106 ProductAtRevision: shared.ProductAtRevision{ 107 Product: shared.Product{ 108 BrowserName: "chrome-experimental", 109 BrowserVersion: "63.0", 110 OSName: "linux", 111 }, 112 Revision: "1234567890", 113 }, 114 ResultsURL: "/static/chrome-experimental-63.0-linux-summary_v2.json.gz", 115 CreatedAt: time.Now(), 116 }, 117 shared.TestRun{ 118 ProductAtRevision: shared.ProductAtRevision{ 119 Product: shared.Product{ 120 BrowserName: "chrome", 121 BrowserVersion: "64.0", 122 OSName: "linux", 123 }, 124 Revision: "1234567890", 125 }, 126 ResultsURL: "/static/chrome-64.0-linux-summary_v2.json.gz", 127 CreatedAt: time.Now(), 128 Labels: []string{"experimental"}, 129 }, 130 shared.TestRun{ 131 ProductAtRevision: shared.ProductAtRevision{ 132 Product: shared.Product{ 133 BrowserName: "chrome-experimental", 134 BrowserVersion: "65.0", 135 OSName: "linux", 136 }, 137 Revision: "1234567890", 138 }, 139 ResultsURL: "/static/chrome-experimental-65.0-linux-summary_v2.json.gz", 140 CreatedAt: time.Now(), 141 Labels: []string{"experimental"}, 142 }, 143 } 144 145 ctx, done, err := sharedtest.NewAEContext(true) 146 assert.Nil(t, err) 147 defer done() 148 149 store := shared.NewAppEngineDatastore(ctx, false) 150 keys := make([]shared.Key, len(testRuns)) 151 for i := range testRuns { 152 keys[i] = store.NewIncompleteKey("TestRun") 153 } 154 keys, err = store.PutMulti(keys, testRuns) 155 assert.Nil(t, err) 156 157 chrome, chromeExperimental := shared.ProductSpec{}, shared.ProductSpec{} 158 chrome.BrowserName = "chrome" 159 chromeExperimental.BrowserName = "chrome-experimental" 160 products := shared.ProductSpecs{chrome, chromeExperimental} 161 labels := mapset.NewSet() 162 labels.Add("experimental") 163 ten := 10 164 loaded, err := store.TestRunQuery().LoadTestRuns(products, labels, nil, nil, nil, &ten, nil) 165 allRuns := loaded.AllRuns() 166 assert.Nil(t, err) 167 assert.Equal(t, 2, len(allRuns)) 168 for _, run := range allRuns { 169 assert.True(t, run.ID > 0, "ID field should be populated.") 170 } 171 assert.Equal(t, "64.0", allRuns[0].BrowserVersion) 172 assert.Equal(t, "65.0", allRuns[1].BrowserVersion) 173 } 174 175 func TestLoadTestRuns_LabelinProductSpec(t *testing.T) { 176 testRuns := []shared.TestRun{ 177 shared.TestRun{ 178 ProductAtRevision: shared.ProductAtRevision{ 179 Product: shared.Product{BrowserName: "chrome"}, 180 }, 181 Labels: []string{"foo"}, 182 }, 183 shared.TestRun{ 184 ProductAtRevision: shared.ProductAtRevision{ 185 Product: shared.Product{BrowserName: "chrome"}, 186 }, 187 Labels: []string{"bar"}, 188 }, 189 } 190 191 ctx, done, err := sharedtest.NewAEContext(true) 192 assert.Nil(t, err) 193 defer done() 194 195 store := shared.NewAppEngineDatastore(ctx, false) 196 keys := make([]shared.Key, len(testRuns)) 197 for i := range testRuns { 198 keys[i] = store.NewIncompleteKey("TestRun") 199 } 200 keys, err = store.PutMulti(keys, testRuns) 201 assert.Nil(t, err) 202 203 products := make([]shared.ProductSpec, 1) 204 products[0].BrowserName = "chrome" 205 products[0].Labels = mapset.NewSetWith("foo") 206 loaded, err := store.TestRunQuery().LoadTestRuns(products, nil, nil, nil, nil, nil, nil) 207 allRuns := loaded.AllRuns() 208 assert.Nil(t, err) 209 assert.Equal(t, 1, len(allRuns)) 210 assert.Equal(t, "foo", allRuns[0].Labels[0]) 211 } 212 213 func TestLoadTestRuns_SHAinProductSpec(t *testing.T) { 214 testRuns := []shared.TestRun{ 215 shared.TestRun{ 216 ProductAtRevision: shared.ProductAtRevision{ 217 Product: shared.Product{BrowserName: "chrome"}, 218 FullRevisionHash: strings.Repeat("0", 40), 219 Revision: strings.Repeat("0", 10), 220 }, 221 }, 222 shared.TestRun{ 223 ProductAtRevision: shared.ProductAtRevision{ 224 Product: shared.Product{ 225 BrowserName: "chrome", 226 BrowserVersion: "63.1.1.1", 227 }, 228 FullRevisionHash: strings.Repeat("1", 40), 229 Revision: strings.Repeat("1", 10), 230 }, 231 }, 232 } 233 234 ctx, done, err := sharedtest.NewAEContext(true) 235 assert.Nil(t, err) 236 defer done() 237 238 store := shared.NewAppEngineDatastore(ctx, false) 239 keys := make([]shared.Key, len(testRuns)) 240 for i := range testRuns { 241 keys[i] = store.NewIncompleteKey("TestRun") 242 } 243 keys, err = store.PutMulti(keys, testRuns) 244 assert.Nil(t, err) 245 246 products := make([]shared.ProductSpec, 1) 247 products[0].BrowserName = "chrome" 248 products[0].Revision = strings.Repeat("1", 10) 249 loaded, err := store.TestRunQuery().LoadTestRuns(products, nil, nil, nil, nil, nil, nil) 250 assert.Nil(t, err) 251 allRuns := loaded.AllRuns() 252 assert.Equal(t, 1, len(allRuns)) 253 assert.Equal(t, "1111111111", allRuns[0].Revision) 254 255 // Partial SHA 256 products[0].Revision = "11111" 257 loaded, err = store.TestRunQuery().LoadTestRuns(products, nil, nil, nil, nil, nil, nil) 258 allRuns = loaded.AllRuns() 259 assert.Equal(t, 1, len(allRuns)) 260 assert.Equal(t, "1111111111", allRuns[0].Revision) 261 262 // Partial SHA, Browser version 263 products[0].BrowserVersion = "63" 264 loaded, err = store.TestRunQuery().LoadTestRuns(products, nil, nil, nil, nil, nil, nil) 265 allRuns = loaded.AllRuns() 266 assert.Equal(t, 1, len(allRuns)) 267 assert.Equal(t, "1111111111", allRuns[0].Revision) 268 269 // Partial SHA, Exact version 270 products[0].BrowserVersion = "63.1.1.1" 271 loaded, err = store.TestRunQuery().LoadTestRuns(products, nil, nil, nil, nil, nil, nil) 272 allRuns = loaded.AllRuns() 273 assert.Equal(t, 1, len(allRuns)) 274 assert.Equal(t, "1111111111", allRuns[0].Revision) 275 } 276 277 func TestLoadTestRuns_Ordering(t *testing.T) { 278 testRuns := []shared.TestRun{ 279 shared.TestRun{ 280 ProductAtRevision: shared.ProductAtRevision{ 281 Product: shared.Product{ 282 BrowserName: "chrome", 283 }, 284 Revision: "1234567890", 285 }, 286 CreatedAt: time.Now(), 287 TimeStart: time.Now().AddDate(0, 0, -1), 288 }, 289 shared.TestRun{ 290 ProductAtRevision: shared.ProductAtRevision{ 291 Product: shared.Product{ 292 BrowserName: "chrome", 293 }, 294 Revision: "0987654321", 295 }, 296 CreatedAt: time.Now().AddDate(0, 0, -1), 297 TimeStart: time.Now(), 298 }, 299 } 300 301 ctx, done, err := sharedtest.NewAEContext(true) 302 assert.Nil(t, err) 303 defer done() 304 305 store := shared.NewAppEngineDatastore(ctx, false) 306 for _, testRun := range testRuns { 307 key := store.NewIncompleteKey("TestRun") 308 store.Put(key, &testRun) 309 } 310 311 chrome, _ := shared.ParseProductSpec("chrome") 312 loaded, err := store.TestRunQuery().LoadTestRuns(shared.ProductSpecs{chrome}, nil, nil, nil, nil, nil, nil) 313 assert.Nil(t, err) 314 allRuns := loaded.AllRuns() 315 assert.Equal(t, 2, len(allRuns)) 316 // Runs should be ordered descendingly by TimeStart. 317 assert.Equal(t, "0987654321", allRuns[0].Revision) 318 assert.Equal(t, "1234567890", allRuns[1].Revision) 319 } 320 321 func TestLoadTestRuns_From(t *testing.T) { 322 now := time.Now() 323 yesterday := now.AddDate(0, 0, -1) 324 lastWeek := now.AddDate(0, 0, -7) 325 testRuns := []shared.TestRun{ 326 shared.TestRun{ 327 ProductAtRevision: shared.ProductAtRevision{ 328 Product: shared.Product{ 329 BrowserName: "chrome", 330 }, 331 Revision: "1234567890", 332 }, 333 TimeStart: now, 334 }, 335 shared.TestRun{ 336 ProductAtRevision: shared.ProductAtRevision{ 337 Product: shared.Product{ 338 BrowserName: "chrome", 339 }, 340 Revision: "0987654321", 341 }, 342 TimeStart: lastWeek, 343 }, 344 } 345 346 ctx, done, err := sharedtest.NewAEContext(true) 347 assert.Nil(t, err) 348 defer done() 349 350 store := shared.NewAppEngineDatastore(ctx, false) 351 for _, testRun := range testRuns { 352 key := store.NewIncompleteKey("TestRun") 353 store.Put(key, &testRun) 354 } 355 356 chrome, _ := shared.ParseProductSpec("chrome") 357 loaded, err := store.TestRunQuery().LoadTestRuns(shared.ProductSpecs{chrome}, nil, nil, &yesterday, nil, nil, nil) 358 assert.Nil(t, err) 359 allRuns := loaded.AllRuns() 360 assert.Equal(t, 1, len(allRuns)) 361 assert.Equal(t, "1234567890", allRuns[0].Revision) 362 } 363 364 func TestLoadTestRuns_To(t *testing.T) { 365 now := time.Now() 366 yesterday := now.AddDate(0, 0, -1) 367 testRuns := []shared.TestRun{ 368 shared.TestRun{ 369 ProductAtRevision: shared.ProductAtRevision{ 370 Product: shared.Product{ 371 BrowserName: "chrome", 372 }, 373 Revision: "1234567890", 374 }, 375 TimeStart: now, 376 }, 377 shared.TestRun{ 378 ProductAtRevision: shared.ProductAtRevision{ 379 Product: shared.Product{ 380 BrowserName: "chrome", 381 }, 382 Revision: "0987654321", 383 }, 384 TimeStart: yesterday, 385 }, 386 } 387 388 ctx, done, err := sharedtest.NewAEContext(true) 389 assert.Nil(t, err) 390 defer done() 391 392 store := shared.NewAppEngineDatastore(ctx, false) 393 for _, testRun := range testRuns { 394 key := store.NewIncompleteKey("TestRun") 395 store.Put(key, &testRun) 396 } 397 398 chrome, _ := shared.ParseProductSpec("chrome") 399 loaded, err := store.TestRunQuery().LoadTestRuns(shared.ProductSpecs{chrome}, nil, nil, nil, &now, nil, nil) 400 assert.Nil(t, err) 401 allRuns := loaded.AllRuns() 402 assert.Equal(t, 1, len(allRuns)) 403 assert.Equal(t, "0987654321", allRuns[0].Revision) 404 } 405 406 func TestGetAlignedRunSHAs(t *testing.T) { 407 ctx, done, err := sharedtest.NewAEContext(true) 408 assert.Nil(t, err) 409 defer done() 410 411 browserNames := shared.GetDefaultBrowserNames() 412 413 // Nothing in datastore. 414 store := shared.NewAppEngineDatastore(ctx, false) 415 q := store.TestRunQuery() 416 shas, _, _ := q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 417 assert.Equal(t, 0, len(shas)) 418 419 // Only 3 browsers. 420 run := shared.TestRun{ 421 ProductAtRevision: shared.ProductAtRevision{ 422 Revision: "abcdef0000", 423 }, 424 Labels: []string{"foo"}, 425 TimeStart: time.Now().AddDate(0, 0, -1), 426 } 427 428 for _, browser := range browserNames[:len(browserNames)-1] { 429 run.BrowserName = browser 430 store.Put(store.NewIncompleteKey("TestRun"), &run) 431 } 432 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 433 assert.Len(t, shas, 0) 434 435 // But, if request by any subset of those 3 browsers, we find the SHA. 436 var products shared.ProductSpecs 437 for _, browser := range browserNames[:len(browserNames)-1] { 438 product := shared.ProductSpec{} 439 product.BrowserName = browser 440 products = append(products, product) 441 shas, _, _ = q.GetAlignedRunSHAs(products, nil, nil, nil, nil, nil) 442 assert.Len(t, shas, 1) 443 } 444 // And labels 445 shas, _, _ = q.GetAlignedRunSHAs(products, mapset.NewSetWith("foo"), nil, nil, nil, nil) 446 assert.Len(t, shas, 1) 447 shas, _, _ = q.GetAlignedRunSHAs(products, mapset.NewSetWith("bar"), nil, nil, nil, nil) 448 assert.Len(t, shas, 0) 449 450 // All 4 browsers, but experimental. 451 run.Revision = "abcdef0111" 452 run.TimeStart = time.Now().AddDate(0, 0, -2) 453 for _, browser := range browserNames { 454 run.BrowserName = browser + "-" + shared.ExperimentalLabel 455 store.Put(store.NewIncompleteKey("TestRun"), &run) 456 } 457 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 458 assert.Equal(t, 0, len(shas)) 459 460 // 2 browsers, and other 2, but experimental. 461 run.Revision = "abcdef0222" 462 run.TimeStart = time.Now().AddDate(0, 0, -3) 463 for i, browser := range browserNames { 464 run.BrowserName = browser 465 if i > 1 { 466 run.BrowserName += "-" + shared.ExperimentalLabel 467 } 468 store.Put(store.NewIncompleteKey("TestRun"), &run) 469 } 470 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 471 assert.Equal(t, 0, len(shas)) 472 473 // 2 browsers which are twice. 474 run.Revision = "abcdef0333" 475 run.TimeStart = time.Now().AddDate(0, 0, -3) 476 for _, browser := range browserNames[:2] { 477 run.BrowserName = browser 478 store.Put(store.NewIncompleteKey("TestRun"), &run) 479 store.Put(store.NewIncompleteKey("TestRun"), &run) 480 } 481 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 482 assert.Equal(t, 0, len(shas)) 483 484 // All 4 browsers. 485 run.Revision = "abcdef0123" 486 run.TimeStart = time.Now().AddDate(0, 0, -4) 487 for _, browser := range browserNames { 488 run.BrowserName = browser 489 store.Put(store.NewIncompleteKey("TestRun"), &run) 490 } 491 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 492 assert.Equal(t, []string{"abcdef0123"}, shas) 493 494 // Another (earlier) run, also all 4 browsers. 495 run.Revision = "abcdef9999" 496 run.TimeStart = time.Now().AddDate(0, 0, -5) 497 for _, browser := range browserNames { 498 run.BrowserName = browser 499 store.Put(store.NewIncompleteKey("TestRun"), &run) 500 } 501 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, nil, nil) 502 assert.Equal(t, []string{"abcdef0123", "abcdef9999"}, shas) 503 // Limit 1 504 one := 1 505 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, &one, nil) 506 assert.Equal(t, []string{"abcdef0123"}, shas) 507 // Limit 1, Offset 1 508 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, nil, nil, &one, &one) 509 assert.Equal(t, []string{"abcdef9999"}, shas) 510 // From 4 days ago @ midnight. 511 from := time.Now().AddDate(0, 0, -4).Truncate(24 * time.Hour) 512 shas, _, _ = q.GetAlignedRunSHAs(shared.GetDefaultProducts(), nil, &from, nil, nil, nil) 513 assert.Equal(t, []string{"abcdef0123"}, shas) 514 }