forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_test.go
131 lines (118 loc) · 3.52 KB
/
tx_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
package dynamo
import (
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
)
func TestTx(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
date1 := time.Date(1969, 1, 1, 1, 1, 1, 0, time.UTC)
date2 := time.Date(1969, 2, 2, 2, 2, 2, 0, time.UTC)
date3 := time.Date(1969, 3, 3, 3, 3, 3, 0, time.UTC)
widget1 := widget{UserID: 69, Time: date1, Msg: "dog"}
widget2 := widget{UserID: 69, Time: date2, Msg: "cat"}
// basic write & check
table := testDB.Table(testTable)
tx := testDB.WriteTx()
var cc, ccold ConsumedCapacity
tx.Idempotent(true)
tx.Put(table.Put(widget1))
tx.Put(table.Put(widget2))
tx.Check(table.Check("UserID", 69).Range("Time", date3).IfNotExists())
tx.ConsumedCapacity(&cc)
err := tx.Run()
if err != nil {
t.Error(err)
}
if cc.Total == 0 {
t.Error("bad consumed capacity:", cc)
}
ccold = cc
err = tx.Run()
if err != nil {
t.Error(err)
}
if cc.Total <= ccold.Total {
t.Error("bad consumed capacity:", cc.Total, ccold.Total)
}
if cc.Read <= ccold.Read {
t.Error("bad consumed capacity:", cc.Read, ccold.Read)
}
if cc.Write != ccold.Write {
t.Error("bad consumed capacity:", cc.Write, "≠", ccold.Write)
}
// GetOne
getTx := testDB.GetTx()
var record1, record2, record3 widget
var cc2 ConsumedCapacity
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date1), &record1)
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date2), &record2)
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date3), &record3)
getTx.ConsumedCapacity(&cc2)
err = getTx.Run()
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(record1, widget1) {
t.Error("bad results:", record1, "≠", widget1)
}
if !reflect.DeepEqual(record2, widget2) {
t.Error("bad results:", record2, "≠", widget2)
}
if !reflect.DeepEqual(record3, widget{}) {
t.Error("bad results:", record3, "≠", widget{})
}
if cc2.Total == 0 {
t.Error("bad consumed capacity:", cc2)
}
// All
oldCC2 := cc2
var records []widget
err = getTx.All(&records)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(records, []widget{widget1, widget2}) {
t.Error("bad results:", records)
}
if cc2.Total == oldCC2.Total {
t.Error("consumed capacity didn't increase", cc2, oldCC2)
}
// Check & Update
widget2.Msg = "bird"
tx = testDB.WriteTx()
tx.Check(table.Check("UserID", widget1.UserID).Range("Time", widget1.Time).If("Msg = ?", widget1.Msg))
tx.Update(table.Update("UserID", widget2.UserID).Range("Time", widget2.Time).Set("Msg", widget2.Msg))
if err = tx.Run(); err != nil {
t.Error(err)
}
// Delete
tx = testDB.WriteTx()
tx.Delete(table.Delete("UserID", widget1.UserID).Range("Time", widget1.Time).If("Msg = ?", widget1.Msg))
tx.Delete(table.Delete("UserID", widget2.UserID).Range("Time", widget2.Time).If("Msg = ?", widget2.Msg))
if err = tx.Run(); err != nil {
t.Error(err)
}
// zero results
if err = getTx.Run(); err != ErrNotFound {
t.Error("expected ErrNotFound, got:", err)
}
// TransactionCanceledException
tx = testDB.WriteTx()
tx.Put(table.Put(widget{UserID: 69, Time: date1}).If("'Msg' = ?", "should not exist"))
tx.Put(table.Put(widget{UserID: 69, Time: date2}))
tx.Check(table.Check("UserID", 69).Range("Time", date3).IfExists().If("Msg = ?", "don't exist foo"))
err = tx.Run()
if err == nil {
t.Error("expected error")
} else {
if err.(awserr.Error).Code() != "TransactionCanceledException" {
t.Error("unexpected error:", err)
}
}
t.Logf("1: %+v 2: %+v 3: %+v", record1, record2, record3)
t.Logf("All: %+v (len: %d)", records, len(records))
}