Debian Codegraph @

docstore/gcpfirestore/fs_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
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 // https://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
15 package gcpfirestore
16
17 import (
18 "context"
19 "errors"
20 "testing"
21
22 vkit "cloud.google.com/go/firestore/apiv1"
23 pb "cloud.google.com/go/firestore/apiv1/firestorepb"
24 "gocloud.dev/docstore"
25 "gocloud.dev/docstore/driver"
26 "gocloud.dev/docstore/drivertest"
27 "gocloud.dev/internal/testing/setup"
28 "google.golang.org/api/option"
29 "google.golang.org/grpc/status"
30 "google.golang.org/protobuf/proto"
31 tspb "google.golang.org/protobuf/types/known/timestamppb"
32 )
33
34 const (
35 // projectID is the project ID that was used during the last test run using --record.
36 projectID = "go-cloud-test-216917"
37 collectionName1 = "docstore-test-1"
38 collectionName2 = "docstore-test-2"
39 collectionName3 = "docstore-test-3"
40 endPoint = "firestore.googleapis.com:443"
41 )
42
43 type harness struct {
44 client *vkit.Client
45 done func()
46 }
47
48 func (h *harness) SupportsAtomicWrites() bool {
49 return true
50 }
51
52 func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
53 t.Helper()
54
55 conn, done := setup.NewGCPgRPCConn(ctx, t, endPoint, "docstore")
56 client, err := vkit.NewClient(ctx, option.WithGRPCConn(conn))
57 if err != nil {
58 done()
59 return nil, err
60 }
61 return &harness{client, done}, nil
62 }
63
64 func (h *harness) MakeCollection(_ context.Context, kind drivertest.CollectionKind) (driver.Collection, error) {
65 switch kind {
66 case drivertest.SingleKey, drivertest.NoRev:
67 return newCollection(h.client, CollectionResourceID(projectID, collectionName1), drivertest.KeyField, nil, nil)
68 case drivertest.TwoKey:
69 return newCollection(h.client, CollectionResourceID(projectID, collectionName2), "",
70 func(doc docstore.Document) string {
71 return drivertest.HighScoreKey(doc).(string)
72 }, &Options{AllowLocalFilters: true})
73 case drivertest.AltRev:
74 return newCollection(h.client, CollectionResourceID(projectID, collectionName1), drivertest.KeyField, nil,
75 &Options{RevisionField: drivertest.AlternateRevisionField})
76 default:
77 panic("bad kind")
78 }
79 }
80
81 func (*harness) BeforeDoTypes() []any {
82 return []any{&pb.BatchGetDocumentsRequest{}, &pb.CommitRequest{}}
83 }
84
85 func (*harness) BeforeQueryTypes() []any {
86 return []any{&pb.RunQueryRequest{}}
87 }
88
89 func (*harness) RevisionsEqual(rev1, rev2 any) bool {
90 return proto.Equal(rev1.(*tspb.Timestamp), rev2.(*tspb.Timestamp))
91 }
92
93 func (h *harness) Close() {
94 _ = h.client.Close()
95 h.done()
96 }
97
98 // codecTester implements drivertest.CodecTester.
99 type codecTester struct {
100 nc *nativeCodec
101 }
102
103 func (*codecTester) UnsupportedTypes() []drivertest.UnsupportedType {
104 return []drivertest.UnsupportedType{drivertest.Uint, drivertest.Arrays}
105 }
106
107 func (c *codecTester) NativeEncode(x any) (any, error) {
108 return c.nc.Encode(x)
109 }
110
111 func (c *codecTester) NativeDecode(value, dest any) error {
112 return c.nc.Decode(value.(*pb.Document), dest)
113 }
114
115 func (c *codecTester) DocstoreEncode(x any) (any, error) {
116 var e encoder
117 if err := drivertest.MustDocument(x).Encode(&e); err != nil {
118 return nil, err
119 }
120 return &pb.Document{
121 Name: "projects/P/databases/(default)/documents/C/D",
122 Fields: e.pv.GetMapValue().Fields,
123 }, nil
124 }
125
126 func (c *codecTester) DocstoreDecode(value, dest any) error {
127 mv := &pb.Value{ValueType: &pb.Value_MapValue{MapValue: &pb.MapValue{
128 Fields: value.(*pb.Document).Fields,
129 }}}
130 return drivertest.MustDocument(dest).Decode(decoder{mv})
131 }
132
133 type verifyAs struct{}
134
135 func (verifyAs) Name() string {
136 return "verify As"
137 }
138
139 func (verifyAs) CollectionCheck(coll *docstore.Collection) error {
140 var fc *vkit.Client
141 if !coll.As(&fc) {
142 return errors.New("Collection.As failed")
143 }
144 return nil
145 }
146
147 func (verifyAs) QueryCheck(it *docstore.DocumentIterator) error {
148 var c pb.Firestore_RunQueryClient
149 if !it.As(&c) {
150 return errors.New("DocumentIterator.As failed")
151 }
152 _, err := c.Header()
153 return err
154 }
155
156 func (v verifyAs) ErrorCheck(c *docstore.Collection, err error) error {
157 var s *status.Status
158 if !c.ErrorAs(err, &s) {
159 return errors.New("Collection.ErrorAs failed")
160 }
161 return nil
162 }
163
164 func TestConformance(t *testing.T) {
165 drivertest.MakeUniqueStringDeterministicForTesting(1)
166 nc, err := newNativeCodec()
167 if err != nil {
168 t.Fatal(err)
169 }
170 drivertest.RunConformanceTests(t, newHarness, &codecTester{nc}, []drivertest.AsTest{verifyAs{}})
171 }
172
173 func BenchmarkConformance(b *testing.B) {
174 ctx := context.Background()
175 client, err := vkit.NewClient(ctx)
176 if err != nil {
177 b.Fatal(err)
178 }
179 coll, err := newCollection(client, CollectionResourceID(projectID, collectionName3), drivertest.KeyField, nil, nil)
180 if err != nil {
181 b.Fatal(err)
182 }
183 drivertest.RunBenchmarks(b, docstore.NewCollection(coll))
184 }
185
186 // gcpfirestore-specific tests.
187
188 func TestResourceIDRegexp(t *testing.T) {
189 for _, good := range []string{
190 "projects/abc-_.309/databases/(default)/documents/C",
191 "projects/P/databases/(default)/documents/C/D/E",
192 "projects/P/databases/mydb/documents/E/F/G",
193 } {
194 if !resourceIDRE.MatchString(good) {
195 t.Errorf("%q did not match but should have", good)
196 }
197 }
198
199 for _, bad := range []string{
200 "",
201 "Projects/P/databases/(default)/documents/C",
202 "P/databases/(default)/documents/C",
203 "projects/P/Q/databases/(default)/documents/C",
204 "projects/P/databases/(default)/C",
205 "projects/P/databases/(default)/documents/",
206 "projects/P/databases/(default)",
207 } {
208 if resourceIDRE.MatchString(bad) {
209 t.Errorf("%q matched but should not have", bad)
210 }
211 }
212 }