forked from open-feature/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
1459 lines (1272 loc) · 45.8 KB
/
client_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package openfeature
import (
"context"
"errors"
"math"
"reflect"
"testing"
"time"
"github.com/golang/mock/gomock"
)
type clientMocks struct {
clientHandlerAPI *MockclientEvent
evaluationAPI *MockevaluationImpl
providerAPI *MockFeatureProvider
}
func hydratedMocksForClientTests(t *testing.T, expectedEvaluations int) clientMocks {
ctrl := gomock.NewController(t)
mockClientApi := NewMockclientEvent(ctrl)
mockEvaluationApi := NewMockevaluationImpl(ctrl)
mockProvider := NewMockFeatureProvider(ctrl)
mockClientApi.EXPECT().State(gomock.Any()).AnyTimes().Return(ReadyState)
mockProvider.EXPECT().Metadata().AnyTimes()
mockProvider.EXPECT().Hooks().AnyTimes()
mockEvaluationApi.EXPECT().ForEvaluation(gomock.Any()).Times(expectedEvaluations).DoAndReturn(func(_ string) (*MockFeatureProvider, []Hook, EvaluationContext) {
return mockProvider, nil, EvaluationContext{}
})
return clientMocks{
clientHandlerAPI: mockClientApi,
evaluationAPI: mockEvaluationApi,
providerAPI: mockProvider,
}
}
// The client MUST provide a method to add `hooks` which accepts one or more API-conformant `hooks`,
// and appends them to the collection of any previously added hooks.
// When new hooks are added, previously added hooks are not removed.
func TestRequirement_1_2_1(t *testing.T) {
defer t.Cleanup(initSingleton)
ctrl := gomock.NewController(t)
mockHook := NewMockHook(ctrl)
client := NewClient("test-client")
client.AddHooks(mockHook)
client.AddHooks(mockHook, mockHook)
if len(client.hooks) != 3 {
t.Error("func client.AddHooks didn't append the list of hooks to the client's existing collection of hooks")
}
}
// The client interface MUST define a `metadata` member or accessor,
// containing an immutable `domain` field or accessor of type string,
// which corresponds to the `domain` value supplied during client creation.
func TestRequirement_1_2_2(t *testing.T) {
defer t.Cleanup(initSingleton)
clientName := "test-client"
client := NewClient(clientName)
if client.Metadata().Domain() != clientName {
t.Errorf("client domain not initiated as expected, got %s, want %s", client.Metadata().Domain(), clientName)
}
}
// TestRequirements_1_3 tests all the 1.3.* requirements by asserting that the returned client matches the interface
// defined by the 1.3.* requirements
//
// Requirement_1_3_1
// The `client` MUST provide methods for typed flag evaluation, including boolean, numeric, string,
// and structure, with parameters `flag key` (string, required), `default value` (boolean | number | string | structure, required),
// `evaluation context` (optional), and `evaluation options` (optional), which returns the flag value.
//
// Requirement_1_3_2_1
// The client SHOULD provide functions for floating-point numbers and integers, consistent with language idioms.
//
// Requirement_1_3_3
// The `client` SHOULD guarantee the returned value of any typed flag evaluation method is of the expected type.
// If the value returned by the underlying provider implementation does not match the expected type,
// it's to be considered abnormal execution, and the supplied `default value` should be returned.
func TestRequirements_1_3(t *testing.T) {
defer t.Cleanup(initSingleton)
client := NewClient("test-client")
type requirements interface {
BooleanValue(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (bool, error)
StringValue(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (string, error)
FloatValue(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (float64, error)
IntValue(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (int64, error)
ObjectValue(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (interface{}, error)
}
var clientI interface{} = client
if _, ok := clientI.(requirements); !ok {
t.Error("client returned by NewClient doesn't implement the 1.3.* requirements interface")
}
}
// The `client` MUST provide methods for detailed flag value evaluation with parameters `flag key` (string, required),
// `default value` (boolean | number | string | structure, required), `evaluation context` (optional),
// and `evaluation options` (optional), which returns an `evaluation details` structure.
func TestRequirement_1_4_1(t *testing.T) {
defer t.Cleanup(initSingleton)
client := NewClient("test-client")
type requirements interface {
BooleanValueDetails(ctx context.Context, flag string, defaultValue bool, evalCtx EvaluationContext, options ...Option) (BooleanEvaluationDetails, error)
StringValueDetails(ctx context.Context, flag string, defaultValue string, evalCtx EvaluationContext, options ...Option) (StringEvaluationDetails, error)
FloatValueDetails(ctx context.Context, flag string, defaultValue float64, evalCtx EvaluationContext, options ...Option) (FloatEvaluationDetails, error)
IntValueDetails(ctx context.Context, flag string, defaultValue int64, evalCtx EvaluationContext, options ...Option) (IntEvaluationDetails, error)
ObjectValueDetails(ctx context.Context, flag string, defaultValue interface{}, evalCtx EvaluationContext, options ...Option) (InterfaceEvaluationDetails, error)
}
var clientI interface{} = client
if _, ok := clientI.(requirements); !ok {
t.Error("client returned by NewClient doesn't implement the 1.4.1 requirements interface")
}
}
const (
booleanValue = true
stringValue = "str"
intValue = 10
floatValue = 0.1
booleanVariant = "boolean"
stringVariant = "string"
intVariant = "ten"
floatVariant = "tenth"
objectVariant = "object"
testReason = "TEST_REASON"
incorrectValue = "Incorrect value returned!"
incorrectVariant = "Incorrect variant returned!"
incorrectReason = "Incorrect reason returned!"
)
var objectValue = map[string]interface{}{"foo": 1, "bar": true, "baz": "buz"}
// Requirement_1_4_2
// The `evaluation details` structure's `value` field MUST contain the evaluated flag value.
// Requirement_1_4_5
// In cases of normal execution, the `evaluation details` structure's `variant` field MUST
// contain the value of the `variant` field in the `flag resolution` structure returned
// by the configured `provider`, if the field is set.
// Requirement_1_4_6
// In cases of normal execution, the `evaluation details` structure's `reason` field MUST
// contain the value of the `reason` field in the `flag resolution` structure returned
// by the configured `provider`, if the field is set.
func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {
defer t.Cleanup(initSingleton)
flagKey := "foo"
t.Run("BooleanValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: booleanValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: booleanVariant,
Reason: testReason,
},
})
evDetails, err := client.BooleanValueDetails(context.Background(), flagKey, false, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.Value != booleanValue {
t.Error(err)
}
})
t.Run("StringValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().StringEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(StringResolutionDetail{
Value: stringValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: stringVariant,
Reason: testReason,
},
})
evDetails, err := client.StringValueDetails(context.Background(), flagKey, "", EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.Value != stringValue {
t.Error(incorrectValue)
}
if evDetails.Variant != stringVariant {
t.Error(incorrectVariant)
}
if evDetails.Reason != testReason {
t.Error(incorrectReason)
}
})
t.Run("FloatValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().FloatEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(FloatResolutionDetail{
Value: floatValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: floatVariant,
Reason: testReason,
},
})
evDetails, err := client.FloatValueDetails(context.Background(), flagKey, 0, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.Value != floatValue {
t.Error(incorrectValue)
}
if evDetails.Variant != floatVariant {
t.Error(incorrectVariant)
}
if evDetails.Reason != testReason {
t.Error(incorrectReason)
}
})
t.Run("IntValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().IntEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(IntResolutionDetail{
Value: intValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: intVariant,
Reason: testReason,
},
})
evDetails, err := client.IntValueDetails(context.Background(), flagKey, 0, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.Value != intValue {
t.Error(incorrectValue)
}
if evDetails.Variant != intVariant {
t.Error(incorrectVariant)
}
if evDetails.Reason != testReason {
t.Error(incorrectReason)
}
})
t.Run("ObjectValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().ObjectEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(InterfaceResolutionDetail{
Value: objectValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: objectVariant,
Reason: testReason,
},
})
evDetails, err := client.ObjectValueDetails(context.Background(), flagKey, nil, EvaluationContext{})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(evDetails.Value, objectValue) {
t.Error(incorrectValue)
}
if evDetails.Variant != objectVariant {
t.Error(incorrectVariant)
}
if evDetails.Reason != testReason {
t.Error(incorrectReason)
}
})
}
// TODO Requirement_1_4_3 once upgraded Go to 1.18 for generics
// The `evaluation details` structure's `flag key` field MUST contain the `flag key`
// argument passed to the detailed flag evaluation method.
func TestRequirement_1_4_4(t *testing.T) {
defer t.Cleanup(initSingleton)
flagKey := "foo"
t.Run("BooleanValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: booleanValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: booleanVariant,
Reason: testReason,
},
})
evDetails, err := client.BooleanValueDetails(context.Background(), flagKey, true, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.FlagKey != flagKey {
t.Errorf(
"flag key isn't as expected in EvaluationDetail, got %s, expected %s",
evDetails.FlagKey, flagKey,
)
}
})
t.Run("StringValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().StringEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(StringResolutionDetail{
Value: stringValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: stringVariant,
Reason: testReason,
},
})
evDetails, err := client.StringValueDetails(context.Background(), flagKey, "", EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.FlagKey != flagKey {
t.Errorf(
"flag key isn't as expected in EvaluationDetail, got %s, expected %s",
evDetails.FlagKey, flagKey,
)
}
})
t.Run("FloatValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().FloatEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(FloatResolutionDetail{
Value: floatValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: floatVariant,
Reason: testReason,
},
})
evDetails, err := client.FloatValueDetails(context.Background(), flagKey, 1, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.FlagKey != flagKey {
t.Errorf(
"flag key isn't as expected in EvaluationDetail, got %s, expected %s",
evDetails.FlagKey, flagKey,
)
}
})
t.Run("IntValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().IntEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(IntResolutionDetail{
Value: intValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: intVariant,
Reason: testReason,
},
})
evDetails, err := client.IntValueDetails(context.Background(), flagKey, 1, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.FlagKey != flagKey {
t.Errorf(
"flag key isn't as expected in EvaluationDetail, got %s, expected %s",
evDetails.FlagKey, flagKey,
)
}
})
t.Run("ObjectValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().ObjectEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(InterfaceResolutionDetail{
Value: objectValue,
ProviderResolutionDetail: ProviderResolutionDetail{
Variant: objectVariant,
Reason: testReason,
},
})
evDetails, err := client.ObjectValueDetails(context.Background(), flagKey, 1, EvaluationContext{})
if err != nil {
t.Error(err)
}
if evDetails.FlagKey != flagKey {
t.Errorf(
"flag key isn't as expected in EvaluationDetail, got %s, expected %s",
evDetails.FlagKey, flagKey,
)
}
})
}
// In cases of abnormal execution, the `evaluation details` structure's
// `error code` field MUST contain an `error code`.
func TestRequirement_1_4_7(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: false,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
})
res, err := client.evaluate(
context.Background(), "foo", Boolean, true, EvaluationContext{}, EvaluationOptions{},
)
if err == nil {
t.Error("expected err, got nil")
}
expectedErrorCode := GeneralCode
if res.ErrorCode != expectedErrorCode {
t.Errorf("expected error code to be '%s', got '%s'", expectedErrorCode, res.ErrorCode)
}
}
// In cases of abnormal execution (network failure, unhandled error, etc) the `reason` field
// in the `evaluation details` SHOULD indicate an error.
func TestRequirement_1_4_8(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: false,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
})
res, err := client.evaluate(
context.Background(), "foo", Boolean, true, EvaluationContext{}, EvaluationOptions{},
)
if err == nil {
t.Error("expected err, got nil")
}
expectedReason := ErrorReason
if res.Reason != expectedReason {
t.Errorf("expected reason to be '%s', got '%s'", expectedReason, res.Reason)
}
}
// Methods, functions, or operations on the client MUST NOT throw exceptions, or otherwise abnormally terminate.
// Flag evaluation calls must always return the `default value` in the event of abnormal execution.
// Exceptions include functions or methods for the purposes for configuration or setup.
//
// This test asserts that the flag evaluation calls return the default value in the event of abnormal execution.
// The MUST NOT abnormally terminate clause of this requirement is satisfied by the error included in the return
// signatures, as is idiomatic in Go. Errors aren't fatal, the operations won't terminate as a result of an error.
func TestRequirement_1_4_9(t *testing.T) {
flagKey := "flag-key"
evalCtx := EvaluationContext{}
flatCtx := flattenContext(evalCtx)
t.Run("Boolean", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
defaultValue := true
mocks.providerAPI.EXPECT().BooleanEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(BoolResolutionDetail{
Value: false,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
}).Times(2)
value, err := client.BooleanValue(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected BooleanValue to return an error, got nil")
}
if value != defaultValue {
t.Errorf("expected default value from BooleanValue, got %v", value)
}
valueDetails, err := client.BooleanValueDetails(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected BooleanValueDetails to return an error, got nil")
}
if valueDetails.Value != defaultValue {
t.Errorf("expected default value from BooleanValueDetails, got %v", value)
}
})
t.Run("String", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
defaultValue := "default"
mocks.providerAPI.EXPECT().StringEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(StringResolutionDetail{
Value: "foo",
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
}).Times(2)
value, err := client.StringValue(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected StringValue to return an error, got nil")
}
if value != defaultValue {
t.Errorf("expected default value from StringValue, got %v", value)
}
valueDetails, err := client.StringValueDetails(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected StringValueDetails to return an error, got nil")
}
if valueDetails.Value != defaultValue {
t.Errorf("expected default value from StringValueDetails, got %v", value)
}
})
t.Run("Float", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
defaultValue := 3.14159
mocks.providerAPI.EXPECT().FloatEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(FloatResolutionDetail{
Value: 0,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
}).Times(2)
value, err := client.FloatValue(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected FloatValue to return an error, got nil")
}
if value != defaultValue {
t.Errorf("expected default value from FloatValue, got %v", value)
}
valueDetails, err := client.FloatValueDetails(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected FloatValueDetails to return an error, got nil")
}
if valueDetails.Value != defaultValue {
t.Errorf("expected default value from FloatValueDetails, got %v", value)
}
})
t.Run("Int", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
var defaultValue int64 = 3
mocks.providerAPI.EXPECT().IntEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(IntResolutionDetail{
Value: 0,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
}).Times(2)
value, err := client.IntValue(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected IntValue to return an error, got nil")
}
if value != defaultValue {
t.Errorf("expected default value from IntValue, got %v", value)
}
valueDetails, err := client.IntValueDetails(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected FloatValueDetails to return an error, got nil")
}
if valueDetails.Value != defaultValue {
t.Errorf("expected default value from IntValueDetails, got %v", value)
}
})
t.Run("Object", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
type obj struct {
foo string
}
defaultValue := obj{foo: "bar"}
mocks.providerAPI.EXPECT().ObjectEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(InterfaceResolutionDetail{
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError("test"),
},
}).Times(2)
value, err := client.ObjectValue(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected ObjectValue to return an error, got nil")
}
if value != defaultValue {
t.Errorf("expected default value from ObjectValue, got %v", value)
}
valueDetails, err := client.ObjectValueDetails(context.Background(), flagKey, defaultValue, evalCtx)
if err == nil {
t.Error("expected ObjectValueDetails to return an error, got nil")
}
if valueDetails.Value.(obj) != defaultValue {
t.Errorf("expected default value from ObjectValueDetails, got %v", value)
}
})
}
// TODO Requirement_1_4_10
// In the case of abnormal execution, the client SHOULD log an informative error message.
// Requirement_1_4_11
// The `client` SHOULD provide asynchronous or non-blocking mechanisms for flag evaluation.
//
// Satisfied by goroutines.
// In cases of abnormal execution, the `evaluation details` structure's `error message` field MAY contain a
// string containing additional details about the nature of the error.
func TestRequirement_1_4_12(t *testing.T) {
defer t.Cleanup(initSingleton)
errMessage := "error forced by test"
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: true,
ProviderResolutionDetail: ProviderResolutionDetail{
ResolutionError: NewGeneralResolutionError(errMessage),
},
})
evalDetails, err := client.evaluate(
context.Background(), "foo", Boolean, true, EvaluationContext{}, EvaluationOptions{},
)
if err == nil {
t.Error("expected err, got nil")
}
if evalDetails.ErrorMessage != errMessage {
t.Errorf(
"expected evaluation details to contain error message '%s', got '%s'",
errMessage, evalDetails.ErrorMessage,
)
}
}
// Requirement_1_4_13
// If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set,
// the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise,
// it MUST contain an empty record.
func TestRequirement_1_4_13(t *testing.T) {
flagKey := "flag-key"
evalCtx := EvaluationContext{}
flatCtx := flattenContext(evalCtx)
t.Run("No Metadata", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
defaultValue := true
mocks.providerAPI.EXPECT().BooleanEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(BoolResolutionDetail{
Value: true,
ProviderResolutionDetail: ProviderResolutionDetail{
FlagMetadata: nil,
},
}).Times(1)
evDetails, err := client.BooleanValueDetails(context.Background(), flagKey, defaultValue, EvaluationContext{})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(evDetails.FlagMetadata, FlagMetadata{}) {
t.Errorf(
"flag metadata is not as expected in EvaluationDetail, got %v, expected %v",
evDetails.FlagMetadata, FlagMetadata{},
)
}
})
t.Run("Metadata present", func(t *testing.T) {
defer t.Cleanup(initSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
defaultValue := true
metadata := FlagMetadata{
"bing": "bong",
}
mocks.providerAPI.EXPECT().BooleanEvaluation(context.Background(), flagKey, defaultValue, flatCtx).
Return(BoolResolutionDetail{
Value: true,
ProviderResolutionDetail: ProviderResolutionDetail{
FlagMetadata: metadata,
},
}).Times(1)
evDetails, err := client.BooleanValueDetails(context.Background(), flagKey, defaultValue, EvaluationContext{})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(metadata, evDetails.FlagMetadata) {
t.Errorf(
"flag metadata is not as expected in EvaluationDetail, got %v, expected %v",
evDetails.FlagMetadata, metadata,
)
}
})
}
// Requirement_1_5_1
// The `evaluation options` structure's `hooks` field denotes an ordered collection of hooks that the client MUST
// execute for the respective flag evaluation, in addition to those already configured.
//
// Is tested by TestRequirement_4_4_2.
// TODO Requirement_1_6_1
// The `client` SHOULD transform the `evaluation context` using the `provider's` `context transformer` function
// if one is defined, before passing the result of the transformation to the provider's flag resolution functions.
// TestRequirement_6_1 tests the 6.1.1 and 6.1.2 requirements by asserting that the returned client matches the interface
// defined by the 6.1.1 and 6.1.2 requirements
// Requirement_6_1_1
// The `client` MUST define a function for tracking the occurrence of a particular action or application state,
// with parameters `tracking event name` (string, required), `evaluation context` (optional) and `tracking event details` (optional),
// which returns nothing.
// Requirement_6_1_2
// The `client` MUST define a function for tracking the occurrence of a particular action or application state,
// with parameters `tracking event name` (string, required) and `tracking event details` (optional), which returns nothing.
func TestRequirement_6_1(t *testing.T) {
client := NewClient("test-client")
type requirements interface {
Track(ctx context.Context, trackingEventName string, evalCtx EvaluationContext, details TrackingEventDetails)
}
var clientI interface{} = client
if _, ok := clientI.(requirements); !ok {
t.Error("client returned by NewClient doesn't implement the 1.6.* requirements interface")
}
}
// Requirement_6_1_3
// The evaluation context passed to the provider's track function MUST be merged in the order, with duplicate values being overwritten:
// - API (global; lowest precedence)
// - transaction
// - client
// - invocation (highest precedence)
// Requirement_6_1_4
// If the client's `track` function is called and the associated provider does not implement tracking, the client's `track` function MUST no-op.
// Allow backward compatible to non-Tracker Provider
func TestTrack(t *testing.T) {
type inputCtx struct {
api EvaluationContext
txn EvaluationContext
client EvaluationContext
invocation EvaluationContext
}
// mockTrackingProvider is a feature provider that implements tracker contract.
type mockTrackingProvider struct {
*MockTracker
*MockFeatureProvider
}
type testcase struct {
inCtx inputCtx
eventName string
outCtx EvaluationContext
// allow asserting the input to provider
provider func(tc *testcase, provider *MockFeatureProvider) FeatureProvider
}
tests := map[string]*testcase{
"merging in correct order": {
eventName: "example-event",
inCtx: inputCtx{
api: EvaluationContext{
attributes: map[string]interface{}{
"1": "api",
"2": "api",
"3": "api",
"4": "api",
},
},
txn: EvaluationContext{
attributes: map[string]interface{}{
"2": "txn",
"3": "txn",
"4": "txn",
},
},
client: EvaluationContext{
attributes: map[string]interface{}{
"3": "client",
"4": "client",
},
},
invocation: EvaluationContext{
attributes: map[string]interface{}{
"4": "invocation",
},
},
},
outCtx: EvaluationContext{
attributes: map[string]interface{}{
"1": "api",
"2": "txn",
"3": "client",
"4": "invocation",
},
},
provider: func(tc *testcase, mockProvider *MockFeatureProvider) FeatureProvider {
provider := &mockTrackingProvider{
MockTracker: NewMockTracker(mockProvider.ctrl),
MockFeatureProvider: mockProvider,
}
// assert AnyTimesif Track is called once with evalCtx expected
provider.MockTracker.EXPECT().Track(gomock.Any(), gomock.Any(), tc.outCtx, TrackingEventDetails{}).Times(1)
return provider
},
},
"do no-op if Provider do not implement Tracker": {
inCtx: inputCtx{},
eventName: "example-event",
outCtx: EvaluationContext{},
provider: func(tc *testcase, provider *MockFeatureProvider) FeatureProvider {
return provider
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// arrange
mocks := hydratedMocksForClientTests(t, 0)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
provider := test.provider(test, mocks.providerAPI)
mocks.evaluationAPI.EXPECT().ForEvaluation("test-client").AnyTimes().DoAndReturn(func(_ string) (FeatureProvider, []Hook, EvaluationContext) {
return provider, nil, test.inCtx.api
})
client.evaluationContext = test.inCtx.client
ctx := WithTransactionContext(context.Background(), test.inCtx.txn)
// action
client.Track(ctx, test.eventName, test.inCtx.invocation, TrackingEventDetails{})
})
}
}
func TestFlattenContext(t *testing.T) {
tests := map[string]struct {
inCtx EvaluationContext
outCtx FlattenedContext
}{
"happy path": {
inCtx: EvaluationContext{
attributes: map[string]interface{}{
"1": "string",
"2": 0.01,
"3": false,
},
targetingKey: "user",
},
outCtx: FlattenedContext{
TargetingKey: "user",
"1": "string",
"2": 0.01,
"3": false,
},
},
"no targeting key": {
inCtx: EvaluationContext{
attributes: map[string]interface{}{
"1": "string",
"2": 0.01,
"3": false,
},
},
outCtx: FlattenedContext{
"1": "string",
"2": 0.01,
"3": false,
},
},
"duplicated key": {
inCtx: EvaluationContext{
targetingKey: "user",
attributes: map[string]interface{}{
TargetingKey: "not user",
"1": "string",
"2": 0.01,
"3": false,
},
},
outCtx: FlattenedContext{
TargetingKey: "user",
"1": "string",
"2": 0.01,
"3": false,
},
},
"no attributes": {
inCtx: EvaluationContext{
targetingKey: "user",
},
outCtx: FlattenedContext{
TargetingKey: "user",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
out := flattenContext(test.inCtx)
if !reflect.DeepEqual(test.outCtx, out) {
t.Errorf(
"%s, unexpected value received from flatten context, expected %v got %v",
name,
test.outCtx,
out,
)
}
})
}
}
// TestBeforeHookNilContext asserts that when a Before hook returns a nil EvaluationContext it doesn't overwrite the
// existing EvaluationContext
func TestBeforeHookNilContext(t *testing.T) {
defer t.Cleanup(initSingleton)
hookNilContext := UnimplementedHook{}
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
attributes := map[string]interface{}{"should": "persist"}
evalCtx := EvaluationContext{attributes: attributes}
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), attributes)
_, err := client.BooleanValue(
context.Background(), "foo", false, evalCtx, WithHooks(hookNilContext),
)
if err != nil {
t.Error(err)
}