@@ -18,6 +18,7 @@ package bigtable
18
18
19
19
import (
20
20
"context"
21
+ "encoding/binary"
21
22
"flag"
22
23
"fmt"
23
24
"log"
@@ -280,6 +281,73 @@ func TestIntegration_ReadRowList(t *testing.T) {
280
281
}
281
282
}
282
283
284
+ func TestIntegration_Aggregates (t * testing.T ) {
285
+ ctx := context .Background ()
286
+ _ , _ , _ , table , _ , cleanup , err := setupIntegration (ctx , t )
287
+ if err != nil {
288
+ t .Fatal (err )
289
+ }
290
+ defer cleanup ()
291
+ key := "some-key"
292
+ family := "sum"
293
+ column := "col"
294
+ mut := NewMutation ()
295
+ mut .AddIntToCell (family , column , 1000 , 5 )
296
+
297
+ // Add 5 to empty cell.
298
+ if err := table .Apply (ctx , key , mut ); err != nil {
299
+ t .Fatalf ("Mutating row %q: %v" , key , err )
300
+ }
301
+ row , err := table .ReadRow (ctx , key )
302
+ if err != nil {
303
+ t .Fatalf ("Reading a row: %v" , err )
304
+ }
305
+ wantRow := Row {
306
+ family : []ReadItem {
307
+ {Row : key , Column : fmt .Sprintf ("%s:%s" , family , column ), Timestamp : 1000 , Value : binary .BigEndian .AppendUint64 ([]byte {}, 5 )},
308
+ },
309
+ }
310
+ if ! testutil .Equal (row , wantRow ) {
311
+ t .Fatalf ("Read row mismatch.\n got %#v\n want %#v" , row , wantRow )
312
+ }
313
+
314
+ // Add 5 again.
315
+ if err := table .Apply (ctx , key , mut ); err != nil {
316
+ t .Fatalf ("Mutating row %q: %v" , key , err )
317
+ }
318
+ row , err = table .ReadRow (ctx , key )
319
+ if err != nil {
320
+ t .Fatalf ("Reading a row: %v" , err )
321
+ }
322
+ wantRow = Row {
323
+ family : []ReadItem {
324
+ {Row : key , Column : fmt .Sprintf ("%s:%s" , family , column ), Timestamp : 1000 , Value : binary .BigEndian .AppendUint64 ([]byte {}, 10 )},
325
+ },
326
+ }
327
+ if ! testutil .Equal (row , wantRow ) {
328
+ t .Fatalf ("Read row mismatch.\n got %#v\n want %#v" , row , wantRow )
329
+ }
330
+
331
+ // Merge 5, which translates in the backend to adding 5 for sum column families.
332
+ mut2 := NewMutation ()
333
+ mut2 .MergeBytesToCell (family , column , 1000 , binary .BigEndian .AppendUint64 ([]byte {}, 5 ))
334
+ if err := table .Apply (ctx , key , mut ); err != nil {
335
+ t .Fatalf ("Mutating row %q: %v" , key , err )
336
+ }
337
+ row , err = table .ReadRow (ctx , key )
338
+ if err != nil {
339
+ t .Fatalf ("Reading a row: %v" , err )
340
+ }
341
+ wantRow = Row {
342
+ family : []ReadItem {
343
+ {Row : key , Column : fmt .Sprintf ("%s:%s" , family , column ), Timestamp : 1000 , Value : binary .BigEndian .AppendUint64 ([]byte {}, 15 )},
344
+ },
345
+ }
346
+ if ! testutil .Equal (row , wantRow ) {
347
+ t .Fatalf ("Read row mismatch.\n got %#v\n want %#v" , row , wantRow )
348
+ }
349
+ }
350
+
283
351
func TestIntegration_ReadRowListReverse (t * testing.T ) {
284
352
ctx := context .Background ()
285
353
_ , _ , _ , table , _ , cleanup , err := setupIntegration (ctx , t )
@@ -4221,6 +4289,18 @@ func setupIntegration(ctx context.Context, t *testing.T) (_ IntegrationEnv, _ *C
4221
4289
return nil , nil , nil , nil , "" , nil , err
4222
4290
}
4223
4291
4292
+ err = retryOnUnavailable (ctx , func () error {
4293
+ return adminClient .CreateColumnFamilyWithConfig (ctx , tableName , "sum" , Family {ValueType : AggregateType {
4294
+ Input : Int64Type {},
4295
+ Aggregator : SumAggregator {},
4296
+ }})
4297
+ })
4298
+ if err != nil {
4299
+ cancel ()
4300
+ t .Logf ("Error creating aggregate column family: %v" , err )
4301
+ return nil , nil , nil , nil , "" , nil , err
4302
+ }
4303
+
4224
4304
return testEnv , client , adminClient , client .Open (tableName ), tableName , func () {
4225
4305
if err := adminClient .DeleteTable (ctx , tableName ); err != nil {
4226
4306
t .Errorf ("DeleteTable got error %v" , err )
0 commit comments