This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperations_get_test.go
253 lines (230 loc) · 6.15 KB
/
operations_get_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package bucket
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/volatiletech/null"
)
func TestHandler_Get(t *testing.T) {
wsInsert, id, err := testInsert()
if err != nil {
t.Fatal(err)
}
wsGet := webshop{}
if err := th.Get(context.Background(), "webshop", id, &wsGet); err != nil {
t.Fatal(err)
}
assert.Equal(t, wsInsert, wsGet, "should be equal")
}
func TestHandler_GetNullString(t *testing.T) {
type nullStrTest struct {
Bin int `json:"bin"`
CardBrand string `json:"card_brand"`
IssuingBank string `json:"issuing_bank"`
CardType null.String `json:"card_type"`
}
cardTypeWrite := nullStrTest{
Bin: 50003,
CardBrand: "VISA",
IssuingBank: "",
CardType: null.String{String: "US", Valid: true},
}
_, id, err := th.Insert(context.Background(), "card_type", "", cardTypeWrite, 0)
if err != nil {
t.Error(err)
}
cardTypeRead := nullStrTest{}
if err := th.Get(context.Background(), "card_type", id, &cardTypeRead); err != nil {
t.Error(err)
}
assert.Equal(t, cardTypeWrite, cardTypeRead, "should be equal")
}
func TestHandler_GetNilEmbeddedStruct(t *testing.T) {
wsInsert := generate()
wsInsert.Product = nil
ctx := context.Background()
typ := "webshop"
id := ""
_, id, err := th.Insert(ctx, typ, id, wsInsert, 0)
if err != nil {
t.Error(err)
}
wsGet := &webshop{}
errGet := th.Get(ctx, typ, id, wsGet)
if errGet != nil {
t.Error(errGet)
}
assert.Equal(t, &wsInsert, wsGet, "should be equal")
}
func TestHandler_GetPrimitivePtrNil(t *testing.T) {
a := "a"
type wtyp struct {
Job *string `json:"name,omitempty"`
}
test := wtyp{Job: &a}
_, id, errInsert := th.Insert(context.Background(), "webshop", "", test, 0)
if errInsert != nil {
t.Error("Error")
}
var testGet = wtyp{}
errGet := th.Get(context.Background(), "webshop", id, &testGet)
if errGet != nil {
t.Error("Error")
}
assert.Equal(t, test, testGet, "They should be equal")
}
func TestHandler_GetPrimitivePtr(t *testing.T) {
a := "artist"
type wtyp struct {
Job *string `json:"name,omitempty"`
}
test := wtyp{Job: &a}
_, id, errInsert := th.Insert(context.Background(), "webshop", "", test, 0)
if errInsert != nil {
t.Error("Error")
}
var testGet = wtyp{}
errGet := th.Get(context.Background(), "webshop", id, &testGet)
if errGet != nil {
t.Error("Error")
}
assert.Equal(t, test, testGet, "They should be equal")
}
func TestHandler_GetNonPointerInput(t *testing.T) {
a := "a"
type wtyp struct {
Job *string `json:"name,omitempty"`
}
test := wtyp{Job: &a}
_, id, errInsert := th.Insert(context.Background(), "webshop", "", test, 0)
if errInsert != nil {
t.Error("Error")
}
var testGet = wtyp{}
errGet := th.Get(context.Background(), "webshop", id, &testGet)
if errGet != nil {
t.Error("error")
}
assert.Equal(t, test, testGet, "They should be equal")
}
func TestHandler_GetNonExportedField(t *testing.T) {
a := "helder"
type wtyp struct {
job string
}
testInsert := wtyp{job: a}
_, id, errInsert := th.Insert(context.Background(), "webshop", "", testInsert, 0)
if errInsert != nil {
t.Error("Error")
}
var testGet = wtyp{}
errGet := th.Get(context.Background(), "webshop", id, &testGet)
if errGet != nil {
t.Error("error")
}
assert.NotEqual(t, testInsert, testGet, "They should be not equal")
}
func TestHandler_GetIDNotFoundError(t *testing.T) {
ws := webshop{}
if err := th.Get(context.Background(), "webshop", "123", &ws); err == nil {
t.Error("read with invalid ID")
}
}
func TestHandler_GetInvalidInput(t *testing.T) {
_, id, err := testInsert()
if err != nil {
t.Fatal(err)
}
wsGet := webshop{}
if err := th.Get(context.Background(), "webshop", id, wsGet); err != ErrInputStructPointer {
t.Errorf("error should be %s instead of %s", ErrInputStructPointer, err)
}
}
func TestHandler_GetEmptyRefTagExpectErr(t *testing.T) {
type wsInsert struct {
Token string `json:"token"`
Product *product `json:"product" cb_referenced:"product"`
}
websh := wsInsert{
Token: "",
Product: &product{
Name: "testprod",
Description: "description",
Price: 1221,
CurrencyID: 923,
},
}
type wsGet struct {
Token string `json:"token"`
Product *product `json:"product" cb_referenced:""`
}
ctx := context.Background()
_, id, err := th.Insert(ctx, "webshop", "", websh, 0)
if err != nil {
t.Fatal(err)
}
if err := th.Get(ctx, "webshop", id, &wsGet{}); err != ErrEmptyRefTag {
t.Errorf("error should be %s instead of %s", ErrEmptyRefTag, err)
}
}
func TestHandler_GetAndTouch(t *testing.T) {
webshopInsert, ID, err := testInsert()
if err != nil {
t.Fatal(err)
}
ws := webshop{}
if err := th.GetAndTouch(context.Background(), "webshop", ID, &ws, 10); err != nil {
t.Error("error", err)
}
assert.Equal(t, webshopInsert, ws, "should be equal")
}
func BenchmarkHandler_Get(b *testing.B) {
b.StopTimer()
_, id, err := testInsert()
if err != nil {
b.Fatal(err)
}
wsGet := webshop{}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := th.Get(context.Background(), "webshop", id, &wsGet); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHandler_GetSingle(b *testing.B) {
for i := 0; i < b.N; i++ {
startInsert := time.Now()
_, ID, _ := th.Insert(context.Background(), "webshop", "", generate(), 0)
fmt.Printf("Insert: %vns\tGet: ", time.Since(startInsert).Nanoseconds())
start := time.Now()
_ = th.Get(context.Background(), "webshop", ID, webshop{})
fmt.Printf("%vns\n", time.Since(start).Nanoseconds())
}
}
func BenchmarkHandler_GetEmbedded(b *testing.B) {
b.StopTimer()
_, id, _ := testInsert()
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = th.Get(context.Background(), "webshop", id, &webshop{})
}
}
func BenchmarkHandler_GetPtr(b *testing.B) {
type jobtyp struct {
Job *string `json:"job,omitempty"`
}
j := "helder"
for i := 0; i < b.N; i++ {
job := jobtyp{Job: &j}
startInsert := time.Now()
_, id, _ := th.Insert(context.Background(), "job", "", job, 0)
fmt.Printf("Insert: %vns\tGet: ", time.Since(startInsert).Nanoseconds())
var jobRead jobtyp
start := time.Now()
_ = th.Get(context.Background(), "job", id, &jobRead)
fmt.Printf("%vns\n", time.Since(start).Nanoseconds())
}
}