@@ -13,7 +13,7 @@ import (
13
13
)
14
14
15
15
func TestFromModelFeatureFlag (t * testing.T ) {
16
- flagId , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174000" )
16
+ flagID , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174000" )
17
17
18
18
tests := []struct {
19
19
name string
@@ -24,7 +24,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
24
24
{
25
25
name : "should convert model.FeatureFlag to dbmodel.FeatureFlag" ,
26
26
mff : model.FeatureFlag {
27
- ID : flagId .String (),
27
+ ID : flagID .String (),
28
28
Name : "my-flag" ,
29
29
Description : testutils .String ("my flag description" ),
30
30
Variations : & map [string ]interface {}{
@@ -49,7 +49,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
49
49
},
50
50
wantErr : assert .NoError ,
51
51
want : dbmodel.FeatureFlag {
52
- ID : flagId ,
52
+ ID : flagID ,
53
53
Name : "my-flag" ,
54
54
Description : testutils .String ("my flag description" ),
55
55
Variations : dbmodel .JSONB (map [string ]interface {}{
@@ -70,7 +70,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
70
70
}, {
71
71
name : "should convert model.FeatureFlag to dbmodel.FeatureFlag without metadata" ,
72
72
mff : model.FeatureFlag {
73
- ID : flagId .String (),
73
+ ID : flagID .String (),
74
74
Name : "my-flag" ,
75
75
Description : testutils .String ("my flag description" ),
76
76
Variations : & map [string ]interface {}{
@@ -92,7 +92,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
92
92
},
93
93
wantErr : assert .NoError ,
94
94
want : dbmodel.FeatureFlag {
95
- ID : flagId ,
95
+ ID : flagID ,
96
96
Name : "my-flag" ,
97
97
Description : testutils .String ("my flag description" ),
98
98
Variations : dbmodel .JSONB (map [string ]interface {}{
@@ -111,7 +111,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
111
111
}, {
112
112
name : "should convert model.FeatureFlag to dbmodel.FeatureFlag without variation" ,
113
113
mff : model.FeatureFlag {
114
- ID : flagId .String (),
114
+ ID : flagID .String (),
115
115
Name : "my-flag" ,
116
116
Description : testutils .String ("my flag description" ),
117
117
VariationType : "boolean" ,
@@ -129,7 +129,7 @@ func TestFromModelFeatureFlag(t *testing.T) {
129
129
},
130
130
wantErr : assert .NoError ,
131
131
want : dbmodel.FeatureFlag {
132
- ID : flagId ,
132
+ ID : flagID ,
133
133
Name : "my-flag" ,
134
134
Description : testutils .String ("my flag description" ),
135
135
Variations : nil ,
@@ -196,3 +196,132 @@ func TestFromModelFeatureFlag(t *testing.T) {
196
196
})
197
197
}
198
198
}
199
+
200
+ func TestToModelFeatureFlag (t * testing.T ) {
201
+ flagID , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174000" )
202
+ ruleIdDefault , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174111" )
203
+ ruleId1 , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174333" )
204
+ ruleId2 , _ := uuid .Parse ("123e4567-e89b-12d3-a456-426614174222" )
205
+
206
+ tests := []struct {
207
+ name string
208
+ dbFF dbmodel.FeatureFlag
209
+ dbRule []dbmodel.Rule
210
+ want model.FeatureFlag
211
+ }{
212
+ {
213
+ name : "should convert model.FeatureFlag to dbmodel.FeatureFlag" ,
214
+ dbFF : dbmodel.FeatureFlag {
215
+ ID : flagID ,
216
+ Name : "my-flag" ,
217
+ Description : testutils .String ("my flag description" ),
218
+ Variations : dbmodel .JSONB (map [string ]interface {}{
219
+ "A" : "a" ,
220
+ "B" : "b" ,
221
+ }),
222
+ Type : model .FlagTypeString ,
223
+ BucketingKey : testutils .String ("teamID" ),
224
+ Metadata : dbmodel .JSONB (map [string ]interface {}{
225
+ "key" : "value" ,
226
+ }),
227
+ TrackEvents : testutils .Bool (true ),
228
+ Disable : testutils .Bool (false ),
229
+ Version : testutils .String ("1.0.0" ),
230
+ CreatedDate : time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
231
+ LastUpdatedDate : time .Date (2022 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
232
+ },
233
+ dbRule : []dbmodel.Rule {
234
+ {
235
+ ID : ruleId1 ,
236
+ Name : "Rule 1" ,
237
+ FeatureFlagID : flagID ,
238
+ Disable : false ,
239
+ Percentages : dbmodel .JSONB (map [string ]interface {}{"A" : float64 (50 ), "B" : float64 (50 )}),
240
+ IsDefault : false ,
241
+ Query : `targetingKey eq "foo"` ,
242
+ OrderIndex : 6 ,
243
+ },
244
+ {
245
+ ID : ruleId2 ,
246
+ Name : "rule 2" ,
247
+ FeatureFlagID : flagID ,
248
+ Query : `targetingKey eq "bar"` ,
249
+ Disable : true ,
250
+ OrderIndex : 10 ,
251
+ IsDefault : false ,
252
+ ProgressiveRolloutInitialVariation : testutils .String ("A" ),
253
+ ProgressiveRolloutEndVariation : testutils .String ("B" ),
254
+ ProgressiveRolloutInitialPercentage : testutils .Float64 (0 ),
255
+ ProgressiveRolloutEndPercentage : testutils .Float64 (100 ),
256
+ ProgressiveRolloutStartDate : testutils .Time (time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
257
+ ProgressiveRolloutEndDate : testutils .Time (time .Date (2022 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
258
+ },
259
+ {
260
+ ID : ruleIdDefault ,
261
+ Name : "defaultRule" ,
262
+ FeatureFlagID : flagID ,
263
+ Disable : false ,
264
+ VariationResult : testutils .String ("A" ),
265
+ IsDefault : true ,
266
+ },
267
+ },
268
+ want : model.FeatureFlag {
269
+ ID : flagID .String (),
270
+ Name : "my-flag" ,
271
+ Description : testutils .String ("my flag description" ),
272
+ Variations : & map [string ]interface {}{
273
+ "A" : "a" ,
274
+ "B" : "b" ,
275
+ },
276
+ VariationType : model .FlagTypeString ,
277
+ BucketingKey : testutils .String ("teamID" ),
278
+ Metadata : & map [string ]interface {}{
279
+ "key" : "value" ,
280
+ },
281
+ TrackEvents : testutils .Bool (true ),
282
+ Disable : testutils .Bool (false ),
283
+ Version : testutils .String ("1.0.0" ),
284
+ CreatedDate : time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
285
+ LastUpdatedDate : time .Date (2022 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
286
+ DefaultRule : & model.Rule {
287
+ ID : ruleIdDefault .String (),
288
+ Name : "defaultRule" ,
289
+ VariationResult : testutils .String ("A" ),
290
+ },
291
+ Rules : & []model.Rule {
292
+ {
293
+ ID : ruleId1 .String (),
294
+ Name : "Rule 1" ,
295
+ Query : `targetingKey eq "foo"` ,
296
+ Disable : false ,
297
+ Percentages : & map [string ]float64 {"A" : 50 , "B" : 50 },
298
+ },
299
+ {
300
+ ID : ruleId2 .String (),
301
+ Name : "rule 2" ,
302
+ Query : `targetingKey eq "bar"` ,
303
+ Disable : true ,
304
+ ProgressiveRollout : & model.ProgressiveRollout {
305
+ Initial : & model.ProgressiveRolloutStep {
306
+ Variation : testutils .String ("A" ),
307
+ Percentage : testutils .Float64 (0 ),
308
+ Date : testutils .Time (time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
309
+ },
310
+ End : & model.ProgressiveRolloutStep {
311
+ Variation : testutils .String ("B" ),
312
+ Percentage : testutils .Float64 (100 ),
313
+ Date : testutils .Time (time .Date (2022 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )),
314
+ },
315
+ },
316
+ },
317
+ },
318
+ },
319
+ },
320
+ }
321
+ for _ , tt := range tests {
322
+ t .Run (tt .name , func (t * testing.T ) {
323
+ got := tt .dbFF .ToModelFeatureFlag (tt .dbRule )
324
+ assert .Equalf (t , tt .want , got , "FromModelFeatureFlag" )
325
+ })
326
+ }
327
+ }
0 commit comments