1/* 2Copyright 2018 Google LLC 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package spanner 18 19import ( 20 "context" 21 "os" 22 "testing" 23 "time" 24 25 . "cloud.google.com/go/spanner/internal/testutil" 26 sppb "google.golang.org/genproto/googleapis/spanner/v1" 27) 28 29func TestPartitionRoundTrip(t *testing.T) { 30 t.Parallel() 31 for i, want := range []Partition{ 32 {rreq: &sppb.ReadRequest{Table: "t"}}, 33 {qreq: &sppb.ExecuteSqlRequest{Sql: "sql"}}, 34 } { 35 got := serdesPartition(t, i, &want) 36 if !testEqual(got, want) { 37 t.Errorf("got: %#v\nwant:%#v", got, want) 38 } 39 } 40} 41 42func TestBROTIDRoundTrip(t *testing.T) { 43 t.Parallel() 44 tm := time.Now() 45 want := BatchReadOnlyTransactionID{ 46 tid: []byte("tid"), 47 sid: "sid", 48 rts: tm, 49 } 50 data, err := want.MarshalBinary() 51 if err != nil { 52 t.Fatal(err) 53 } 54 var got BatchReadOnlyTransactionID 55 if err := got.UnmarshalBinary(data); err != nil { 56 t.Fatal(err) 57 } 58 if !testEqual(got, want) { 59 t.Errorf("got: %#v\nwant:%#v", got, want) 60 } 61} 62 63// serdesPartition is a helper that serialize a Partition then deserialize it. 64func serdesPartition(t *testing.T, i int, p1 *Partition) (p2 Partition) { 65 var ( 66 data []byte 67 err error 68 ) 69 if data, err = p1.MarshalBinary(); err != nil { 70 t.Fatalf("#%d: encoding failed %v", i, err) 71 } 72 if err = p2.UnmarshalBinary(data); err != nil { 73 t.Fatalf("#%d: decoding failed %v", i, err) 74 } 75 return p2 76} 77 78func TestPartitionQuery_QueryOptions(t *testing.T) { 79 for _, tt := range queryOptionsTestCases() { 80 t.Run(tt.name, func(t *testing.T) { 81 if tt.env.Options != nil { 82 os.Setenv("SPANNER_OPTIMIZER_VERSION", tt.env.Options.OptimizerVersion) 83 defer os.Setenv("SPANNER_OPTIMIZER_VERSION", "") 84 } 85 86 ctx := context.Background() 87 _, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{QueryOptions: tt.client}) 88 defer teardown() 89 90 var ( 91 err error 92 txn *BatchReadOnlyTransaction 93 ps []*Partition 94 stmt = NewStatement(SelectSingerIDAlbumIDAlbumTitleFromAlbums) 95 ) 96 97 if txn, err = client.BatchReadOnlyTransaction(ctx, StrongRead()); err != nil { 98 t.Fatal(err) 99 } 100 defer txn.Cleanup(ctx) 101 102 if tt.query.Options == nil { 103 ps, err = txn.PartitionQuery(ctx, stmt, PartitionOptions{0, 3}) 104 } else { 105 ps, err = txn.PartitionQueryWithOptions(ctx, stmt, PartitionOptions{0, 3}, tt.query) 106 } 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 for _, p := range ps { 112 if got, want := p.qreq.QueryOptions.OptimizerVersion, tt.want.Options.OptimizerVersion; got != want { 113 t.Fatalf("Incorrect optimizer version: got %v, want %v", got, want) 114 } 115 } 116 }) 117 } 118} 119