-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.go
175 lines (144 loc) · 4.32 KB
/
tests.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
// Package tests implements a test suite for store drivers.
package tests
import (
"reflect"
"sync/atomic"
"testing"
"github.com/Unbabel/replicant/store"
"github.com/Unbabel/replicant/transaction"
)
// Setup function type for creating a store for tests
type Setup func(t *testing.T) (s store.Store)
// Destroy function type for cleaning up after tests
type Destroy func(t *testing.T, s store.Store)
// Run store test suite
func Run(t *testing.T, s Setup, d Destroy) {
t.Run("StoreHas", StoreHas(t, s, d))
t.Run("StoreSetGet", StoreSetGet(t, s, d))
t.Run("StoreDelete", StoreDelete(t, s, d))
t.Run("StoreIter", StoreIter(t, s, d))
}
// StoreHas test
func StoreHas(t *testing.T, setup Setup, destroy Destroy) func(t *testing.T) {
return func(t *testing.T) {
s := setup(t)
defer destroy(t, s)
tests := testCases{
{config: transaction.Config{Name: "txn1"}, wantErr: false},
{config: transaction.Config{Name: "txn2"}, wantErr: true},
{config: transaction.Config{Name: "txn3"}, wantErr: false},
{config: transaction.Config{Name: "txn4"}, wantErr: true},
{config: transaction.Config{Name: "txn5"}, wantErr: true},
}
for _, tt := range tests {
if !tt.wantErr {
s.Set(tt.config.Name, tt.config)
}
}
for _, tt := range tests {
t.Run(tt.config.Name, func(t *testing.T) {
gotExists, err := s.Has(tt.config.Name)
if err != nil {
t.Fatal(err)
}
if gotExists == tt.wantErr {
t.Errorf("Store.Has() = %v, want %v", gotExists, tt.wantErr)
}
})
}
}
}
// StoreSetGet test
func StoreSetGet(t *testing.T, setup Setup, destroy Destroy) func(t *testing.T) {
return func(t *testing.T) {
s := setup(t)
defer destroy(t, s)
tests := testCases{
{config: transaction.Config{Name: "txn1"}, wantErr: false},
{config: transaction.Config{Name: "txn2"}, wantErr: true},
{config: transaction.Config{Name: "txn3"}, wantErr: false},
{config: transaction.Config{Name: "txn4"}, wantErr: true},
{config: transaction.Config{Name: "txn5"}, wantErr: true},
}
for _, tt := range tests {
if !tt.wantErr {
s.Set(tt.config.Name, tt.config)
}
}
for _, tt := range tests {
t.Run(tt.config.Name, func(t *testing.T) {
gotConfig, err := s.Get(tt.config.Name)
if (err != nil) != tt.wantErr {
t.Errorf("Store.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(gotConfig, tt.config) {
t.Errorf("Store.Get() = %v, want %v", gotConfig, tt.config)
}
})
}
}
}
// StoreDelete type
func StoreDelete(t *testing.T, setup Setup, destroy Destroy) func(t *testing.T) {
return func(t *testing.T) {
s := setup(t)
defer destroy(t, s)
tests := testCases{
{config: transaction.Config{Name: "txn1"}, wantErr: false},
{config: transaction.Config{Name: "txn2"}, wantErr: true},
{config: transaction.Config{Name: "txn3"}, wantErr: false},
{config: transaction.Config{Name: "txn4"}, wantErr: true},
{config: transaction.Config{Name: "txn5"}, wantErr: true},
}
for _, tt := range tests {
if !tt.wantErr {
s.Set(tt.config.Name, tt.config)
}
}
for _, tt := range tests {
t.Run(tt.config.Name, func(t *testing.T) {
if err := s.Delete(tt.config.Name); (err != nil) != tt.wantErr {
t.Errorf("Store.Delete() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
}
// StoreIter type
func StoreIter(t *testing.T, setup Setup, destroy Destroy) func(t *testing.T) {
return func(t *testing.T) {
s := setup(t)
defer destroy(t, s)
tests := testCases{
{config: transaction.Config{Name: "txn1"}, wantErr: false},
{config: transaction.Config{Name: "txn2"}, wantErr: false},
{config: transaction.Config{Name: "txn3"}, wantErr: false},
{config: transaction.Config{Name: "txn4"}, wantErr: false},
{config: transaction.Config{Name: "txn5"}, wantErr: false},
}
for _, tt := range tests {
if !tt.wantErr {
s.Set(tt.config.Name, tt.config)
}
}
var counter int32
cb := func(n string, c transaction.Config) bool {
if (n != "") && (n == c.Name) {
atomic.AddInt32(&counter, 1)
t.Log(n)
}
return true
}
if err := s.Iter(cb); err != nil {
t.Errorf("Store.Iter() error = %v", err)
}
if len(tests) != int(counter) {
t.Errorf("expected %d iterations, got %d", len(tests), counter)
}
}
}
type testCases []struct {
config transaction.Config
wantErr bool
}