-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nocasedict.py
executable file
·2636 lines (2382 loc) · 76.3 KB
/
test_nocasedict.py
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
"""
Test the NocaseDict class.
"""
from __future__ import absolute_import
import sys
import re
from collections import OrderedDict
import pytest
from ..utils.simplified_test_function import simplified_test_function
# pylint: disable=wrong-import-position, wrong-import-order, invalid-name
from ..utils.import_installed import import_installed
nocasedict = import_installed('nocasedict')
from nocasedict import NocaseDict as _NocaseDict # noqa: E402
# pylint: enable=wrong-import-position, wrong-import-order, invalid-name
# Controlls whether the tests are run against a standard dict instead of
# NocaseDict. This should be False normally, but when testcases are added or
# changed, it should be set to True in a local test to verify that the new
# or changed testcase is compatible with the behavior of the standard dict.
TEST_AGAINST_DICT = False
# The dictionary class being tested
# pylint: disable=invalid-name
NocaseDict = dict if TEST_AGAINST_DICT else _NocaseDict
# Indicates that the dict being tested is guaranteed to preserve order
TESTDICT_IS_ORDERED = \
not TEST_AGAINST_DICT or sys.version_info[0:2] >= (3, 7)
# Indicates the dict being tested supports lt/gt comparison (between dicts)
TESTDICT_SUPPORTS_COMPARISON = \
TEST_AGAINST_DICT and sys.version_info[0:2] == (2, 7)
# Indicates the dict being tested supports non-string init kwargs
TESTDICT_SUPPORTS_NONSTRING_KWARGS = \
TEST_AGAINST_DICT and sys.version_info[0:2] == (2, 7)
# Indicates the dict being tested issues UserWarning about unordered kwargs
TESTDICT_WARNS_KWARGS = \
not TEST_AGAINST_DICT and sys.version_info[0:2] < (3, 7)
class NonComparable(object):
# pylint: disable=too-few-public-methods
"""
Class that raises TypeError when comparing its objects for equality or
when hashing its objects.
"""
def __eq__(self, other):
raise TypeError("Cannot compare %s to %s" % (type(self), type(other)))
def __ne__(self, other):
raise TypeError("Cannot compare %s to %s" % (type(self), type(other)))
def __hash__(self):
raise TypeError("Cannot hash %s" % type(self))
TESTCASES_NOCASEDICT_INIT = [
# Testcases for NocaseDict.__init__() / ncd=NocaseDict()
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * init_args: Tuple of positional arguments to NocaseDict().
# * init_kwargs: Dict of keyword arguments to NocaseDict().
# * exp_dict: Expected resulting dictionary, as OrderedDict.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict from no args",
dict(
init_args=(),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
None, None, True
),
(
"Empty dict from None as positional arg (not iterable)",
dict(
init_args=(None,),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
TypeError, None, True
),
(
"Empty dict from empty list as positional arg",
dict(
init_args=(list(),),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
None, None, True
),
(
"Empty dict from empty tuple as positional arg",
dict(
init_args=(tuple(),),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
None, None, True
),
(
"Empty dict from empty dict as positional arg",
dict(
init_args=(dict(),),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
None, None, True
),
(
"Empty dict from empty NocaseDict as positional arg",
dict(
init_args=(NocaseDict(),),
init_kwargs={},
exp_dict=OrderedDict(),
verify_order=True,
),
None, None, True
),
# Non-empty NocaseDict
(
"Dict from list as positional arg",
dict(
init_args=([('Dog', 'Cat'), ('Budgie', 'Fish')],),
init_kwargs={},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=TESTDICT_IS_ORDERED,
),
None, None, True
),
(
"Dict from tuple as positional arg",
dict(
init_args=((('Dog', 'Cat'), ('Budgie', 'Fish')),),
init_kwargs={},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=TESTDICT_IS_ORDERED,
),
None, None, True
),
(
"Dict from dict as positional arg",
dict(
init_args=({'Dog': 'Cat', 'Budgie': 'Fish'},),
init_kwargs={},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=False,
),
None, None, True
),
(
"Dict from keyword args",
dict(
init_args=(),
init_kwargs={'Dog': 'Cat', 'Budgie': 'Fish'},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=False,
),
None, UserWarning if TESTDICT_WARNS_KWARGS else None, True
),
(
"Dict from list as positional arg and keyword args",
dict(
init_args=([('Dog', 'Cat')],),
init_kwargs={'Budgie': 'Fish'},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=TESTDICT_IS_ORDERED,
),
None, None, True
),
(
"Dict from tuple as positional arg and keyword args",
dict(
init_args=((('Dog', 'Cat'),),),
init_kwargs={'Budgie': 'Fish'},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=TESTDICT_IS_ORDERED,
),
None, None, True
),
(
"Dict from dict as positional arg and keyword args",
dict(
init_args=({'Dog': 'Cat'},),
init_kwargs={'Budgie': 'Fish'},
exp_dict=OrderedDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
verify_order=TESTDICT_IS_ORDERED,
),
None, None, True
),
# Error cases
(
"String as positional arg (items cannot be unpacked into k,v)",
dict(
init_args=('illegal',),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
ValueError, None, True
),
(
"Integer as positional arg (not iterable)",
dict(
init_args=(42,),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
TypeError, None, True
),
(
"Two positional args (too many args)",
dict(
init_args=(list(), list()),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
TypeError, None, True
),
(
"List as positional arg, whose item has only one item (cannot be "
"unpacked into k,v)",
dict(
init_args=([('Dog',)],),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
ValueError, None, True
),
(
"List as positional arg, whose item has too many items (cannot be "
"unpacked into k,v)",
dict(
init_args=([('Dog', 'Cat', 'bad')],),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
ValueError, None, True
),
(
"Tuple as positional arg, whose item has only one item (cannot be "
"unpacked into k,v)",
dict(
init_args=((('Dog',),),),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
ValueError, None, True
),
(
"Tuple as positional arg, whose item has too many items (cannot be "
"unpacked into k,v)",
dict(
init_args=((('Dog', 'Cat', 'bad'),),),
init_kwargs={},
exp_dict=None,
verify_order=None,
),
ValueError, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_INIT)
@simplified_test_function
def test_NocaseDict_init(testcase,
init_args, init_kwargs, exp_dict, verify_order):
"""
Test function for NocaseDict.__init__() / ncd=NocaseDict()
"""
# The code to be tested
act_dict = NocaseDict(*init_args, **init_kwargs)
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
# The verification below also uses some NocaseDict features, but that is
# unavoidable if we want to work through the public interface:
act_items = []
for key in act_dict: # Uses NocaseDict iteration
act_value = act_dict[key] # Uses NocaseDict getitem
assert key in exp_dict, "Unexpected extra key %r" % key
exp_value = exp_dict[key]
assert act_value == exp_value, "Unexpected value at key %r" % key
act_items.append((key, act_value))
exp_items = []
for key in exp_dict:
exp_value = exp_dict[key]
# Next line uses NocaseDict contains:
assert key in act_dict, "Unexpected missing key %r" % key
act_value = act_dict[key] # Uses NocaseDict getitem
assert act_value == exp_value, "Unexpected value at key %r" % key
exp_items.append((key, exp_value))
if verify_order:
assert act_items == exp_items, "Unexpected order of items"
TESTCASES_NOCASEDICT_GETITEM = [
# Testcases for NocaseDict.__getitem__() / ncd[key]
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * key: Key to be used for the test.
# * exp_value: Expected value for the key.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict, with None key (not found)",
dict(
obj=NocaseDict(),
key=None,
exp_value=None,
),
KeyError, None, True
),
(
"Empty dict, with integer key (no lower / not found)",
dict(
obj=NocaseDict(),
key=1234,
exp_value=None,
),
KeyError if TEST_AGAINST_DICT else TypeError, None, True
),
(
"Empty dict, with empty string key (not found)",
dict(
obj=NocaseDict(),
key='',
exp_value=None,
),
KeyError, None, True
),
(
"Empty dict, with non-empty key (not found)",
dict(
obj=NocaseDict(),
key='Dog',
exp_value=None,
),
KeyError, None, True
),
# Non-empty NocaseDict
(
"Non-empty dict, with None key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key=None,
exp_value=None,
),
KeyError, None, True
),
(
"Non-empty dict, with empty string key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='',
exp_value=None,
),
KeyError, None, True
),
(
"Non-empty dict, with non-empty non-existing key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='invalid',
exp_value=None,
),
KeyError, None, True
),
(
"Non-empty dict, with existing key in original case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='Dog',
exp_value='Cat',
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original upper case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='DOG',
exp_value='Cat',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
(
"Non-empty dict, with existing key in non-original lower case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='dog',
exp_value='Cat',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
(
"Non-empty dict, with existing key in non-original mixed case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='doG',
exp_value='Cat',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_GETITEM)
@simplified_test_function
def test_NocaseDict_getitem(testcase,
obj, key, exp_value):
"""
Test function for NocaseDict.__getitem__() / ncd[key]
"""
# The code to be tested
act_value = obj[key]
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
assert act_value == exp_value, "Unexpected value at key %r" % key
TESTCASES_NOCASEDICT_SETITEM = [
# Testcases for NocaseDict.__setitem__() / ncd[key]=value
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * key: Key to be used for the test.
# * value: New value and expected value to be used for the test.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict, with None key",
dict(
obj=NocaseDict(),
key=None,
value=None,
),
None, None, True
),
(
"Empty dict, with integer key (no lower / success)",
dict(
obj=NocaseDict(),
key=1234,
value=None,
),
None if TEST_AGAINST_DICT else TypeError, None, True
),
(
"Empty dict, with empty string key",
dict(
obj=NocaseDict(),
key='',
value='Newbie',
),
None, None, True
),
(
"Empty dict, with non-empty key",
dict(
obj=NocaseDict(),
key='Dog',
value='Kitten',
),
None, None, True
),
# Non-empty NocaseDict
(
"Non-empty dict, with None key",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key=None,
value='Kitten',
),
None, None, True
),
(
"Non-empty dict, with empty string key",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='',
value='Newbie',
),
None, None, True
),
(
"Non-empty dict, with non-empty non-existing key",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='newkey',
value='Newbie',
),
None, None, True
),
(
"Non-empty dict, with existing key in original case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='Dog',
value='Kitten',
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original upper case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='DOG',
value='Kitten',
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original lower case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='dog',
value='Kitten',
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original mixed case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='doG',
value='Kitten',
),
None, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_SETITEM)
@simplified_test_function
def test_NocaseDict_setitem(testcase, obj, key, value):
"""
Test function for NocaseDict.__setitem__() / ncd[key]=value
"""
# The code to be tested
obj[key] = value
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
# The verification below also uses some NocaseDict features, but that is
# unavoidable if we want to work through the public interface:
act_value = obj[key] # Uses NocaseDIct getitem
assert act_value == value, "Unexpected value at key %r" % key
TESTCASES_NOCASEDICT_DELITEM = [
# Testcases for NocaseDict.__delitem__() / del ncd[key]
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * key: Key to be used for the test.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict, with None key (not found)",
dict(
obj=NocaseDict(),
key=None,
),
KeyError, None, True
),
(
"Empty dict, with integer key (no lower / not found)",
dict(
obj=NocaseDict(),
key=1234,
),
KeyError if TEST_AGAINST_DICT else TypeError, None, True
),
(
"Empty dict, with empty string key (not found)",
dict(
obj=NocaseDict(),
key='',
),
KeyError, None, True
),
(
"Empty dict, with non-empty key (not found)",
dict(
obj=NocaseDict(),
key='Dog',
),
KeyError, None, True
),
# Non-empty NocaseDict
(
"Non-empty dict, with None key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key=None,
),
KeyError, None, True
),
(
"Non-empty dict, with empty non-existing string key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='',
),
KeyError, None, True
),
(
"Non-empty dict, with non-empty non-existing key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='invalid',
),
KeyError, None, True
),
(
"Non-empty dict, with existing key in original case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='Dog',
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original upper case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='DOG',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
(
"Non-empty dict, with existing key in non-original lower case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='dog',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
(
"Non-empty dict, with existing key in non-original mixed case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='doG',
),
KeyError if TEST_AGAINST_DICT else None, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_DELITEM)
@simplified_test_function
def test_NocaseDict_delitem(testcase, obj, key):
"""
Test function for NocaseDict.__delitem__() / del ncd[key]
"""
# The code to be tested
del obj[key]
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
# The verification below also uses some NocaseDict features, but that is
# unavoidable if we want to work through the public interface:
with pytest.raises(KeyError):
# The following line uses NocaseDict getitem
obj[key] # pylint: disable=pointless-statement
TESTCASES_NOCASEDICT_LEN = [
# Testcases for NocaseDict.__len__() / len(ncd)
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * exp_len: Expected len() value.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
(
"Empty dict",
dict(
obj=NocaseDict(),
exp_len=0,
),
None, None, True
),
(
"Dict with two items",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
exp_len=2,
),
None, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_LEN)
@simplified_test_function
def test_NocaseDict_len(testcase, obj, exp_len):
"""
Test function for NocaseDict.__len__() / len(ncd)
"""
# The code to be tested
act_len = len(obj)
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
assert act_len == exp_len
TESTCASES_NOCASEDICT_CONTAINS = [
# Testcases for NocaseDict.__contains__() / key in ncd
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * key: Key to be used for the test.
# * exp_result: Expected result (bool).
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict, with None key",
dict(
obj=NocaseDict(),
key=None,
exp_result=False,
),
None, None, True
),
(
"Empty dict, with integer key (no lower / success)",
dict(
obj=NocaseDict(),
key=1234,
exp_result=False if TEST_AGAINST_DICT else None,
),
None if TEST_AGAINST_DICT else TypeError, None, True
),
(
"Empty dict, with empty string key (not found)",
dict(
obj=NocaseDict(),
key='',
exp_result=False,
),
None, None, True
),
(
"Empty dict, with non-empty key (not found)",
dict(
obj=NocaseDict(),
key='Dog',
exp_result=False,
),
None, None, True
),
# Non-empty NocaseDict
(
"Non-empty dict, with non-existing None key",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key=None,
exp_result=False,
),
None, None, True
),
(
"Non-empty dict, with empty non-existing string key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='',
exp_result=False,
),
None, None, True
),
(
"Non-empty dict, with non-empty non-existing key (not found)",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='invalid',
exp_result=False,
),
None, None, True
),
(
"Non-empty dict, with existing key in original case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='Dog',
exp_result=True,
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original upper case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='DOG',
exp_result=not TEST_AGAINST_DICT,
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original lower case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='dog',
exp_result=not TEST_AGAINST_DICT,
),
None, None, True
),
(
"Non-empty dict, with existing key in non-original mixed case",
dict(
obj=NocaseDict([('Dog', 'Cat'), ('Budgie', 'Fish')]),
key='doG',
exp_result=not TEST_AGAINST_DICT,
),
None, None, True
),
]
@pytest.mark.parametrize(
"desc, kwargs, exp_exc_types, exp_warn_types, condition",
TESTCASES_NOCASEDICT_CONTAINS)
@simplified_test_function
def test_NocaseDict_contains(testcase, obj, key, exp_result):
"""
Test function for NocaseDict.__contains__() / key in ncd
"""
# The code to be tested
act_result = key in obj
# Ensure that exceptions raised in the remainder of this function
# are not mistaken as expected exceptions
assert testcase.exp_exc_types is None
assert act_result == exp_result, "Unexpected result at key %r" % key
TESTCASES_NOCASEDICT_GET = [
# Testcases for NocaseDict.get()
# Each list item is a testcase tuple with these items:
# * desc: Short testcase description.
# * kwargs: Keyword arguments for the test function:
# * obj: NocaseDict object to be used for the test.
# * key: Key to be used for the test.
# * default: Default value to be used for the test, or None to not pass.
# * exp_value: Expected value at the key.
# * exp_exc_types: Expected exception type(s), or None.
# * exp_warn_types: Expected warning type(s), or None.
# * condition: Boolean condition for testcase to run, or 'pdb' for debugger
# Empty NocaseDict
(
"Empty dict, with None key",
dict(
obj=NocaseDict(),
key=None,
default=None,
exp_value=None,
),
None, None, True
),
(
"Empty dict, with integer key (no lower / success)",
dict(
obj=NocaseDict(),
key=1234,
default=None,
exp_value=None,
),
None if TEST_AGAINST_DICT else TypeError, None, True
),
(
"Empty dict, with empty string key (defaulted without default)",
dict(
obj=NocaseDict(),
key='',
default=None,
exp_value=None,
),
None, None, True
),
(
"Empty dict, with empty string key (defaulted to a value)",
dict(
obj=NocaseDict(),
key='',
default='Newbie',
exp_value='Newbie',
),
None, None, True
),
(
"Empty dict, with non-empty key (defaulted without default)",
dict(
obj=NocaseDict(),
key='Dog',
default=None,
exp_value=None,
),
None, None, True
),
(
"Empty dict, with non-empty key (defaulted to a value)",