go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/common/bq/fake.go (about)

     1  // Copyright 2021 The LUCI Authors.
     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 bq
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  )
    23  
    24  // Fake is a fake BQ client for tests.
    25  type Fake struct {
    26  	// mu protects access/mutation to this Fake.
    27  	mu sync.RWMutex
    28  	// sent is a map of "project.dataset.table" keys to slices of sent rows.
    29  	sent map[string][]proto.Message
    30  }
    31  
    32  // SendRow provides a mock SendRow implementation for tests.
    33  func (f *Fake) SendRow(ctx context.Context, row Row) error {
    34  	f.mu.Lock()
    35  	defer f.mu.Unlock()
    36  	key := row.CloudProject + "." + row.Dataset + "." + row.Table
    37  	if f.sent == nil {
    38  		f.sent = make(map[string][]proto.Message)
    39  	}
    40  	f.sent[key] = append(f.sent[key], row.Payload)
    41  	return nil
    42  }
    43  
    44  // Rows returns the stored rows for a given dataset and table.
    45  //
    46  // cloudProject can be empty, implying the same cloud project as the running
    47  // code.
    48  func (f *Fake) Rows(cloudProject, dataset, table string) []proto.Message {
    49  	f.mu.RLock()
    50  	defer f.mu.RUnlock()
    51  	rows := f.sent[cloudProject+"."+dataset+"."+table]
    52  	ret := make([]proto.Message, len(rows))
    53  	copy(ret, rows)
    54  	return ret
    55  }
    56  
    57  // RowsCount returns the number of stored rows for a given dataset and table.
    58  //
    59  // cloudProject can be empty, implying the same cloud project as the running
    60  // code.
    61  func (f *Fake) RowsCount(cloudProject, dataset, table string) int {
    62  	f.mu.RLock()
    63  	defer f.mu.RUnlock()
    64  	return len(f.sent[cloudProject+"."+dataset+"."+table])
    65  }
    66  
    67  // TotalSent returns total number of all rows sent across all destinations.
    68  func (f *Fake) TotalSent() int {
    69  	f.mu.RLock()
    70  	defer f.mu.RUnlock()
    71  	cnt := 0
    72  	for _, rows := range f.sent {
    73  		cnt += len(rows)
    74  	}
    75  	return cnt
    76  }
    77  
    78  // Ensure that Fake implement the Client interface.
    79  var _ Client = (*Fake)(nil)