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	"testing"
21	"time"
22
23	sppb "google.golang.org/genproto/googleapis/spanner/v1"
24)
25
26func TestPartitionRoundTrip(t *testing.T) {
27	t.Parallel()
28	for i, want := range []Partition{
29		{rreq: &sppb.ReadRequest{Table: "t"}},
30		{qreq: &sppb.ExecuteSqlRequest{Sql: "sql"}},
31	} {
32		got := serdesPartition(t, i, &want)
33		if !testEqual(got, want) {
34			t.Errorf("got: %#v\nwant:%#v", got, want)
35		}
36	}
37}
38
39func TestBROTIDRoundTrip(t *testing.T) {
40	t.Parallel()
41	tm := time.Now()
42	want := BatchReadOnlyTransactionID{
43		tid: []byte("tid"),
44		sid: "sid",
45		rts: tm,
46	}
47	data, err := want.MarshalBinary()
48	if err != nil {
49		t.Fatal(err)
50	}
51	var got BatchReadOnlyTransactionID
52	if err := got.UnmarshalBinary(data); err != nil {
53		t.Fatal(err)
54	}
55	if !testEqual(got, want) {
56		t.Errorf("got: %#v\nwant:%#v", got, want)
57	}
58}
59
60// serdesPartition is a helper that serialize a Partition then deserialize it
61func serdesPartition(t *testing.T, i int, p1 *Partition) (p2 Partition) {
62	var (
63		data []byte
64		err  error
65	)
66	if data, err = p1.MarshalBinary(); err != nil {
67		t.Fatalf("#%d: encoding failed %v", i, err)
68	}
69	if err = p2.UnmarshalBinary(data); err != nil {
70		t.Fatalf("#%d: decoding failed %v", i, err)
71	}
72	return p2
73}
74