-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_input_validator.py
More file actions
1135 lines (958 loc) · 53.3 KB
/
test_input_validator.py
File metadata and controls
1135 lines (958 loc) · 53.3 KB
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
"""Unit tests for the input_validator module."""
#pylint: disable=protected-access,too-many-statements,no-self-use,line-too-long
from __future__ import absolute_import, division, print_function, \
unicode_literals
import logging
from splitio.client.factory import SplitFactory, get_factory
from splitio.client.client import CONTROL, Client
from splitio.client.manager import SplitManager
from splitio.client.key import Key
from splitio.storage import SplitStorage, EventStorage, ImpressionStorage, TelemetryStorage, \
SegmentStorage
from splitio.models.splits import Split
from splitio.client import input_validator
class ClientInputValidationTests(object):
"""Input validation test cases."""
def test_get_treatment(self, mocker):
"""Test get_treatment validation."""
split_mock = mocker.Mock(spec=Split)
default_treatment_mock = mocker.PropertyMock()
default_treatment_mock.return_value = 'default_treatment'
type(split_mock).default_treatment = default_treatment_mock
conditions_mock = mocker.PropertyMock()
conditions_mock.return_value = []
type(split_mock).conditions = conditions_mock
storage_mock = mocker.Mock(spec=SplitStorage)
storage_mock.get.return_value = split_mock
def _get_storage_mock(storage):
return {
'splits': storage_mock,
'segments': mocker.Mock(spec=SegmentStorage),
'impressions': mocker.Mock(spec=ImpressionStorage),
'events': mocker.Mock(spec=EventStorage),
'telemetry': mocker.Mock(spec=TelemetryStorage)
}[storage]
factory_mock = mocker.Mock(spec=SplitFactory)
factory_mock._get_storage.side_effect = _get_storage_mock
factory_destroyed = mocker.PropertyMock()
factory_destroyed.return_value = False
type(factory_mock).destroyed = factory_destroyed
client = Client(factory_mock)
client._logger = mocker.Mock()
mocker.patch('splitio.client.input_validator._LOGGER', new=client._logger)
assert client.get_treatment(None, 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null key, key must be a non-empty string.', 'get_treatment')
]
client._logger.reset_mock()
assert client.get_treatment('', 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment', 'key', 'key')
]
client._logger.reset_mock()
key = ''.join('a' for _ in range(0, 255))
assert client.get_treatment(key, 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatment', 'key', 250)
]
client._logger.reset_mock()
assert client.get_treatment(12345, 'some_feature') == 'default_treatment'
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment', 'key', 12345)
]
client._logger.reset_mock()
assert client.get_treatment(float('nan'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment(float('inf'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment(True, 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment([], 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', None) == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', 123) == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', True) == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', []) == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', '') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment('some_key', 'some_feature') == 'default_treatment'
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment(Key(None, 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key('', 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key(float('nan'), 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key(float('inf'), 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key(True, 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key([], 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key(12345, 'bucketing_key'), 'some_feature') == 'default_treatment'
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment', 'matching_key', 12345)
]
client._logger.reset_mock()
key = ''.join('a' for _ in range(0, 255))
assert client.get_treatment(Key(key, 'bucketing_key'), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatment', 'matching_key', 250)
]
client._logger.reset_mock()
assert client.get_treatment(Key('matching_key', None), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key('matching_key', True), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key('matching_key', []), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key('matching_key', ''), 'some_feature') == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment(Key('matching_key', 12345), 'some_feature') == 'default_treatment'
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment', 'bucketing_key', 12345)
]
client._logger.reset_mock()
assert client.get_treatment('matching_key', 'some_feature', True) == CONTROL
assert client._logger.error.mock_calls == [
mocker.call('%s: attributes must be of type dictionary.', 'get_treatment')
]
client._logger.reset_mock()
assert client.get_treatment('matching_key', 'some_feature', {'test': 'test'}) == 'default_treatment'
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment('matching_key', 'some_feature', None) == 'default_treatment'
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment('matching_key', ' some_feature ', None) == 'default_treatment'
assert client._logger.warning.mock_calls == [
mocker.call('%s: feature_name \'%s\' has extra whitespace, trimming.', 'get_treatment', ' some_feature ')
]
client._logger.reset_mock()
storage_mock.get.return_value = None
assert client.get_treatment('matching_key', 'some_feature', None) == CONTROL
assert client._logger.warning.mock_calls == [
mocker.call(
"%s: you passed \"%s\" that does not exist in this environment, "
"please double check what Splits exist in the web console.",
'get_treatment',
'some_feature'
)
]
def test_get_treatment_with_config(self, mocker):
"""Test get_treatment validation."""
split_mock = mocker.Mock(spec=Split)
default_treatment_mock = mocker.PropertyMock()
default_treatment_mock.return_value = 'default_treatment'
type(split_mock).default_treatment = default_treatment_mock
conditions_mock = mocker.PropertyMock()
conditions_mock.return_value = []
type(split_mock).conditions = conditions_mock
def _configs(treatment):
return '{"some": "property"}' if treatment == 'default_treatment' else None
split_mock.get_configurations_for.side_effect = _configs
storage_mock = mocker.Mock(spec=SplitStorage)
storage_mock.get.return_value = split_mock
def _get_storage_mock(storage):
return {
'splits': storage_mock,
'segments': mocker.Mock(spec=SegmentStorage),
'impressions': mocker.Mock(spec=ImpressionStorage),
'events': mocker.Mock(spec=EventStorage),
'telemetry': mocker.Mock(spec=TelemetryStorage)
}[storage]
factory_mock = mocker.Mock(spec=SplitFactory)
factory_mock._get_storage.side_effect = _get_storage_mock
factory_destroyed = mocker.PropertyMock()
factory_destroyed.return_value = False
type(factory_mock).destroyed = factory_destroyed
client = Client(factory_mock)
client._logger = mocker.Mock()
mocker.patch('splitio.client.input_validator._LOGGER', new=client._logger)
assert client.get_treatment_with_config(None, 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null key, key must be a non-empty string.', 'get_treatment_with_config')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('', 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment_with_config', 'key', 'key')
]
client._logger.reset_mock()
key = ''.join('a' for _ in range(0, 255))
assert client.get_treatment_with_config(key, 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatment_with_config', 'key', 250)
]
client._logger.reset_mock()
assert client.get_treatment_with_config(12345, 'some_feature') == ('default_treatment', '{"some": "property"}')
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment_with_config', 'key', 12345)
]
client._logger.reset_mock()
assert client.get_treatment_with_config(float('nan'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(float('inf'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(True, 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config([], 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', None) == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment_with_config', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', 123) == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', True) == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', []) == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', '') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment_with_config', 'feature_name', 'feature_name')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('some_key', 'some_feature') == ('default_treatment', '{"some": "property"}')
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment_with_config(Key(None, 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('', 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key(float('nan'), 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key(float('inf'), 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key(True, 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key([], 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'matching_key', 'matching_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key(12345, 'bucketing_key'), 'some_feature') == ('default_treatment', '{"some": "property"}')
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment_with_config', 'matching_key', 12345)
]
client._logger.reset_mock()
key = ''.join('a' for _ in range(0, 255))
assert client.get_treatment_with_config(Key(key, 'bucketing_key'), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatment_with_config', 'matching_key', 250)
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('matching_key', None), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null %s, %s must be a non-empty string.', 'get_treatment_with_config', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('matching_key', True), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('matching_key', []), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatment_with_config', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('matching_key', ''), 'some_feature') == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatment_with_config', 'bucketing_key', 'bucketing_key')
]
client._logger.reset_mock()
assert client.get_treatment_with_config(Key('matching_key', 12345), 'some_feature') == ('default_treatment', '{"some": "property"}')
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatment_with_config', 'bucketing_key', 12345)
]
client._logger.reset_mock()
assert client.get_treatment_with_config('matching_key', 'some_feature', True) == (CONTROL, None)
assert client._logger.error.mock_calls == [
mocker.call('%s: attributes must be of type dictionary.', 'get_treatment_with_config')
]
client._logger.reset_mock()
assert client.get_treatment_with_config('matching_key', 'some_feature', {'test': 'test'}) == ('default_treatment', '{"some": "property"}')
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment_with_config('matching_key', 'some_feature', None) == ('default_treatment', '{"some": "property"}')
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.get_treatment_with_config('matching_key', ' some_feature ', None) == ('default_treatment', '{"some": "property"}')
assert client._logger.warning.mock_calls == [
mocker.call('%s: feature_name \'%s\' has extra whitespace, trimming.', 'get_treatment_with_config', ' some_feature ')
]
client._logger.reset_mock()
storage_mock.get.return_value = None
assert client.get_treatment_with_config('matching_key', 'some_feature', None) == (CONTROL, None)
assert client._logger.warning.mock_calls == [
mocker.call(
"%s: you passed \"%s\" that does not exist in this environment, "
"please double check what Splits exist in the web console.",
'get_treatment_with_config',
'some_feature'
)
]
def test_valid_properties(self, mocker):
"""Test valid_properties() method"""
assert input_validator.valid_properties(None) == (True, None, 1024)
assert input_validator.valid_properties([]) == (False, None, 0)
assert input_validator.valid_properties(True) == (False, None, 0)
assert input_validator.valid_properties(dict()) == (True, None, 1024)
assert input_validator.valid_properties({ 2: 123 }) == (True, None, 1024)
class Test:
pass
assert input_validator.valid_properties({
"test": Test()
}) == (True, { "test": None }, 1028)
props1 = {
"test1": "test",
"test2": 1,
"test3": True,
"test4": None,
"test5": [],
2: "t",
}
r1, r2, r3 = input_validator.valid_properties(props1)
assert r1 == True
assert len(r2.keys()) == 5
assert r2["test1"] == "test"
assert r2["test2"] == 1
assert r2["test3"] == True
assert r2["test4"] == None
assert r2["test5"] == None
assert r3 == 1053
props2 = dict();
for i in range(301):
props2[str(i)] = i
assert input_validator.valid_properties(props2) == (True, props2, 1817)
props3 = dict();
for i in range(100, 210):
props3["prop" + str(i)] = "a" * 300
r1, r2, r3 = input_validator.valid_properties(props3)
assert r1 == False
assert r3 == 32952
def test_track(self, mocker):
"""Test track method()."""
events_storage_mock = mocker.Mock(spec=EventStorage)
events_storage_mock.put.return_value = True
factory_mock = mocker.Mock(spec=SplitFactory)
factory_destroyed = mocker.PropertyMock()
factory_destroyed.return_value = False
type(factory_mock).destroyed = factory_destroyed
factory_mock._apikey = 'some-test'
client = Client(factory_mock)
client._events_storage = mocker.Mock(spec=EventStorage)
client._events_storage.put.return_value = True
client._logger = mocker.Mock()
mocker.patch('splitio.client.input_validator._LOGGER', new=client._logger)
assert client.track(None, "traffic_type", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed a null %s, %s must be a non-empty string.", 'track', 'key', 'key')
]
client._logger.reset_mock()
assert client.track("", "traffic_type", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an empty %s, %s must be a non-empty string.", 'track', 'key', 'key')
]
client._logger.reset_mock()
assert client.track(12345, "traffic_type", "event_type", 1) is True
assert client._logger.warning.mock_calls == [
mocker.call("%s: %s %s is not of type string, converting.", 'track', 'key', 12345)
]
client._logger.reset_mock()
assert client.track(True, "traffic_type", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'key', 'key')
]
client._logger.reset_mock()
assert client.track([], "traffic_type", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'key', 'key')
]
client._logger.reset_mock()
key = ''.join('a' for _ in range(0, 255))
assert client.track(key, "traffic_type", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: %s too long - must be %s characters or less.", 'track', 'key', 250)
]
client._logger.reset_mock()
assert client.track("some_key", None, "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed a null %s, %s must be a non-empty string.", 'track', 'traffic_type', 'traffic_type')
]
client._logger.reset_mock()
assert client.track("some_key", "", "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an empty %s, %s must be a non-empty string.", 'track', 'traffic_type', 'traffic_type')
]
client._logger.reset_mock()
assert client.track("some_key", 12345, "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'traffic_type', 'traffic_type')
]
client._logger.reset_mock()
assert client.track("some_key", True, "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'traffic_type', 'traffic_type')
]
client._logger.reset_mock()
assert client.track("some_key", [], "event_type", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'traffic_type', 'traffic_type')
]
client._logger.reset_mock()
assert client.track("some_key", "TRAFFIC_type", "event_type", 1) is True
assert client._logger.warning.mock_calls == [
mocker.call("track: %s should be all lowercase - converting string to lowercase.", 'TRAFFIC_type')
]
assert client.track("some_key", "traffic_type", None, 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed a null %s, %s must be a non-empty string.", 'track', 'event_type', 'event_type')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an empty %s, %s must be a non-empty string.", 'track', 'event_type', 'event_type')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", True, 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'event_type', 'event_type')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", [], 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'event_type', 'event_type')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", 12345, 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed an invalid %s, %s must be a non-empty string.", 'track', 'event_type', 'event_type')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "@@", 1) is False
assert client._logger.error.mock_calls == [
mocker.call("%s: you passed %s, event_type must adhere to the regular "
"expression %s. This means "
"an event name must be alphanumeric, cannot be more than 80 "
"characters long, and can only include a dash, underscore, "
"period, or colon as separators of alphanumeric characters.",
'track', '@@', '^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$')
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", None) is True
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1) is True
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1.23) is True
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", "test") is False
assert client._logger.error.mock_calls == [
mocker.call("track: value must be a number.")
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", True) is False
assert client._logger.error.mock_calls == [
mocker.call("track: value must be a number.")
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", []) is False
assert client._logger.error.mock_calls == [
mocker.call("track: value must be a number.")
]
# Test traffic type existance
ready_property = mocker.PropertyMock()
ready_property.return_value = True
type(factory_mock).ready = ready_property
split_storage_mock = mocker.Mock(spec=SplitStorage)
split_storage_mock.is_valid_traffic_type.return_value = True
factory_mock._get_storage.return_value = split_storage_mock
# Test that it doesn't warn if tt is cached, not in localhost mode and sdk is ready
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", None) is True
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == []
# Test that it does warn if tt is cached, not in localhost mode and sdk is ready
split_storage_mock.is_valid_traffic_type.return_value = False
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", None) is True
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == [mocker.call(
'track: Traffic Type %s does not have any corresponding Splits in this environment, '
'make sure you\'re tracking your events to a valid traffic type defined '
'in the Split console.',
'traffic_type'
)]
# Test that it does not warn when in localhost mode.
factory_mock._apikey = 'localhost'
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", None) is True
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == []
# Test that it does not warn when not in localhost mode and not ready
factory_mock._apikey = 'not-localhost'
ready_property.return_value = False
type(factory_mock).ready = ready_property
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", None) is True
assert client._logger.error.mock_calls == []
assert client._logger.warning.mock_calls == []
# Test track with invalid properties
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, []) is False
assert client._logger.error.mock_calls == [
mocker.call("track: properties must be of type dictionary.")
]
# Test track with invalid properties
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, True) is False
assert client._logger.error.mock_calls == [
mocker.call("track: properties must be of type dictionary.")
]
# Test track with properties
props1 = {
"test1": "test",
"test2": 1,
"test3": True,
"test4": None,
"test5": [],
2: "t",
}
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, props1) is True
assert client._logger.warning.mock_calls == [
mocker.call("Property %s is of invalid type. Setting value to None", [])
]
# Test track with more than 300 properties
props2 = dict();
for i in range(301):
props2[str(i)] = i
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, props2) is True
assert client._logger.warning.mock_calls == [
mocker.call("Event has more than 300 properties. Some of them will be trimmed when processed")
]
# Test track with properties higher than 32kb
client._logger.reset_mock()
props3 = dict();
for i in range(100, 210):
props3["prop" + str(i)] = "a" * 300
assert client.track("some_key", "traffic_type", "event_type", 1, props3) is False
assert client._logger.error.mock_calls == [
mocker.call("The maximum size allowed for the properties is 32768 bytes. Current one is 32952 bytes. Event not queued")
]
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, None) is True
assert client._logger.error.mock_calls == []
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, 1573936400000) is True
assert client._logger.error.mock_calls == []
# Test track with invalid timestamp
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, "invalid_timestamp") is False
assert client._logger.error.mock_calls == [
mocker.call("track: timestamp must be an integer.")
]
def test_get_treatments(self, mocker):
"""Test getTreatments() method."""
split_mock = mocker.Mock(spec=Split)
default_treatment_mock = mocker.PropertyMock()
default_treatment_mock.return_value = 'default_treatment'
type(split_mock).default_treatment = default_treatment_mock
conditions_mock = mocker.PropertyMock()
conditions_mock.return_value = []
type(split_mock).conditions = conditions_mock
storage_mock = mocker.Mock(spec=SplitStorage)
storage_mock.fetch_many.return_value = {
'some_feature': split_mock,
'some': split_mock,
}
def _get_storage_mock(storage):
return {
'splits': storage_mock,
'segments': mocker.Mock(spec=SegmentStorage),
'impressions': mocker.Mock(spec=ImpressionStorage),
'events': mocker.Mock(spec=EventStorage),
'telemetry': mocker.Mock(spec=TelemetryStorage)
}[storage]
factory_mock = mocker.Mock(spec=SplitFactory)
factory_mock._get_storage.side_effect = _get_storage_mock
factory_destroyed = mocker.PropertyMock()
factory_destroyed.return_value = False
type(factory_mock).destroyed = factory_destroyed
client = Client(factory_mock)
client._logger = mocker.Mock()
mocker.patch('splitio.client.input_validator._LOGGER', new=client._logger)
assert client.get_treatments(None, ['some_feature']) == {'some_feature': CONTROL}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null key, key must be a non-empty string.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments("", ['some_feature']) == {'some_feature': CONTROL}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatments', 'key', 'key')
]
key = ''.join('a' for _ in range(0, 255))
client._logger.reset_mock()
assert client.get_treatments(key, ['some_feature']) == {'some_feature': CONTROL}
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatments', 'key', 250)
]
client._logger.reset_mock()
assert client.get_treatments(12345, ['some_feature']) == {'some_feature': 'default_treatment'}
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatments', 'key', 12345)
]
client._logger.reset_mock()
assert client.get_treatments(True, ['some_feature']) == {'some_feature': CONTROL}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatments', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatments([], ['some_feature']) == {'some_feature': CONTROL}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatments', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', None) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', True) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', 'some_string') == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', []) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', [None, None]) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments')
]
client._logger.reset_mock()
assert client.get_treatments('some_key', [True]) == {}
assert mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments') in client._logger.error.mock_calls
client._logger.reset_mock()
assert client.get_treatments('some_key', ['', '']) == {}
assert mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments') in client._logger.error.mock_calls
client._logger.reset_mock()
assert client.get_treatments('some_key', ['some ']) == {'some': 'default_treatment'}
assert client._logger.warning.mock_calls == [
mocker.call('%s: feature_name \'%s\' has extra whitespace, trimming.', 'get_treatments', 'some ')
]
client._logger.reset_mock()
storage_mock.fetch_many.return_value = {
'some_feature': None
}
storage_mock.get.return_value = None
ready_mock = mocker.PropertyMock()
ready_mock.return_value = True
type(factory_mock).ready = ready_mock
assert client.get_treatments('matching_key', ['some_feature'], None) == {'some_feature': CONTROL}
assert client._logger.warning.mock_calls == [
mocker.call(
"%s: you passed \"%s\" that does not exist in this environment, "
"please double check what Splits exist in the web console.",
'get_treatments',
'some_feature'
)
]
def test_get_treatments_with_config(self, mocker):
"""Test getTreatments() method."""
split_mock = mocker.Mock(spec=Split)
default_treatment_mock = mocker.PropertyMock()
default_treatment_mock.return_value = 'default_treatment'
type(split_mock).default_treatment = default_treatment_mock
conditions_mock = mocker.PropertyMock()
conditions_mock.return_value = []
type(split_mock).conditions = conditions_mock
storage_mock = mocker.Mock(spec=SplitStorage)
storage_mock.fetch_many.return_value = {
'some_feature': split_mock
}
factory_mock = mocker.Mock(spec=SplitFactory)
factory_mock._get_storage.return_value = storage_mock
factory_destroyed = mocker.PropertyMock()
factory_destroyed.return_value = False
type(factory_mock).destroyed = factory_destroyed
def _configs(treatment):
return '{"some": "property"}' if treatment == 'default_treatment' else None
split_mock.get_configurations_for.side_effect = _configs
client = Client(factory_mock)
client._logger = mocker.Mock()
mocker.patch('splitio.client.input_validator._LOGGER', new=client._logger)
assert client.get_treatments_with_config(None, ['some_feature']) == {'some_feature': (CONTROL, None)}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed a null key, key must be a non-empty string.', 'get_treatments_with_config')
]
client._logger.reset_mock()
assert client.get_treatments_with_config("", ['some_feature']) == {'some_feature': (CONTROL, None)}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an empty %s, %s must be a non-empty string.', 'get_treatments_with_config', 'key', 'key')
]
key = ''.join('a' for _ in range(0, 255))
client._logger.reset_mock()
assert client.get_treatments_with_config(key, ['some_feature']) == {'some_feature': (CONTROL, None)}
assert client._logger.error.mock_calls == [
mocker.call('%s: %s too long - must be %s characters or less.', 'get_treatments_with_config', 'key', 250)
]
client._logger.reset_mock()
assert client.get_treatments_with_config(12345, ['some_feature']) == {'some_feature': ('default_treatment', '{"some": "property"}')}
assert client._logger.warning.mock_calls == [
mocker.call('%s: %s %s is not of type string, converting.', 'get_treatments_with_config', 'key', 12345)
]
client._logger.reset_mock()
assert client.get_treatments_with_config(True, ['some_feature']) == {'some_feature': (CONTROL, None)}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatments_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatments_with_config([], ['some_feature']) == {'some_feature': (CONTROL, None)}
assert client._logger.error.mock_calls == [
mocker.call('%s: you passed an invalid %s, %s must be a non-empty string.', 'get_treatments_with_config', 'key', 'key')
]
client._logger.reset_mock()
assert client.get_treatments_with_config('some_key', None) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments_with_config')
]
client._logger.reset_mock()
assert client.get_treatments_with_config('some_key', True) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments_with_config')
]
client._logger.reset_mock()
assert client.get_treatments_with_config('some_key', 'some_string') == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments_with_config')
]
client._logger.reset_mock()
assert client.get_treatments_with_config('some_key', []) == {}
assert client._logger.error.mock_calls == [
mocker.call('%s: feature_names must be a non-empty array.', 'get_treatments_with_config')