1// Copyright 2018 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//     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
15package spanner
16
17import (
18	"context"
19	"io"
20	"testing"
21
22	"cloud.google.com/go/spanner/internal/testutil"
23	sppb "google.golang.org/genproto/googleapis/spanner/v1"
24	"google.golang.org/grpc/codes"
25)
26
27func TestMockPartitionedUpdate(t *testing.T) {
28	t.Parallel()
29	ctx := context.Background()
30	ms := testutil.NewMockCloudSpanner(t, trxTs)
31	ms.Serve()
32	mc := sppb.NewSpannerClient(dialMock(t, ms))
33	client := &Client{database: "mockdb"}
34	client.clients = append(client.clients, mc)
35	stmt := NewStatement("UPDATE t SET x = 2 WHERE x = 1")
36	rowCount, err := client.PartitionedUpdate(ctx, stmt)
37	if err != nil {
38		t.Fatal(err)
39	}
40	want := int64(3)
41	if rowCount != want {
42		t.Errorf("got %d, want %d", rowCount, want)
43	}
44}
45
46func TestMockPartitionedUpdateWithQuery(t *testing.T) {
47	t.Parallel()
48	ctx := context.Background()
49	ms := testutil.NewMockCloudSpanner(t, trxTs)
50	ms.AddMsg(io.EOF, true)
51	ms.Serve()
52	mc := sppb.NewSpannerClient(dialMock(t, ms))
53	client := &Client{database: "mockdb"}
54	client.clients = append(client.clients, mc)
55	stmt := NewStatement("SELECT t.key key, t.value value FROM t_mock t")
56	_, err := client.PartitionedUpdate(ctx, stmt)
57	wantCode := codes.InvalidArgument
58	if serr, ok := err.(*Error); !ok || serr.Code != wantCode {
59		t.Errorf("got error %v, want code %s", err, wantCode)
60	}
61}
62