|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gcpspanner |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "slices" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "cloud.google.com/go/spanner" |
| 23 | + "google.golang.org/api/iterator" |
| 24 | +) |
| 25 | + |
| 26 | +func TestUpsertBrowserCompatFeatures(t *testing.T) { |
| 27 | + restartDatabaseContainer(t) |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + feature := getSampleFeatures()[0] |
| 31 | + featureID, err := spannerClient.UpsertWebFeature(ctx, feature) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("failed to insert feature: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + initial := []string{"html.elements.address", "html.elements.section"} |
| 37 | + err = spannerClient.UpsertBrowserCompatFeatures(ctx, *featureID, initial) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("UpsertBrowserCompatFeatures initial insert failed: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + expected := slices.Clone(initial) |
| 43 | + details := readAllBrowserCompatFeatures(t, ctx, *featureID) |
| 44 | + slices.Sort(details) |
| 45 | + slices.Sort(expected) |
| 46 | + if !slices.Equal(details, expected) { |
| 47 | + t.Errorf("initial compat features mismatch.\nexpected %+v\nreceived %+v", expected, details) |
| 48 | + } |
| 49 | + |
| 50 | + updated := []string{"html.elements.article"} |
| 51 | + err = spannerClient.UpsertBrowserCompatFeatures(ctx, *featureID, updated) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("UpsertBrowserCompatFeatures update failed: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + expected = slices.Clone(updated) |
| 57 | + details = readAllBrowserCompatFeatures(t, ctx, *featureID) |
| 58 | + slices.Sort(details) |
| 59 | + slices.Sort(expected) |
| 60 | + if !slices.Equal(details, expected) { |
| 61 | + t.Errorf("updated compat features mismatch.\nexpected %+v\nreceived %+v", expected, details) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func readAllBrowserCompatFeatures(t *testing.T, ctx context.Context, featureID string) []string { |
| 66 | + stmt := spanner.NewStatement(` |
| 67 | + SELECT CompatFeature |
| 68 | + FROM WebFeatureBrowserCompatFeatures |
| 69 | + WHERE WebFeatureID = @id`) |
| 70 | + stmt.Params["id"] = featureID |
| 71 | + |
| 72 | + iter := spannerClient.Single().Query(ctx, stmt) |
| 73 | + defer iter.Stop() |
| 74 | + |
| 75 | + var features []string |
| 76 | + for { |
| 77 | + row, err := iter.Next() |
| 78 | + if err == iterator.Done { |
| 79 | + break |
| 80 | + } |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("query failed: %v", err) |
| 83 | + } |
| 84 | + var compat string |
| 85 | + if err := row.Columns(&compat); err != nil { |
| 86 | + t.Fatalf("column parse failed: %v", err) |
| 87 | + } |
| 88 | + features = append(features, compat) |
| 89 | + } |
| 90 | + |
| 91 | + return features |
| 92 | +} |
0 commit comments