-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_update_test.go
120 lines (103 loc) · 2.51 KB
/
redis_update_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
package rds_test
import (
"time"
"github.com/rs/rest-layer/schema/query"
"github.com/rs/rest-layer/resource"
)
func (s *RedisMainTestSuite) TestUpdate() {
bob := &resource.Item{
ID: "upd_id1",
ETag: "asdf",
Payload: map[string]interface{}{
"age": 35,
"birth": time.Now(),
"height": 185.54576,
"name": "Bob",
"male": true,
},
}
err := s.handler.Insert(s.ctx, []*resource.Item{bob})
s.NoError(err)
bobUpdated := &resource.Item{
ID: "upd_id1",
ETag: "asdf2",
Payload: map[string]interface{}{
"age": 77,
"height": 186,
"name": "Боб",
"birth": time.Now(),
"male": true,
},
}
// test no errors on update
err = s.handler.Update(s.ctx, bobUpdated, bob)
s.NoError(err)
// test we can fetch it back
q := &query.Query{
Window: &query.Window{
Offset: 0,
Limit: 100,
},
Predicate: query.Predicate{&query.Equal{Field: "id", Value: "upd_id1"}},
}
res, err := s.handler.Find(s.ctx, q)
s.NoError(err)
s.Len(res.Items, 1)
result := res.Items[0]
// test Bob has new values and unchanged ones
s.Equal("upd_id1", result.ID)
s.Equal("asdf2", result.ETag)
s.Len(result.Payload, 7)
s.Equal(77, result.Payload["age"])
s.Equal(186, result.Payload["height"])
s.Equal("Боб", result.Payload["name"])
s.Equal(true, result.Payload["male"])
s.Equal("upd_id1", result.Payload["id"])
}
func (s *RedisMainTestSuite) TestUpdate_Conflict() {
bob := &resource.Item{
ID: "upd_id1",
ETag: "asdf",
Payload: map[string]interface{}{
"age": 35,
"birth": time.Now(),
"height": 185.54576,
"name": "Bob",
"male": true,
},
}
err := s.handler.Insert(s.ctx, []*resource.Item{bob})
s.NoError(err)
bobUpdated := &resource.Item{
ID: "upd_id1",
ETag: "asdf3",
Payload: map[string]interface{}{
"age": 77,
},
}
bob.ETag = "asdf2"
// test conflict error on update
err = s.handler.Update(s.ctx, bobUpdated, bob)
s.EqualError(err, "Conflict")
// test we can fetch it back
q := &query.Query{
Window: &query.Window{
Offset: 0,
Limit: 100,
},
Predicate: query.Predicate{&query.Equal{Field: "id", Value: "upd_id1"}},
}
res, err := s.handler.Find(s.ctx, q)
s.NoError(err)
s.Len(res.Items, 1)
result := res.Items[0]
// test Bob's data wasn't changed because of a conflict
s.Equal("upd_id1", result.ID)
s.Equal("asdf", result.ETag)
s.Len(result.Payload, 7)
s.Equal(35, result.Payload["age"])
s.Equal(185.54576, result.Payload["height"])
s.Equal("Bob", result.Payload["name"])
s.Equal(true, result.Payload["male"])
s.Equal("upd_id1", result.Payload["id"])
}