-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsave_test.go
131 lines (120 loc) · 4 KB
/
save_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 Google LLC
//
// 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
//
// https://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 (
"bytes"
"context"
"os"
"strings"
"testing"
"cloud.google.com/go/firestore"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)
func TestSave(t *testing.T) {
testutil.EndToEndTest(t)
// TODO: revert this to testutil.SystemTest(t).ProjectID
// when datastore and firestore can co-exist in a project.
projectID := os.Getenv("GOLANG_SAMPLES_FIRESTORE_PROJECT")
if projectID == "" {
t.Skip("Skipping firestore test. Set GOLANG_SAMPLES_FIRESTORE_PROJECT.")
}
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
t.Fatal(err)
}
// TODO(someone): check values of docs to make sure data is being manipulated properly.
if err = addDocAsMap(ctx, client); err != nil {
t.Fatalf("addDocAsMap: %v", err)
}
if err = addDocDataTypes(ctx, client); err != nil {
t.Fatalf("addDocDataTypes: %v", err)
}
if err = addDocAsEntity(ctx, client); err != nil {
t.Fatalf("addDocAsEntity: %v", err)
}
if err = addDocWithID(ctx, client); err != nil {
t.Fatalf("addDocWithID: %v", err)
}
if err = addDocWithoutID(ctx, client); err != nil {
t.Fatalf("addDocWithoutID: %v", err)
}
if err = addDocAfterAutoGeneratedID(ctx, client); err != nil {
t.Fatalf("addDocAfterAutoGeneratedID: %v", err)
}
if err = updateDoc(ctx, client); err != nil {
t.Fatalf("updateDoc: %v", err)
}
if err = updateDocCreateIfMissing(ctx, client); err != nil {
t.Fatalf("updateDocCreateIfMissing: %v", err)
}
if err = updateDocMultiple(ctx, client); err != nil {
t.Fatalf("updateDocMultiple: %v", err)
}
if err = updateDocNested(ctx, client); err != nil {
t.Fatalf("updateDocNested: %v", err)
}
if value, _, err := getField(ctx, client, "users", "frank", "favorites"); err != nil {
t.Fatal(err)
} else {
favorites := value.(map[string]interface{})
if got, want := favorites["color"], "Red"; got != want {
t.Errorf("users/frank/favorites.color = %#v; want %#v", got, want)
}
if got, want := favorites["food"], "Pizza"; got != want {
t.Errorf("users/frank/favorites.age = %#v; want %#v", got, want)
}
}
if err = deleteDoc(ctx, client); err != nil {
t.Fatalf("deleteDoc: %v", err)
}
if _, exists, err := getField(ctx, client, "cities", "BJ", "capital"); err != nil {
t.Fatal(err)
} else if !exists {
t.Error("Expected 'cities/BJ/capital' to be present")
}
if err = deleteField(ctx, client); err != nil {
t.Fatalf("deleteField: %v", err)
}
if _, exists, err := getField(ctx, client, "cities", "BJ", "capital"); err != nil {
t.Fatal(err)
} else if exists {
t.Error("Expected 'cities/BJ/capital' to be deleted")
}
if err = runSimpleTransaction(ctx, client); err != nil {
t.Fatalf("runSimpleTransaction: %v", err)
}
if _, err = infoTransaction(ctx, client); err != nil {
t.Fatalf("infoTransaction: %v", err)
}
if err = batchWrite(ctx, client); err != nil {
t.Fatalf("batchWrite: %v", err)
}
var buf bytes.Buffer
if err := deleteCollection(&buf, projectID, "cities", 2); err != nil {
t.Fatalf("Cannot delete collection %v", err)
}
got := buf.String()
if !strings.Contains(got, "cities") {
t.Errorf("wanted collection name in deleteCollection results")
}
}
func getField(ctx context.Context, client *firestore.Client, collection, doc, field string) (value interface{}, exists bool, err error) {
dsnap, err := client.Collection(collection).Doc(doc).Get(ctx)
if err != nil {
return nil, false, err
}
val, ok := dsnap.Data()[field]
return val, ok, nil
}