2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00
restic/vendor/cloud.google.com/go/firestore/util_test.go
2017-10-22 10:07:36 +02:00

148 lines
3.6 KiB
Go

// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package firestore
import (
"fmt"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
pb "google.golang.org/genproto/googleapis/firestore/v1beta1"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
tspb "github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/api/option"
"google.golang.org/genproto/googleapis/type/latlng"
"google.golang.org/grpc"
)
var (
aTime = time.Date(2017, 1, 26, 0, 0, 0, 0, time.UTC)
aTime2 = time.Date(2017, 2, 5, 0, 0, 0, 0, time.UTC)
aTime3 = time.Date(2017, 3, 20, 0, 0, 0, 0, time.UTC)
aTimestamp = mustTimestampProto(aTime)
aTimestamp2 = mustTimestampProto(aTime2)
aTimestamp3 = mustTimestampProto(aTime3)
)
func mustTimestampProto(t time.Time) *tspb.Timestamp {
ts, err := ptypes.TimestampProto(t)
if err != nil {
panic(err)
}
return ts
}
// testEqual implements equality for Firestore tests.
func testEqual(a, b interface{}) bool {
switch a := a.(type) {
case time.Time:
return a.Equal(b.(time.Time))
case proto.Message:
return proto.Equal(a, b.(proto.Message))
case *DocumentSnapshot:
return a.equal(b.(*DocumentSnapshot))
case *DocumentRef:
return a.equal(b.(*DocumentRef))
case *CollectionRef:
return a.equal(b.(*CollectionRef))
default:
return reflect.DeepEqual(a, b)
}
}
func TestTestEqual(t *testing.T) {
for _, test := range []struct {
a, b interface{}
want bool
}{
{nil, nil, true},
{([]int)(nil), nil, false},
{nil, ([]int)(nil), false},
{([]int)(nil), ([]int)(nil), true},
} {
if got := testEqual(test.a, test.b); got != test.want {
t.Errorf("testEqual(%#v, %#v) == %t, want %t", test.a, test.b, got, test.want)
}
}
}
func newMock(t *testing.T) (*Client, *mockServer) {
srv, err := newMockServer()
if err != nil {
t.Fatal(err)
}
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
t.Fatal(err)
}
client, err := NewClient(context.Background(), "projectID", option.WithGRPCConn(conn))
if err != nil {
t.Fatal(err)
}
return client, srv
}
func intval(i int) *pb.Value {
return &pb.Value{&pb.Value_IntegerValue{int64(i)}}
}
func boolval(b bool) *pb.Value {
return &pb.Value{&pb.Value_BooleanValue{b}}
}
func floatval(f float64) *pb.Value {
return &pb.Value{&pb.Value_DoubleValue{f}}
}
func strval(s string) *pb.Value {
return &pb.Value{&pb.Value_StringValue{s}}
}
func bytesval(b []byte) *pb.Value {
return &pb.Value{&pb.Value_BytesValue{b}}
}
func tsval(t time.Time) *pb.Value {
ts, err := ptypes.TimestampProto(t)
if err != nil {
panic(fmt.Sprintf("bad time %s in test: %v", t, err))
}
return &pb.Value{&pb.Value_TimestampValue{ts}}
}
func geoval(ll *latlng.LatLng) *pb.Value {
return &pb.Value{&pb.Value_GeoPointValue{ll}}
}
func arrayval(s ...*pb.Value) *pb.Value {
if s == nil {
s = []*pb.Value{}
}
return &pb.Value{&pb.Value_ArrayValue{&pb.ArrayValue{s}}}
}
func mapval(m map[string]*pb.Value) *pb.Value {
return &pb.Value{&pb.Value_MapValue{&pb.MapValue{m}}}
}
func refval(path string) *pb.Value {
return &pb.Value{&pb.Value_ReferenceValue{path}}
}