go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/util/bqutil/storagewrite_test.go (about) 1 // Copyright 2023 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 bqutil 16 17 import ( 18 "strings" 19 "testing" 20 21 . "github.com/smartystreets/goconvey/convey" 22 "google.golang.org/protobuf/proto" 23 24 bqpb "go.chromium.org/luci/bisection/proto/bq" 25 . "go.chromium.org/luci/common/testing/assertions" 26 ) 27 28 func TestBatch(t *testing.T) { 29 Convey(`Batch`, t, func() { 30 Convey(`Non-empty`, func() { 31 var rows []proto.Message 32 for i := 0; i < 10; i++ { 33 // Rows of ~1 MB each. 34 row := &bqpb.TestAnalysisRow{ 35 Project: strings.Repeat("a", 999950), 36 } 37 So(proto.Size(row), ShouldEqual, 999954) 38 rows = append(rows, row) 39 } 40 41 result, err := batch(rows) 42 So(err, ShouldBeNil) 43 // ~9 MB in the first batch. 44 So(result[0], ShouldHaveLength, 9) 45 // The rest of the rows in the remaining batch. 46 So(result[1], ShouldHaveLength, 1) 47 }) 48 Convey(`Empty`, func() { 49 result, err := batch(nil) 50 So(err, ShouldBeNil) 51 So(result, ShouldHaveLength, 0) 52 }) 53 Convey(`Single row too large`, func() { 54 // 10 MB row. 55 row := &bqpb.TestAnalysisRow{ 56 Project: strings.Repeat("a", 10*1000*1000), 57 } 58 rows := []proto.Message{row} 59 _, err := batch(rows) 60 So(err, ShouldErrLike, "a single row exceeds the maximum BigQuery AppendRows request size of 9000000 bytes") 61 }) 62 }) 63 }