-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_single.go
94 lines (88 loc) · 2.25 KB
/
test_single.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
package end2end
import (
"context"
"github.com/dal-go/dalgo/dal"
"testing"
)
func testSingleOperations(ctx context.Context, t *testing.T, db dal.DB) {
t.Run("single", func(t *testing.T) {
const id = "r0"
key := dal.NewKeyWithID(E2ETestKind1, id)
var keepGoing bool = true
if keepGoing {
keepGoing = t.Run("delete1", func(t *testing.T) {
testSingleDelete(t, db, key)
})
}
if keepGoing {
keepGoing = t.Run("get1", func(t *testing.T) {
testSingleGet(t, db, key, false)
})
}
if keepGoing {
keepGoing = t.Run("create", func(t *testing.T) {
t.Run("with_predefined_id", func(t *testing.T) {
testSingleCreateWithPredefinedID(t, db, key)
})
})
}
if keepGoing {
keepGoing = t.Run("get2", func(t *testing.T) {
testSingleGet(t, db, key, true)
})
}
if keepGoing {
/*keepGoing*/ _ = t.Run("delete2", func(t *testing.T) {
testSingleDelete(t, db, key)
})
}
})
}
func testSingleDelete(t *testing.T, db dal.DB, key *dal.Key) {
ctx := context.Background()
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.Delete(ctx, key)
})
if err != nil {
t.Errorf("Failed to delete: %v", err)
}
}
func testSingleGet(t *testing.T, db dal.DB, key *dal.Key, mustExists bool) {
var data = new(TestData)
record := dal.NewRecordWithData(key, data)
ctx := context.Background()
err := db.Get(ctx, record)
if err != nil {
if dal.IsNotFound(err) {
if mustExists {
t.Errorf("record expected to exist but received error: %v", err)
}
} else {
t.Errorf("unexpected error: %v", err)
}
} else {
if data.StringProp == "" {
t.Error("field 'StringProp' is unexpectedely empty")
}
if data.IntegerProp == 0 {
t.Error("field 'IntegerProp' is unexpectedely 0")
}
if !mustExists {
t.Error("record unexpectedely found")
}
}
}
func testSingleCreateWithPredefinedID(t *testing.T, db dal.DB, key *dal.Key) {
data := TestData{
StringProp: "str1",
IntegerProp: 1,
}
record := dal.NewRecordWithData(key, &data)
ctx := context.Background()
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.Insert(ctx, record)
})
if err != nil {
t.Errorf("got unexpected error: %v", err)
}
}