Debian Codegraph @

docstore/gcpfirestore/urls_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 "testing"
20
21 "gocloud.dev/docstore"
22 "gocloud.dev/internal/testing/setup"
23 )
24
25 func TestOpenCollectionFromURL(t *testing.T) {
26 cleanup := setup.FakeGCPDefaultCredentials(t)
27 defer cleanup()
28
29 tests := []struct {
30 URL string
31 WantErr bool
32 }{
33 // OK.
34 {"firestore://projects/myproject/databases/(default)/documents/mycoll?name_field=_id", false},
35 // OK, hierarchical collection.
36 {"firestore://projects/myproject/databases/(default)/documents/mycoll/mydoc/subcoll?name_field=_id", false},
37 // Missing project ID.
38 {"firestore:///mycoll?name_field=_id", true},
39 // Empty collection.
40 {"firestore://projects/myproject/", true},
41 // Missing name field.
42 {"firestore://projects/myproject/databases/(default)/documents/mycoll", true},
43 // Passing revision field.
44 {"firestore://projects/myproject/databases/(default)/documents/mycoll?name_field=_id&revision_field=123", false},
45 // Invalid param.
46 {"firestore://projects/myproject/databases/(default)/documents/mycoll?name_field=_id&param=value", true},
47 }
48
49 ctx := context.Background()
50 for _, test := range tests {
51 d, err := docstore.OpenCollection(ctx, test.URL)
52 if d != nil {
53 defer d.Close()
54 }
55 if (err != nil) != test.WantErr {
56 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr)
57 }
58 }
59 }