You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

148 lines
3.8 KiB

  1. // Copyright 2017 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. package firestore
  15. import (
  16. "context"
  17. "fmt"
  18. "testing"
  19. "time"
  20. "cloud.google.com/go/internal/testutil"
  21. "github.com/golang/protobuf/ptypes"
  22. tspb "github.com/golang/protobuf/ptypes/timestamp"
  23. "github.com/google/go-cmp/cmp"
  24. "github.com/google/go-cmp/cmp/cmpopts"
  25. "google.golang.org/api/option"
  26. pb "google.golang.org/genproto/googleapis/firestore/v1"
  27. "google.golang.org/genproto/googleapis/type/latlng"
  28. "google.golang.org/grpc"
  29. )
  30. var (
  31. aTime = time.Date(2017, 1, 26, 0, 0, 0, 0, time.UTC)
  32. aTime2 = time.Date(2017, 2, 5, 0, 0, 0, 0, time.UTC)
  33. aTime3 = time.Date(2017, 3, 20, 0, 0, 0, 0, time.UTC)
  34. aTimestamp = mustTimestampProto(aTime)
  35. aTimestamp2 = mustTimestampProto(aTime2)
  36. aTimestamp3 = mustTimestampProto(aTime3)
  37. )
  38. func mustTimestampProto(t time.Time) *tspb.Timestamp {
  39. ts, err := ptypes.TimestampProto(t)
  40. if err != nil {
  41. panic(err)
  42. }
  43. return ts
  44. }
  45. var cmpOpts = []cmp.Option{
  46. cmp.AllowUnexported(DocumentRef{}, CollectionRef{}, DocumentSnapshot{},
  47. Query{}, filter{}, order{}, fpv{}),
  48. cmpopts.IgnoreTypes(Client{}, &Client{}),
  49. }
  50. // testEqual implements equality for Firestore tests.
  51. func testEqual(a, b interface{}) bool {
  52. return testutil.Equal(a, b, cmpOpts...)
  53. }
  54. func testDiff(a, b interface{}) string {
  55. return testutil.Diff(a, b, cmpOpts...)
  56. }
  57. func TestTestEqual(t *testing.T) {
  58. for _, test := range []struct {
  59. a, b interface{}
  60. want bool
  61. }{
  62. {nil, nil, true},
  63. {([]int)(nil), nil, false},
  64. {nil, ([]int)(nil), false},
  65. {([]int)(nil), ([]int)(nil), true},
  66. } {
  67. if got := testEqual(test.a, test.b); got != test.want {
  68. t.Errorf("testEqual(%#v, %#v) == %t, want %t", test.a, test.b, got, test.want)
  69. }
  70. }
  71. }
  72. func newMock(t *testing.T) (*Client, *mockServer) {
  73. srv, err := newMockServer()
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure(), grpc.WithBlock())
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. client, err := NewClient(context.Background(), "projectID", option.WithGRPCConn(conn))
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. return client, srv
  86. }
  87. func intval(i int) *pb.Value {
  88. return int64val(int64(i))
  89. }
  90. func int64val(i int64) *pb.Value {
  91. return &pb.Value{ValueType: &pb.Value_IntegerValue{i}}
  92. }
  93. func boolval(b bool) *pb.Value {
  94. return &pb.Value{ValueType: &pb.Value_BooleanValue{b}}
  95. }
  96. func floatval(f float64) *pb.Value {
  97. return &pb.Value{ValueType: &pb.Value_DoubleValue{f}}
  98. }
  99. func strval(s string) *pb.Value {
  100. return &pb.Value{ValueType: &pb.Value_StringValue{s}}
  101. }
  102. func bytesval(b []byte) *pb.Value {
  103. return &pb.Value{ValueType: &pb.Value_BytesValue{b}}
  104. }
  105. func tsval(t time.Time) *pb.Value {
  106. ts, err := ptypes.TimestampProto(t)
  107. if err != nil {
  108. panic(fmt.Sprintf("bad time %s in test: %v", t, err))
  109. }
  110. return &pb.Value{ValueType: &pb.Value_TimestampValue{ts}}
  111. }
  112. func geoval(ll *latlng.LatLng) *pb.Value {
  113. return &pb.Value{ValueType: &pb.Value_GeoPointValue{ll}}
  114. }
  115. func arrayval(s ...*pb.Value) *pb.Value {
  116. if s == nil {
  117. s = []*pb.Value{}
  118. }
  119. return &pb.Value{ValueType: &pb.Value_ArrayValue{&pb.ArrayValue{Values: s}}}
  120. }
  121. func mapval(m map[string]*pb.Value) *pb.Value {
  122. return &pb.Value{ValueType: &pb.Value_MapValue{&pb.MapValue{Fields: m}}}
  123. }
  124. func refval(path string) *pb.Value {
  125. return &pb.Value{ValueType: &pb.Value_ReferenceValue{path}}
  126. }