|
| 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 | +// https://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 deletes |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "cloud.google.com/go/bigtable" |
| 27 | + "github.com/GoogleCloudPlatform/golang-samples/internal/testutil" |
| 28 | + "github.com/google/uuid" |
| 29 | + "google.golang.org/grpc/codes" |
| 30 | + "google.golang.org/grpc/status" |
| 31 | +) |
| 32 | + |
| 33 | +func TestStreamingAndBatching(t *testing.T) { |
| 34 | + ctx := context.Background() |
| 35 | + project := os.Getenv("GOLANG_SAMPLES_BIGTABLE_PROJECT") |
| 36 | + instance := os.Getenv("GOLANG_SAMPLES_BIGTABLE_INSTANCE") |
| 37 | + if project == "" || instance == "" { |
| 38 | + t.Fatal("Failed Bigtable integration test. Set GOLANG_SAMPLES_BIGTABLE_PROJECT and GOLANG_SAMPLES_BIGTABLE_INSTANCE.") |
| 39 | + } |
| 40 | + |
| 41 | + // Create client |
| 42 | + adminClient, err := bigtable.NewAdminClient(ctx, project, instance) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("bigtable.NewAdminClient: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Create table |
| 48 | + tableName := "mobile-time-series-" + uuid.New().String()[:8] |
| 49 | + if err := adminClient.DeleteTable(ctx, tableName); err != nil && status.Code(err) != codes.NotFound { |
| 50 | + t.Fatalf("adminClient.DeleteTable(%q): %v", tableName, err) |
| 51 | + } |
| 52 | + testutil.Retry(t, 10, 10*time.Second, func(r *testutil.R) { |
| 53 | + if err := adminClient.CreateTable(ctx, tableName); err != nil { |
| 54 | + if status.Code(err) == codes.AlreadyExists { |
| 55 | + adminClient.DeleteTable(ctx, tableName) |
| 56 | + time.Sleep(5 * time.Second) |
| 57 | + } |
| 58 | + r.Errorf("Could not create table %s: %v", tableName, err) |
| 59 | + } |
| 60 | + }) |
| 61 | + if t.Failed() { |
| 62 | + return |
| 63 | + } |
| 64 | + defer adminClient.DeleteTable(ctx, tableName) |
| 65 | + |
| 66 | + // Create column family |
| 67 | + columnFamilyName := "cell_plan" |
| 68 | + if err := adminClient.CreateColumnFamily(ctx, tableName, columnFamilyName); err != nil { |
| 69 | + t.Fatalf("CreateColumnFamily(%s): %v", columnFamilyName, err) |
| 70 | + } |
| 71 | + |
| 72 | + // Write test data |
| 73 | + client, err := bigtable.NewClient(ctx, project, instance) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("bigtable.NewClient: %v", err) |
| 76 | + } |
| 77 | + defer client.Close() |
| 78 | + tbl := client.Open(tableName) |
| 79 | + rowKeys := []string{"phone#4c410523#20190501", "phone#5c10102#20190501"} |
| 80 | + muts := make([]*bigtable.Mutation, len(rowKeys)) |
| 81 | + for i := range rowKeys { |
| 82 | + mut := bigtable.NewMutation() |
| 83 | + mut.Set(columnFamilyName, "data_plan_01gb", 0, []byte("true")) |
| 84 | + mut.Set(columnFamilyName, "data_plan_05gb", 0, []byte("true")) |
| 85 | + muts[i] = mut |
| 86 | + } |
| 87 | + if _, err := tbl.ApplyBulk(ctx, rowKeys, muts); err != nil { |
| 88 | + t.Fatalf("tbl.ApplyBulk: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Run the function |
| 92 | + buf := new(bytes.Buffer) |
| 93 | + if err := streamingAndBatching(buf, project, instance, tableName); err != nil { |
| 94 | + t.Errorf("streamingAndBatching failed: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Verify the output message |
| 98 | + if got, want := buf.String(), "Successfully deleted cells from all rows"; !strings.Contains(got, want) { |
| 99 | + t.Errorf("streamingAndBatching output got %q, want substring %q", got, want) |
| 100 | + } |
| 101 | + |
| 102 | + // Verify that the cells were deleted |
| 103 | + for _, rowKey := range rowKeys { |
| 104 | + row, err := tbl.ReadRow(ctx, rowKey) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("tbl.ReadRow(%q): %v", rowKey, err) |
| 107 | + } |
| 108 | + if _, ok := row[columnFamilyName]; !ok { |
| 109 | + t.Errorf("row %q has no column family %q", rowKey, columnFamilyName) |
| 110 | + continue |
| 111 | + } |
| 112 | + for _, item := range row[columnFamilyName] { |
| 113 | + if item.Column == fmt.Sprintf("%s:data_plan_01gb", columnFamilyName) { |
| 114 | + t.Errorf("row %q still has cell %s:data_plan_01gb", rowKey, columnFamilyName) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments