7
7
"testing"
8
8
"time"
9
9
10
+ "github.com/gofiber/storage/testhelpers/tck"
10
11
"github.com/stretchr/testify/require"
12
+ "github.com/stretchr/testify/suite"
11
13
"github.com/testcontainers/testcontainers-go"
12
14
"github.com/testcontainers/testcontainers-go/modules/mysql"
13
15
"github.com/testcontainers/testcontainers-go/wait"
@@ -22,20 +24,49 @@ const (
22
24
mysqlDatabase string = "fiber"
23
25
)
24
26
27
+ // MySQLStorageTCK is the test suite for the MySQL storage.
28
+ type MySQLStorageTCK struct {}
29
+
30
+ // NewStore is a function that returns a new MySQL storage.
31
+ // It implements the [tck.TCKSuite] interface, allowing the TCK to create a new MySQL storage
32
+ // from the container created by the TCK.
33
+ func (s * MySQLStorageTCK ) NewStore () func (ctx context.Context , tb testing.TB , ctr * mysql.MySQLContainer ) (* Storage , error ) {
34
+ return func (ctx context.Context , tb testing.TB , ctr * mysql.MySQLContainer ) (* Storage , error ) {
35
+ conn , err := ctr .ConnectionString (ctx )
36
+ require .NoError (tb , err )
37
+
38
+ store := New (Config {
39
+ ConnectionURI : conn ,
40
+ Reset : true ,
41
+ })
42
+
43
+ return store , nil
44
+ }
45
+ }
46
+
47
+ // NewContainer is a function that returns a new MySQL container.
48
+ // It implements the [tck.TCKSuite] interface, allowing the TCK to create a new MySQL container
49
+ // for the MySQL storage.
50
+ func (s * MySQLStorageTCK ) NewContainer () func (ctx context.Context , tb testing.TB ) (* mysql.MySQLContainer , error ) {
51
+ return func (ctx context.Context , tb testing.TB ) (* mysql.MySQLContainer , error ) {
52
+ return mustStartMySQL (tb ), nil
53
+ }
54
+ }
55
+
25
56
func newTestStore (t testing.TB ) * Storage {
26
57
t .Helper ()
27
58
28
59
ctx := context .Background ()
29
60
30
- c := mustStartMySQL ( t )
61
+ suite := MySQLStorageTCK {}
31
62
32
- conn , err := c . ConnectionString ( ctx )
63
+ ctr , err := suite . NewContainer ()( ctx , t )
33
64
require .NoError (t , err )
34
65
35
- return New ( Config {
36
- ConnectionURI : conn ,
37
- Reset : true ,
38
- })
66
+ store , err := suite . NewStore ()( ctx , t , ctr )
67
+ require . NoError ( t , err )
68
+
69
+ return store
39
70
}
40
71
41
72
func mustStartMySQL (t testing.TB ) * mysql.MySQLContainer {
@@ -77,222 +108,6 @@ func Test_MYSQL_New(t *testing.T) {
77
108
defer newConfigStore .Close ()
78
109
}
79
110
80
- func Test_MYSQL_Set (t * testing.T ) {
81
- var (
82
- key = "john"
83
- val = []byte ("doe" )
84
- )
85
-
86
- testStore := newTestStore (t )
87
- defer testStore .Close ()
88
-
89
- err := testStore .Set (key , val , 0 )
90
- require .NoError (t , err )
91
- }
92
-
93
- func Test_MYSQL_SetWithContext (t * testing.T ) {
94
- var (
95
- key = "john"
96
- val = []byte ("doe" )
97
- )
98
-
99
- ctx , cancel := context .WithCancel (context .Background ())
100
- cancel ()
101
-
102
- testStore := newTestStore (t )
103
- defer testStore .Close ()
104
-
105
- err := testStore .SetWithContext (ctx , key , val , 0 )
106
- require .ErrorIs (t , err , context .Canceled )
107
- }
108
-
109
- func Test_MYSQL_Set_Override (t * testing.T ) {
110
- var (
111
- key = "john"
112
- val = []byte ("doe" )
113
- )
114
-
115
- testStore := newTestStore (t )
116
- defer testStore .Close ()
117
-
118
- err := testStore .Set (key , val , 0 )
119
- require .NoError (t , err )
120
-
121
- err = testStore .Set (key , val , 0 )
122
- require .NoError (t , err )
123
- }
124
-
125
- func Test_MYSQL_Get (t * testing.T ) {
126
- var (
127
- key = "john"
128
- val = []byte ("doe" )
129
- )
130
-
131
- testStore := newTestStore (t )
132
- defer testStore .Close ()
133
-
134
- err := testStore .Set (key , val , 0 )
135
- require .NoError (t , err )
136
-
137
- result , err := testStore .Get (key )
138
- require .NoError (t , err )
139
- require .Equal (t , val , result )
140
- }
141
-
142
- func Test_MYSQL_GetWithContext (t * testing.T ) {
143
- var (
144
- key = "john"
145
- val = []byte ("doe" )
146
- )
147
-
148
- testStore := newTestStore (t )
149
- defer testStore .Close ()
150
-
151
- err := testStore .Set (key , val , 0 )
152
- require .NoError (t , err )
153
-
154
- ctx , cancel := context .WithCancel (context .Background ())
155
- cancel ()
156
-
157
- result , err := testStore .GetWithContext (ctx , key )
158
- require .ErrorIs (t , err , context .Canceled )
159
- require .Zero (t , len (result ))
160
- }
161
-
162
- func Test_MYSQL_Set_Expiration (t * testing.T ) {
163
- var (
164
- key = "john"
165
- val = []byte ("doe" )
166
- exp = 1 * time .Second
167
- )
168
-
169
- testStore := newTestStore (t )
170
- defer testStore .Close ()
171
-
172
- err := testStore .Set (key , val , exp )
173
- require .NoError (t , err )
174
-
175
- time .Sleep (1100 * time .Millisecond )
176
-
177
- result , err := testStore .Get (key )
178
- require .NoError (t , err )
179
- require .Zero (t , len (result ), "Key should have expired" )
180
- }
181
-
182
- func Test_MYSQL_Get_Expired (t * testing.T ) {
183
- key := "john"
184
-
185
- testStore := newTestStore (t )
186
- defer testStore .Close ()
187
-
188
- result , err := testStore .Get (key )
189
- require .NoError (t , err )
190
- require .Zero (t , len (result ))
191
- }
192
-
193
- func Test_MYSQL_Get_NotExist (t * testing.T ) {
194
- testStore := newTestStore (t )
195
- defer testStore .Close ()
196
-
197
- result , err := testStore .Get ("notexist" )
198
- require .NoError (t , err )
199
- require .Zero (t , len (result ))
200
- }
201
-
202
- func Test_MYSQL_Delete (t * testing.T ) {
203
- var (
204
- key = "john"
205
- val = []byte ("doe" )
206
- )
207
-
208
- testStore := newTestStore (t )
209
- defer testStore .Close ()
210
-
211
- err := testStore .Set (key , val , 0 )
212
- require .NoError (t , err )
213
-
214
- err = testStore .Delete (key )
215
- require .NoError (t , err )
216
-
217
- result , err := testStore .Get (key )
218
- require .NoError (t , err )
219
- require .Zero (t , len (result ))
220
- }
221
-
222
- func Test_MYSQL_DeleteWithContext (t * testing.T ) {
223
- var (
224
- key = "john"
225
- val = []byte ("doe" )
226
- )
227
-
228
- testStore := newTestStore (t )
229
- defer testStore .Close ()
230
-
231
- err := testStore .Set (key , val , 0 )
232
- require .NoError (t , err )
233
-
234
- ctx , cancel := context .WithCancel (context .Background ())
235
- cancel ()
236
-
237
- err = testStore .DeleteWithContext (ctx , key )
238
- require .ErrorIs (t , err , context .Canceled )
239
-
240
- result , err := testStore .Get (key )
241
- require .NoError (t , err )
242
- require .Equal (t , val , result )
243
- }
244
-
245
- func Test_MYSQL_Reset (t * testing.T ) {
246
- val := []byte ("doe" )
247
-
248
- testStore := newTestStore (t )
249
- defer testStore .Close ()
250
-
251
- err := testStore .Set ("john1" , val , 0 )
252
- require .NoError (t , err )
253
-
254
- err = testStore .Set ("john2" , val , 0 )
255
- require .NoError (t , err )
256
-
257
- err = testStore .Reset ()
258
- require .NoError (t , err )
259
-
260
- result , err := testStore .Get ("john1" )
261
- require .NoError (t , err )
262
- require .Zero (t , len (result ))
263
-
264
- result , err = testStore .Get ("john2" )
265
- require .NoError (t , err )
266
- require .Zero (t , len (result ))
267
- }
268
-
269
- func Test_MYSQL_ResetWithContext (t * testing.T ) {
270
- val := []byte ("doe" )
271
-
272
- testStore := newTestStore (t )
273
- defer testStore .Close ()
274
-
275
- err := testStore .Set ("john1" , val , 0 )
276
- require .NoError (t , err )
277
-
278
- err = testStore .Set ("john2" , val , 0 )
279
- require .NoError (t , err )
280
-
281
- ctx , cancel := context .WithCancel (context .Background ())
282
- cancel ()
283
-
284
- err = testStore .ResetWithContext (ctx )
285
- require .ErrorIs (t , err , context .Canceled )
286
-
287
- result , err := testStore .Get ("john1" )
288
- require .NoError (t , err )
289
- require .Equal (t , val , result )
290
-
291
- result , err = testStore .Get ("john2" )
292
- require .NoError (t , err )
293
- require .Equal (t , val , result )
294
- }
295
-
296
111
func Test_MYSQL_GC (t * testing.T ) {
297
112
testVal := []byte ("doe" )
298
113
@@ -332,16 +147,12 @@ func Test_MYSQL_Non_UTF8(t *testing.T) {
332
147
require .Equal (t , val , result )
333
148
}
334
149
335
- func Test_MYSQL_Close (t * testing.T ) {
336
- testStore := newTestStore (t )
337
- require .NoError (t , testStore .Close ())
338
- }
339
-
340
- func Test_MYSQL_Conn (t * testing.T ) {
341
- testStore := newTestStore (t )
342
- defer testStore .Close ()
150
+ func TestMySQLStorageTCK (t * testing.T ) {
151
+ // The TCK needs the concrete type of the storage and the driver type returned by the Conn method.
152
+ s , err := tck .New [* Storage , * sql.DB ](context .Background (), t , & MySQLStorageTCK {}, tck .PerTest ())
153
+ require .NoError (t , err )
343
154
344
- require . True (t , testStore . Conn () != nil )
155
+ suite . Run (t , s )
345
156
}
346
157
347
158
func Benchmark_MYSQL_Set (b * testing.B ) {
0 commit comments