-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinserter_test.go
More file actions
84 lines (74 loc) · 2.76 KB
/
Copy pathinserter_test.go
File metadata and controls
84 lines (74 loc) · 2.76 KB
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
package dalgo2firestore
import (
"context"
"errors"
"testing"
"cloud.google.com/go/firestore"
"github.com/dal-go/record"
)
// testData is a plain payload with no validation of its own: record
// invariants are now the framework's job (dal.BeforeSave, run by the pipeline
// dal.NewDB returns), not this adapter's. See TestConformance for the
// behavioural proof that Insert/Set/UpdateRecord are all covered.
type testData struct{}
func withStubbedDocRef(t *testing.T, fn func()) {
t.Helper()
origKeyToDocRef := keyToDocRef
keyToDocRef = func(_ *record.Key, _ *firestore.Client) *firestore.DocumentRef {
return &firestore.DocumentRef{ID: "test"}
}
defer func() { keyToDocRef = origKeyToDocRef }()
fn()
}
func Test_insert_success(t *testing.T) {
withStubbedDocRef(t, func() {
origCreate := createNonTransactional
createNonTransactional = func(ctx context.Context, _ *firestore.DocumentRef, _ interface{}) (*firestore.WriteResult, error) {
return &firestore.WriteResult{}, nil
}
defer func() { createNonTransactional = origCreate }()
db := database{id: "t", client: &firestore.Client{}}
key := record.NewKeyWithID("c", "1")
rec := record.NewRecordWithData(key, testData{})
if _, err := insert(context.Background(), db, rec, createNonTransactional); err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
func Test_insert_create_error(t *testing.T) {
withStubbedDocRef(t, func() {
origCreate := createNonTransactional
createNonTransactional = func(ctx context.Context, _ *firestore.DocumentRef, _ interface{}) (*firestore.WriteResult, error) {
return nil, errors.New("create failed")
}
defer func() { createNonTransactional = origCreate }()
db := database{id: "t", client: &firestore.Client{}}
key := record.NewKeyWithID("c", "1")
rec := record.NewRecordWithData(key, testData{})
if _, err := insert(context.Background(), db, rec, createNonTransactional); err == nil {
t.Fatalf("expected create error")
}
})
}
func Test_insertMulti_basic(t *testing.T) {
withStubbedDocRef(t, func() {
origCreate := createNonTransactional
count := 0
createNonTransactional = func(ctx context.Context, _ *firestore.DocumentRef, _ interface{}) (*firestore.WriteResult, error) {
count++
return &firestore.WriteResult{}, nil
}
defer func() { createNonTransactional = origCreate }()
db := database{id: "t", client: &firestore.Client{}}
records := []record.Record{
record.NewRecordWithData(record.NewKeyWithID("c", "1"), testData{}),
record.NewRecordWithData(record.NewKeyWithID("c", "2"), testData{}),
}
if err := insertMulti(context.Background(), db, records, createNonTransactional); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if count != len(records) {
t.Fatalf("expected %d creates, got %d", len(records), count)
}
})
}