-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_inserter_test.go
229 lines (213 loc) · 5.8 KB
/
tx_inserter_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
package dal
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"strconv"
"strings"
"testing"
)
//type foo struct {
// title string
//}
//
//func (foo foo) Kind() string {
// return "foo"
//}
//
//func (foo foo) Validate() error {
// if foo.title == "" {
// return fmt.Errorf("missing required field: title")
// }
// return nil
//}
//type inserterMock struct {
//}
//
//func (v inserterMock) Insert(ctx context.Context, record Record, opts ...InsertOption) error {
// options := NewInsertOptions(opts...)
// if idGenerator := options.IDGenerator(); idGenerator != nil {
// if err := idGenerator(ctx, record); err != nil {
// return err
// }
// }
// return nil
//}
type insertArgs struct {
ctx context.Context
record Record
//generateID IDGenerator
attempts int
}
func TestInsertWithRandomID(t *testing.T) {
for _, tt := range []struct {
name string
args insertArgs
//
generatorErr error
generatorErrOnAttemptNo int
expectedErrTexts []string
existsFalseOnAttemptNo int
existsErrorOnAttemptNo int
existsError error
}{
{
name: "should_pass_on_first_attempt",
existsFalseOnAttemptNo: 1,
args: insertArgs{
ctx: context.Background(),
record: NewRecordWithData(&Key{collection: "test_kind"}, new(map[string]any)),
attempts: 1,
},
},
{
name: "exists_error_on_first_attempt",
existsErrorOnAttemptNo: 1,
existsError: errors.New("test exists error"),
existsFalseOnAttemptNo: 1,
args: insertArgs{
ctx: context.Background(),
record: NewRecordWithData(&Key{collection: "test_kind"}, new(map[string]any)),
attempts: 1,
},
},
{
name: "exceeds_max_generates_count",
existsFalseOnAttemptNo: 7,
args: insertArgs{
ctx: context.Background(),
record: NewRecordWithData(&Key{collection: "test_kind"}, new(map[string]any)),
attempts: 5,
},
generatorErr: ErrExceedsMaxNumberOfAttempts,
expectedErrTexts: []string{ErrExceedsMaxNumberOfAttempts.Error(), "5"},
},
{
name: "generator_err_on_first_attempt",
generatorErrOnAttemptNo: 1,
generatorErr: errors.New("test generator intentional error"),
args: insertArgs{
ctx: context.Background(),
record: NewRecordWithData(&Key{collection: "test_kind"}, new(map[string]any)),
attempts: 5,
},
},
} {
t.Run(tt.name, func(t *testing.T) {
attempt := 0
var generateID = func(ctx context.Context, record Record) error {
attempt++
if tt.generatorErrOnAttemptNo == attempt {
return tt.generatorErr
}
record.Key().ID = strconv.Itoa(attempt)
return nil
}
exists := func(key *Key) error {
if tt.existsErrorOnAttemptNo == attempt {
return tt.existsError
}
if attempt < tt.existsFalseOnAttemptNo {
return nil
}
return ErrRecordNotFound
}
insertsCount := 0
insert := func(r Record) error {
insertsCount++
return nil
}
args := tt.args
err := InsertWithRandomID(args.ctx, args.record, generateID, 5, exists, insert)
if err != nil {
if tt.generatorErr != nil && !errors.Is(err, tt.generatorErr) {
t.Errorf("expected error: %v, actual: %v", tt.generatorErr, err)
}
if tt.existsError != nil && !errors.Is(err, tt.existsError) {
t.Errorf("expected error: %v, actual: %v", tt.existsError, err)
}
if len(tt.expectedErrTexts) > 0 {
for _, expectedErrText := range tt.expectedErrTexts {
if !strings.Contains(err.Error(), expectedErrText) {
t.Errorf("expected error text to contain: %v, actual: %v", expectedErrText, err.Error())
}
}
}
assert.Nil(t, args.record.Key().ID)
return
}
assert.NotNil(t, args.record.Key().ID)
assert.Equal(t, 1, insertsCount)
if attempt != tt.existsFalseOnAttemptNo {
t.Errorf("id generator expected to be called %d times, actual: %d", tt.existsFalseOnAttemptNo, attempt)
}
})
}
}
func TestInsertOptions_IDGenerator(t *testing.T) {
err := errors.New("test error")
idGenerator := func(ctx context.Context, record Record) error {
return err
}
io := insertOptions{
idGenerator: idGenerator,
}
assert.Equal(t, err, io.IDGenerator()(context.Background(), nil))
}
func TestNewInsertOptions(t *testing.T) {
called := false
o := func(options *insertOptions) {
called = true
}
io := NewInsertOptions(o)
assert.NotNil(t, io)
assert.True(t, called)
}
func TestWithRandomStringID(t *testing.T) {
const length = 10
key, err := NewKeyWithOptions("c1", WithRandomStringID(RandomLength(length)))
assert.Nil(t, err)
assert.NotNil(t, key)
id := key.ID.(string)
assert.NotEqual(t, "", id)
assert.Equal(t, length, len(id))
}
func TestWithPrefix(t *testing.T) {
key, err := NewKeyWithOptions("c1", WithRandomStringID(Prefix("prefix_")))
assert.Nil(t, err)
assert.NotNil(t, key)
assert.True(t, strings.HasPrefix(key.ID.(string), "prefix_"))
}
func TestWithIDGenerator(t *testing.T) {
for _, tt := range []struct {
name string
key *Key
id string
shouldPanic bool
}{
{name: "nil_id", shouldPanic: false, id: "id1", key: &Key{ID: nil, collection: "c1"}},
{name: "nil_generator", shouldPanic: true, id: "", key: &Key{ID: "id1", collection: "c1"}},
{name: "not_nil_id", shouldPanic: true, id: "id2", key: &Key{ID: "id1", collection: "c1"}},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldPanic {
defer func() {
if r := recover(); r == nil {
t.Errorf("panic expected")
}
}()
}
ctx := context.Background()
var idGenerator IDGenerator
if tt.id != "" {
idGenerator = func(ctx context.Context, record Record) error {
record.Key().ID = tt.id
return nil
}
}
err := WithIDGenerator(ctx, idGenerator)(tt.key)
assert.Nil(t, err)
assert.NotNil(t, tt.key.ID)
})
}
}