forked from RascalSoftware/python-RAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_classlist.py
More file actions
1029 lines (896 loc) · 37.2 KB
/
test_classlist.py
File metadata and controls
1029 lines (896 loc) · 37.2 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
"""Test the ClassList."""
import importlib
import re
import warnings
from collections import deque
from collections.abc import Iterable, Sequence
from typing import Any, Union
import pytest
from RATapi.classlist import ClassList
from tests.utils import InputAttributes, SubInputAttributes
@pytest.fixture
def one_name_class_list():
"""A ClassList of InputAttributes, containing one element with a name defined."""
return ClassList([InputAttributes(name="Alice")])
@pytest.fixture
def two_name_class_list():
"""A ClassList of InputAttributes, containing two elements with names defined."""
return ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")])
@pytest.fixture
def two_name_class_list_table():
"""The table representation of the ClassList defined in the "two_name_class_list" fixture."""
return (
"+-------+-------+\n"
"| index | name |\n"
"+-------+-------+\n"
"| 0 | Alice |\n"
"| 1 | Bob |\n"
"+-------+-------+"
)
@pytest.fixture
def three_name_class_list():
"""A ClassList of InputAttributes, containing three elements with names defined."""
return ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob"), InputAttributes(name="Eve")])
class TestInitialisation:
@pytest.mark.parametrize(
"input_object",
[
(InputAttributes()),
(InputAttributes(name="Alice")),
(InputAttributes(surname="Morgan")),
"Alice",
],
)
def test_input_object(self, input_object: Any) -> None:
"""For an input of an object, the ClassList should contain a one-element list containing the object and
_class_handle should be set to the type of the object.
"""
class_list = ClassList(input_object)
assert class_list.data == [input_object]
assert isinstance(input_object, class_list._class_handle)
@pytest.mark.parametrize(
"input_sequence",
[
([InputAttributes()]),
([InputAttributes(name="Alice")]),
([InputAttributes(surname="Morgan")]),
([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
(InputAttributes(),),
(InputAttributes(name="Alice"),),
(InputAttributes(surname="Morgan"),),
(InputAttributes(name="Alice"), InputAttributes(name="Bob")),
],
)
def test_input_sequence(self, input_sequence: Sequence[object]) -> None:
"""For an input of a sequence, the ClassList should be a list equal to the input sequence, and _class_handle
should be set to the type of the objects within.
"""
class_list = ClassList(input_sequence)
assert class_list.data == list(input_sequence)
for element in input_sequence:
assert isinstance(element, class_list._class_handle)
@pytest.mark.parametrize(
"input_sequence",
[
([InputAttributes(name="Alice"), SubInputAttributes(name="Bob")]),
([SubInputAttributes(name="Alice"), InputAttributes(name="Bob")]),
],
)
def test_input_sequence_subclass(self, input_sequence: Sequence[object]) -> None:
"""For an input of a sequence containing objects of a class and its subclasses, the ClassList should be a list
equal to the input sequence, and _class_handle should be set to the type of the parent class.
"""
class_list = ClassList(input_sequence)
assert class_list.data == list(input_sequence)
for element in input_sequence:
assert isinstance(element, class_list._class_handle)
@pytest.mark.parametrize("empty_input", [([]), (())])
def test_empty_input(self, empty_input: Sequence[object]) -> None:
"""If we initialise a ClassList with an empty input (list or tuple), the ClassList should be an empty list, and
_class_handle should be unset.
"""
class_list = ClassList(empty_input)
assert class_list.data == []
assert not hasattr(class_list, "_class_handle")
@pytest.mark.parametrize(
"input_list",
[
([InputAttributes(name="Alice"), dict(name="Bob")]),
],
)
def test_different_classes(self, input_list: Sequence[object]) -> None:
"""If we initialise a ClassList with an input containing multiple classes, we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"This ClassList only supports elements of type {type(input_list[0]).__name__}. In the input list:\n"
f" index 1 is of type {type(input_list[1]).__name__}\n",
):
ClassList(input_list)
@pytest.mark.parametrize(
"input_list, name_field",
[
([InputAttributes(name="Alice"), InputAttributes(name="Alice")], "name"),
([InputAttributes(id="Alice"), InputAttributes(id="Alice")], "id"),
],
)
def test_identical_name_fields(self, input_list: Sequence[object], name_field: str) -> None:
"""If we initialise a ClassList with input objects with identical values of the name_field,
we should raise a ValueError.
"""
with pytest.raises(
ValueError,
match=f"The value of the '{name_field}' attribute must be unique for each item in the "
f"ClassList:\n '{getattr(input_list[0], name_field).lower()}'"
f" is shared between items 0 and 1 of the input list",
):
ClassList(input_list, name_field=name_field)
def test_str_table(two_name_class_list: ClassList, two_name_class_list_table: str) -> None:
"""For classes with the __dict__ attribute, we should be able to print the ClassList like a table."""
assert str(two_name_class_list) == two_name_class_list_table
def test_str_empty_table() -> None:
"""For empty classes with the __dict__ attribute, we should be able to print the contents of the ClassList as a
list.
"""
empty_attributes = InputAttributes()
assert str(ClassList(empty_attributes)) == str([empty_attributes])
@pytest.mark.parametrize(
"input_list",
[
(["Alice", "Bob"]),
],
)
def test_str_list(input_list: list[str]) -> None:
"""For classes without the __dict__ attribute, we should be able to print the ClassList as a list."""
class_list = ClassList(input_list)
assert str(class_list) == str(input_list)
def test_str_empty_classlist() -> None:
"""For empty ClassLists, we should be able to print the ClassList as an empty list."""
assert str(ClassList()) == str([])
@pytest.mark.parametrize(
["new_item", "expected_classlist"],
[
(InputAttributes(name="Eve"), ClassList([InputAttributes(name="Eve"), InputAttributes(name="Bob")])),
(
InputAttributes(name="John", surname="Luther"),
ClassList([InputAttributes(name="John", surname="Luther"), InputAttributes(name="Bob")]),
),
],
)
def test_setitem(two_name_class_list: ClassList, new_item: InputAttributes, expected_classlist: ClassList) -> None:
"""We should be able to set values in an element of a ClassList using a new object."""
class_list = two_name_class_list
class_list[0] = new_item
assert class_list == expected_classlist
@pytest.mark.parametrize(
"new_item",
[
(InputAttributes(name="Bob")),
],
)
def test_setitem_same_name_field(two_name_class_list: ClassList, new_item: InputAttributes) -> None:
"""If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"The value of the '{two_name_class_list.name_field}' attribute must be unique for each item in the "
f"ClassList:\n '{new_item.name.lower()}' is shared between item 1 of the existing ClassList,"
f" and item 0 of the input list",
):
two_name_class_list[0] = new_item
@pytest.mark.parametrize(
"new_values",
[
"Bob",
],
)
def test_setitem_different_classes(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
"""If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"This ClassList only supports elements of type {two_name_class_list._class_handle.__name__}. "
f"In the input list:\n index 0 is of type {type(new_values).__name__}\n",
):
two_name_class_list[0] = new_values
def test_delitem(two_name_class_list: ClassList, one_name_class_list: ClassList) -> None:
"""We should be able to delete elements from a ClassList with the del operator."""
class_list = two_name_class_list
del class_list[1]
assert class_list == one_name_class_list
def test_delitem_not_present(two_name_class_list: ClassList) -> None:
"""If we use the del operator to delete an index out of range, we should raise an IndexError."""
class_list = two_name_class_list
with pytest.raises(IndexError, match=re.escape("list assignment index out of range")):
del class_list[2]
@pytest.mark.parametrize(
"added_list",
[
(ClassList(InputAttributes(name="Eve"))),
([InputAttributes(name="Eve")]),
(InputAttributes(name="Eve"),),
(InputAttributes(name="Eve")),
],
)
def test_iadd(two_name_class_list: ClassList, added_list: Iterable, three_name_class_list: ClassList) -> None:
"""We should be able to use the "+=" operator to add iterables to a ClassList. Individual objects should be wrapped
in a list before being added.
"""
class_list = two_name_class_list
class_list += added_list
assert class_list == three_name_class_list
@pytest.mark.parametrize(
"added_list",
[
(ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")])),
([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
(InputAttributes(name="Alice"), InputAttributes(name="Bob")),
],
)
def test_iadd_empty_classlist(added_list: Sequence, two_name_class_list: ClassList) -> None:
"""We should be able to use the "+=" operator to add iterables to an empty ClassList, whilst also setting
_class_handle.
"""
class_list = ClassList()
class_list += added_list
assert class_list == two_name_class_list
assert isinstance(added_list[-1], class_list._class_handle)
def test_mul(two_name_class_list: ClassList) -> None:
"""If we use the "*" operator on a ClassList, we should raise a TypeError."""
n = 2
with pytest.raises(
TypeError,
match=re.escape(
f"unsupported operand type(s) for *: "
f"'{two_name_class_list.__class__.__name__}' and "
f"'{n.__class__.__name__}'",
),
):
two_name_class_list * n
def test_rmul(two_name_class_list: ClassList) -> None:
"""If we use the "*" operator on a ClassList, we should raise a TypeError."""
n = 2
with pytest.raises(
TypeError,
match=re.escape(
f"unsupported operand type(s) for *: "
f"'{n.__class__.__name__}' and "
f"'{two_name_class_list.__class__.__name__}'",
),
):
n * two_name_class_list
def test_imul(two_name_class_list: ClassList) -> None:
"""If we use the "*=" operator on a ClassList, we should raise a TypeError."""
n = 2
with pytest.raises(
TypeError,
match=re.escape(
f"unsupported operand type(s) for *=: "
f"'{two_name_class_list.__class__.__name__}' and "
f"'{n.__class__.__name__}'",
),
):
two_name_class_list *= n
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Eve")),
],
)
def test_append_object(two_name_class_list: ClassList, new_object: object, three_name_class_list: ClassList) -> None:
"""We should be able to append to a ClassList using a new object."""
class_list = two_name_class_list
class_list.append(new_object)
assert class_list == three_name_class_list
@pytest.mark.parametrize(
"new_values",
[
({"name": "Eve"}),
],
)
def test_append_kwargs(
two_name_class_list: ClassList,
new_values: dict[str, Any],
three_name_class_list: ClassList,
) -> None:
"""We should be able to append to a ClassList using keyword arguments."""
class_list = two_name_class_list
class_list.append(**new_values)
assert class_list == three_name_class_list
@pytest.mark.parametrize(
["new_object", "new_values"],
[
(InputAttributes(name="Eve"), {"name": "John"}),
],
)
def test_append_object_and_kwargs(
two_name_class_list: ClassList,
new_object: object,
new_values: dict[str, Any],
three_name_class_list: ClassList,
) -> None:
"""If we append to a ClassList using a new object and keyword arguments, we raise a warning, and append the object,
discarding the keyword arguments.
"""
class_list = two_name_class_list
with pytest.warns(SyntaxWarning):
warnings.warn(
"ClassList.append() called with both an object and keyword arguments. "
"The keyword arguments will be ignored.",
SyntaxWarning,
stacklevel=2,
)
class_list.append(new_object, **new_values)
assert class_list == three_name_class_list
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Alice")),
],
)
def test_append_object_empty_classlist(new_object: object, one_name_class_list: ClassList) -> None:
"""We should be able to append to an empty ClassList using a new object, whilst also setting _class_handle."""
class_list = ClassList()
class_list.append(new_object)
assert class_list == one_name_class_list
assert isinstance(new_object, class_list._class_handle)
@pytest.mark.parametrize(
"new_values",
[
({"name": "Alice"}),
],
)
def test_append_kwargs_empty_classlist(new_values: dict[str, Any]) -> None:
"""If we append to an empty ClassList using keyword arguments we should raise a TypeError."""
class_list = ClassList()
with pytest.raises(
TypeError,
match=re.escape(
"ClassList.append() called with keyword arguments for a ClassList "
"without a class defined. Call ClassList.append() with an object to "
"define the class.",
),
):
class_list.append(**new_values)
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Alice")),
],
)
def test_append_object_same_name_field(two_name_class_list: ClassList, new_object: object) -> None:
"""If we append an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"The value of the '{two_name_class_list.name_field}' attribute must be unique for each item in the "
f"ClassList:\n '{new_object.name.lower()}' is shared between item 0 of the existing ClassList, and "
f"item 0 of the input list",
):
two_name_class_list.append(new_object)
@pytest.mark.parametrize(
"new_values",
[
({"name": "Alice"}),
],
)
def test_append_kwargs_same_name_field(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
"""If we append an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"Input arguments contain the {two_name_class_list.name_field} "
f"'{new_values[two_name_class_list.name_field]}', "
f"which is already specified at index 0 of the ClassList",
):
two_name_class_list.append(**new_values)
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Eve")),
],
)
def test_insert_object(two_name_class_list: ClassList, new_object: object) -> None:
"""We should be able to insert an object within a ClassList using a new object."""
two_name_class_list.insert(1, new_object)
assert two_name_class_list == ClassList(
[InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
)
@pytest.mark.parametrize(
"new_values",
[
({"name": "Eve"}),
],
)
def test_insert_kwargs(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
"""We should be able to insert an object within a ClassList using keyword arguments."""
two_name_class_list.insert(1, **new_values)
assert two_name_class_list == ClassList(
[InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
)
@pytest.mark.parametrize(
["new_object", "new_values"],
[
(InputAttributes(name="Eve"), {"name": "John"}),
],
)
def test_insert_object_and_kwargs(
two_name_class_list: ClassList,
new_object: object,
new_values: dict[str, Any],
three_name_class_list: ClassList,
) -> None:
"""If call insert() on a ClassList using a new object and keyword arguments, we raise a warning, and append the
object, discarding the keyword arguments.
"""
class_list = two_name_class_list
with pytest.warns(SyntaxWarning):
warnings.warn(
"ClassList.insert() called with both an object and keyword arguments. "
"The keyword arguments will be ignored.",
SyntaxWarning,
stacklevel=2,
)
class_list.insert(1, new_object, **new_values)
assert class_list == ClassList(
[InputAttributes(name="Alice"), InputAttributes(name="Eve"), InputAttributes(name="Bob")],
)
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Alice")),
],
)
def test_insert_object_empty_classlist(new_object: object, one_name_class_list: ClassList) -> None:
"""We should be able to insert a new object into an empty ClassList, whilst also setting _class_handle."""
class_list = ClassList()
class_list.insert(0, new_object)
assert class_list == one_name_class_list
assert isinstance(new_object, class_list._class_handle)
@pytest.mark.parametrize(
"new_values",
[
({"name": "Alice"}),
],
)
def test_insert_kwargs_empty_classlist(new_values: dict[str, Any]) -> None:
"""If we append to an empty ClassList using keyword arguments we should raise a TypeError."""
class_list = ClassList()
with pytest.raises(
TypeError,
match=re.escape(
"ClassList.insert() called with keyword arguments for a ClassList "
"without a class defined. Call ClassList.insert() with an object to "
"define the class.",
),
):
class_list.insert(0, **new_values)
@pytest.mark.parametrize(
"new_object",
[
(InputAttributes(name="Alice")),
],
)
def test_insert_object_same_name(two_name_class_list: ClassList, new_object: object) -> None:
"""If we insert an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"The value of the '{two_name_class_list.name_field}' attribute must be unique for each item in the "
f"ClassList:\n '{new_object.name.lower()}' is shared between item 0 of the existing "
f"ClassList, and item 0 of the input list",
):
two_name_class_list.insert(1, new_object)
@pytest.mark.parametrize(
"new_values",
[
({"name": "Alice"}),
],
)
def test_insert_kwargs_same_name(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
"""If we insert an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"Input arguments contain the {two_name_class_list.name_field} "
f"'{new_values[two_name_class_list.name_field]}', "
f"which is already specified at index 0 of the ClassList",
):
two_name_class_list.insert(1, **new_values)
@pytest.mark.parametrize(
"remove_value",
[
"Bob",
(InputAttributes(name="Bob")),
],
)
def test_remove(two_name_class_list: ClassList, remove_value: Union[object, str]) -> None:
"""We should be able to remove an object either by the value of the name_field or by specifying the object
itself.
"""
two_name_class_list.remove(remove_value)
assert two_name_class_list == ClassList([InputAttributes(name="Alice")])
@pytest.mark.parametrize(
"remove_value",
[
"Eve",
(InputAttributes(name="Eve")),
],
)
def test_remove_not_present(two_name_class_list: ClassList, remove_value: Union[object, str]) -> None:
"""If we remove an object not included in the ClassList we should raise a ValueError."""
with pytest.raises(ValueError, match=re.escape("list.remove(x): x not in list")):
two_name_class_list.remove(remove_value)
@pytest.mark.parametrize(
["count_value", "expected_count"],
[
("Bob", 1),
(InputAttributes(name="Bob"), 1),
("Eve", 0),
(InputAttributes(name="Eve"), 0),
],
)
def test_count(two_name_class_list: ClassList, count_value: Union[object, str], expected_count: int) -> None:
"""We should be able to determine the number of times an object is in the ClassList using either the object itself
or its name_field value.
"""
assert two_name_class_list.count(count_value) == expected_count
@pytest.mark.parametrize(
["index_value", "expected_index"],
[
("Bob", 1),
(InputAttributes(name="Bob"), 1),
],
)
def test_index(two_name_class_list: ClassList, index_value: Union[object, str], expected_index: int) -> None:
"""We should be able to find the index of an object in the ClassList either by its name_field value or by
specifying the object itself.
"""
assert two_name_class_list.index(index_value) == expected_index
@pytest.mark.parametrize(
["index_value", "offset", "expected_index"],
[
("Bob", 1, 2),
(InputAttributes(name="Bob"), -3, -2),
],
)
def test_index_offset(
two_name_class_list: ClassList,
index_value: Union[object, str],
offset: int,
expected_index: int,
) -> None:
"""We should be able to find the index of an object in the ClassList either by its name_field value or by
specifying the object itself. When using an offset, the value of the index should be shifted accordingly.
"""
assert two_name_class_list.index(index_value, offset) == expected_index
@pytest.mark.parametrize(
"index_value",
[
"Eve",
(InputAttributes(name="Eve")),
],
)
def test_index_not_present(two_name_class_list: ClassList, index_value: Union[object, str]) -> None:
"""If we try to find the index of an object not included in the ClassList we should raise a ValueError."""
# with pytest.raises(ValueError, match=f"'{index_value}' is not in list") as e:
with pytest.raises(ValueError):
two_name_class_list.index(index_value)
@pytest.mark.parametrize(
"extended_list",
[
(ClassList(InputAttributes(name="Eve"))),
([InputAttributes(name="Eve")]),
(InputAttributes(name="Eve"),),
(InputAttributes(name="Eve")),
],
)
def test_extend(two_name_class_list: ClassList, extended_list: Sequence, three_name_class_list: ClassList) -> None:
"""We should be able to extend a ClassList using another ClassList or a sequence. Individual objects should be
wrapped in a list before being added.
"""
class_list = two_name_class_list
class_list.extend(extended_list)
assert class_list == three_name_class_list
@pytest.mark.parametrize(
"extended_list",
[
(ClassList(InputAttributes(name="Alice"))),
([InputAttributes(name="Alice")]),
(InputAttributes(name="Alice"),),
],
)
def test_extend_empty_classlist(extended_list: Sequence, one_name_class_list: ClassList) -> None:
"""We should be able to extend a ClassList using another ClassList or a sequence"""
class_list = ClassList()
class_list.extend(extended_list)
assert class_list == one_name_class_list
assert isinstance(extended_list[-1], class_list._class_handle)
@pytest.mark.parametrize(
["new_values", "expected_classlist"],
[
({"name": "Eve"}, ClassList([InputAttributes(name="Eve"), InputAttributes(name="Bob")])),
(
{"name": "John", "surname": "Luther"},
ClassList([InputAttributes(name="John", surname="Luther"), InputAttributes(name="Bob")]),
),
],
)
def test_set_fields(two_name_class_list: ClassList, new_values: dict[str, Any], expected_classlist: ClassList) -> None:
"""We should be able to set field values in an element of a ClassList using keyword arguments."""
class_list = two_name_class_list
class_list.set_fields(0, **new_values)
assert class_list == expected_classlist
@pytest.mark.parametrize(
"new_values",
[
({"name": "Bob"}),
],
)
def test_set_fields_same_name_field(two_name_class_list: ClassList, new_values: dict[str, Any]) -> None:
"""If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
with pytest.raises(
ValueError,
match=f"Input arguments contain the {two_name_class_list.name_field} "
f"'{new_values[two_name_class_list.name_field]}', "
f"which is already specified at index 1 of the ClassList",
):
two_name_class_list.set_fields(0, **new_values)
@pytest.mark.parametrize(
["class_list", "expected_names"],
[
(ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")]), ["Alice", "Bob"]),
(ClassList([InputAttributes(id="Alice"), InputAttributes(id="Bob")], name_field="id"), ["Alice", "Bob"]),
(ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")], name_field="id"), []),
(ClassList([InputAttributes(surname="Morgan"), InputAttributes(surname="Terwilliger")]), []),
(
ClassList([InputAttributes(name="Alice", surname="Morgan"), InputAttributes(surname="Terwilliger")]),
["Alice"],
),
(
ClassList(
[InputAttributes(name="Alice", surname="Morgan"), InputAttributes(surname="Terwilliger")],
name_field="surname",
),
["Morgan", "Terwilliger"],
),
(ClassList(InputAttributes()), []),
],
)
def test_get_names(class_list: ClassList, expected_names: list[str]) -> None:
"""We should get a list of the values of the name_field attribute from each object with it defined in the
ClassList.
"""
assert class_list.get_names() == expected_names
@pytest.mark.parametrize(
["class_list", "expected_matches"],
[
(ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob")]), [(0, "name")]),
(ClassList([InputAttributes(name="Alice"), InputAttributes(name="Bob", id="Alice")]), [(0, "name"), (1, "id")]),
(ClassList([InputAttributes(surname="Morgan"), InputAttributes(surname="Terwilliger")]), []),
(ClassList(InputAttributes()), []),
],
)
def test_get_all_matches(class_list: ClassList, expected_matches: list[tuple]) -> None:
"""We should get a list of (index, field) tuples matching the given value in the ClassList."""
assert class_list.get_all_matches("Alice") == expected_matches
@pytest.mark.parametrize(
"input_dict",
[
({"name": "Eve"}),
({"surname": "Polastri"}),
],
)
def test__validate_name_field(two_name_class_list: ClassList, input_dict: dict[str, Any]) -> None:
"""We should not raise an error if the input values do not contain a name_field value defined in an object in the
ClassList.
"""
assert two_name_class_list._validate_name_field(input_dict) is None
@pytest.mark.parametrize(
"input_dict",
[
({"name": "Alice"}),
({"name": "BOB"}),
({"name": "alice"}),
],
)
def test__validate_name_field_not_unique(two_name_class_list: ClassList, input_dict: dict[str, Any]) -> None:
"""We should raise a ValueError if we input values containing a name_field defined in an object in the ClassList,
accounting for case sensitivity."""
with pytest.raises(
ValueError,
match=f"Input arguments contain the {two_name_class_list.name_field} "
f"'{input_dict[two_name_class_list.name_field]}', which is already specified at index "
f"{two_name_class_list.index(input_dict['name'].lower())} of the ClassList",
):
two_name_class_list._validate_name_field(input_dict)
@pytest.mark.parametrize(
"input_list",
[
([InputAttributes(name="Eve"), InputAttributes(name="Gareth")]),
([InputAttributes(surname="Polastri"), InputAttributes(surname="Mallory")]),
([InputAttributes(name="Eve", surname="Polastri"), InputAttributes(surname="Mallory")]),
([InputAttributes()]),
([]),
],
)
def test__check_unique_name_fields(two_name_class_list: ClassList, input_list: Iterable) -> None:
"""We should not raise an error if an input list contains objects with different name_field values, or if the
name_field is not defined.
"""
assert two_name_class_list._check_unique_name_fields(input_list) is None
@pytest.mark.parametrize(
["input_list", "error_message"],
[
(
[InputAttributes(name="Alice"), InputAttributes(name="Bob")],
(
" 'alice' is shared between item 0 of the existing ClassList, and item 0 of the input list\n"
" 'bob' is shared between item 1 of the existing ClassList, and item 1 of the input list"
),
),
(
[InputAttributes(name="Alice"), InputAttributes(name="Alice")],
" 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list",
),
(
[InputAttributes(name="Alice"), InputAttributes(name="ALICE")],
" 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list",
),
(
[InputAttributes(name="Alice"), InputAttributes(name="alice")],
" 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list",
),
(
[InputAttributes(name="Eve"), InputAttributes(name="Eve")],
" 'eve' is shared between items 0 and 1 of the input list",
),
(
[
InputAttributes(name="Bob"),
InputAttributes(name="Alice"),
InputAttributes(name="Eve"),
InputAttributes(name="Alice"),
InputAttributes(name="Eve"),
InputAttributes(name="Alice"),
],
(
" 'bob' is shared between item 1 of the existing ClassList, and item 0 of the input list\n"
" 'alice' is shared between item 0 of the existing ClassList,"
" and items 1, 3 and 5 of the input list\n"
" 'eve' is shared between items 2 and 4 of the input list"
),
),
],
)
def test__check_unique_name_fields_not_unique(
two_name_class_list: ClassList, input_list: Sequence, error_message: str
) -> None:
"""We should raise a ValueError if an input list contains multiple objects with (case-insensitive) matching
name_field values defined.
"""
with pytest.raises(
ValueError,
match=f"The value of the '{two_name_class_list.name_field}' attribute must be unique for each item in the "
f"ClassList:\n{error_message}",
):
two_name_class_list._check_unique_name_fields(input_list)
@pytest.mark.parametrize(
"input_list",
[
([InputAttributes(name="Alice"), InputAttributes(name="Bob")]),
],
)
def test__check_classes(input_list: Iterable) -> None:
"""We should not raise an error all objects in the ClassList are of the same type."""
class_list = ClassList([InputAttributes()])
assert class_list._check_classes(input_list) is None
@pytest.mark.parametrize(
"input_list",
[
([InputAttributes(name="Alice"), dict(name="Bob")]),
],
)
def test__check_classes_different_classes(input_list: Sequence) -> None:
"""We should raise a ValueError if an input list contains objects of different types."""
class_list = ClassList([InputAttributes()])
with pytest.raises(
ValueError,
match=(
f"This ClassList only supports elements of type {class_list._class_handle.__name__}. "
f"In the input list:\n index 1 is of type {type(input_list[1]).__name__}"
),
):
class_list._check_classes(input_list)
@pytest.mark.parametrize(
["value", "expected_output"],
[
("Alice", InputAttributes(name="Alice")),
("ALICE", InputAttributes(name="Alice")),
("alice", InputAttributes(name="Alice")),
("Eve", "Eve"),
("EVE", "EVE"),
("eve", "eve"),
],
)
def test__get_item_from_name_field(
two_name_class_list: ClassList,
value: str,
expected_output: Union[object, str],
) -> None:
"""When we input the name_field value of an object defined in the ClassList, we should return the object.
If the value is not the name_field of an object defined in the ClassList, we should return the value.
"""
assert two_name_class_list._get_item_from_name_field(value) == expected_output
@pytest.mark.parametrize(
["input_list", "expected_type"],
[
([InputAttributes(name="Alice")], InputAttributes),
([InputAttributes(name="Alice"), SubInputAttributes(name="Bob")], InputAttributes),
([SubInputAttributes(name="Alice"), InputAttributes(name="Bob")], InputAttributes),
([SubInputAttributes(name="Alice"), SubInputAttributes(name="Bob")], SubInputAttributes),
(
[SubInputAttributes(name="Alice"), SubInputAttributes(name="Bob"), InputAttributes(name="Eve")],
InputAttributes,
),
([InputAttributes(name="Alice"), dict(name="Bob")], InputAttributes),
([dict(name="Alice"), InputAttributes(name="Bob")], dict),
],
)
def test_determine_class_handle(input_list: ClassList, expected_type: type) -> None:
"""The _class_handle for the ClassList should be the type that satisfies the condition "isinstance(element, type)"
for all elements in the ClassList.
"""
assert ClassList._determine_class_handle(input_list) == expected_type
def test_get_item(two_name_class_list):
"""Test item names can be gotten by name, index, object or slice."""
assert two_name_class_list[0] == two_name_class_list["Alice"]
assert two_name_class_list[1] == two_name_class_list["Bob"]
assert two_name_class_list[:] == two_name_class_list
alice = InputAttributes(name="Alice")
assert two_name_class_list[alice] == two_name_class_list["Alice"]
assert two_name_class_list[alice] == two_name_class_list[0]
@pytest.mark.skipif(importlib.util.find_spec("pydantic") is None, reason="Pydantic not installed")
class TestPydantic:
"""Tests for the Pydantic integration for ClassLists."""
import pydantic
class Model(pydantic.BaseModel):
classlist: ClassList[str]
model_class = Model
@pytest.mark.parametrize(
"sequence", [["a", "b", "c"], ("a", "b", "c", "d"), deque(["a", "b", "c"]), ClassList(["a", "b"])]
)
def test_sequence_coercion(self, sequence):
"""Test that sequences are coerced to ClassLists."""
model = self.model_class(classlist=sequence)
assert model.classlist.data == list(sequence)
assert model.classlist._class_handle is type(sequence[0])
@pytest.mark.parametrize("input", [[1, 2, "string"], deque(["a", "b", 5.3]), [3.0, 4]])
def test_coerce_bad_inputs(self, input):
"""Test that Pydantic successfully checks the type for input sequences."""
with pytest.raises(self.pydantic.ValidationError, match="This ClassList only supports elements of type str."):
self.model_class(classlist=input)
def test_coerce_models(self):
"""Test that a ClassList of pydantic Models is coerced from a list of dicts."""
import pydantic
class SubModel(pydantic.BaseModel):
i: int
s: str
f: float
class NestedModel(pydantic.BaseModel):
submodels: ClassList[SubModel]