@@ -117,7 +117,8 @@ def test_different_classes(self, input_list: Sequence[object]) -> None:
117117 """If we initialise a ClassList with an input containing multiple classes, we should raise a ValueError."""
118118 with pytest .raises (
119119 ValueError ,
120- match = f"Input list contains elements of type other than '{ type (input_list [0 ]).__name__ } '" ,
120+ match = f"This ClassList only supports elements of type { type (input_list [0 ]).__name__ } . In the input list:\n "
121+ f" index 1 is of type { type (input_list [1 ]).__name__ } \n " ,
121122 ):
122123 ClassList (input_list )
123124
@@ -134,7 +135,9 @@ def test_identical_name_fields(self, input_list: Sequence[object], name_field: s
134135 """
135136 with pytest .raises (
136137 ValueError ,
137- match = f"Input list contains objects with the same value of the { name_field } attribute" ,
138+ match = f"The value of the '{ name_field } ' attribute must be unique for each item in the "
139+ f"ClassList:\n '{ getattr (input_list [0 ], name_field ).lower ()} '"
140+ f" is shared between items 0 and 1 of the input list" ,
138141 ):
139142 ClassList (input_list , name_field = name_field )
140143
@@ -194,7 +197,12 @@ def test_setitem(two_name_class_list: ClassList, new_item: InputAttributes, expe
194197)
195198def test_setitem_same_name_field (two_name_class_list : ClassList , new_item : InputAttributes ) -> None :
196199 """If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
197- with pytest .raises (ValueError , match = "Input list contains objects with the same value of the name attribute" ):
200+ with pytest .raises (
201+ ValueError ,
202+ match = f"The value of the '{ two_name_class_list .name_field } ' attribute must be unique for each item in the "
203+ f"ClassList:\n '{ new_item .name .lower ()} ' is shared between item 1 of the existing ClassList,"
204+ f" and item 0 of the input list" ,
205+ ):
198206 two_name_class_list [0 ] = new_item
199207
200208
@@ -206,7 +214,11 @@ def test_setitem_same_name_field(two_name_class_list: ClassList, new_item: Input
206214)
207215def test_setitem_different_classes (two_name_class_list : ClassList , new_values : dict [str , Any ]) -> None :
208216 """If we set the name_field of an object in the ClassList to one already defined, we should raise a ValueError."""
209- with pytest .raises (ValueError , match = "Input list contains elements of type other than 'InputAttributes'" ):
217+ with pytest .raises (
218+ ValueError ,
219+ match = f"This ClassList only supports elements of type { two_name_class_list ._class_handle .__name__ } . "
220+ f"In the input list:\n index 0 is of type { type (new_values ).__name__ } \n " ,
221+ ):
210222 two_name_class_list [0 ] = new_values
211223
212224
@@ -403,7 +415,9 @@ def test_append_object_same_name_field(two_name_class_list: ClassList, new_objec
403415 """If we append an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
404416 with pytest .raises (
405417 ValueError ,
406- match = f"Input list contains objects with the same value of the " f"{ two_name_class_list .name_field } attribute" ,
418+ match = f"The value of the '{ two_name_class_list .name_field } ' attribute must be unique for each item in the "
419+ f"ClassList:\n '{ new_object .name .lower ()} ' is shared between item 0 of the existing ClassList, and "
420+ f"item 0 of the input list" ,
407421 ):
408422 two_name_class_list .append (new_object )
409423
@@ -420,7 +434,7 @@ def test_append_kwargs_same_name_field(two_name_class_list: ClassList, new_value
420434 ValueError ,
421435 match = f"Input arguments contain the { two_name_class_list .name_field } "
422436 f"'{ new_values [two_name_class_list .name_field ]} ', "
423- f"which is already specified in the ClassList" ,
437+ f"which is already specified at index 0 of the ClassList" ,
424438 ):
425439 two_name_class_list .append (** new_values )
426440
@@ -526,7 +540,9 @@ def test_insert_object_same_name(two_name_class_list: ClassList, new_object: obj
526540 """If we insert an object with an already-specified name_field value to a ClassList we should raise a ValueError."""
527541 with pytest .raises (
528542 ValueError ,
529- match = f"Input list contains objects with the same value of the " f"{ two_name_class_list .name_field } attribute" ,
543+ match = f"The value of the '{ two_name_class_list .name_field } ' attribute must be unique for each item in the "
544+ f"ClassList:\n '{ new_object .name .lower ()} ' is shared between item 0 of the existing "
545+ f"ClassList, and item 0 of the input list" ,
530546 ):
531547 two_name_class_list .insert (1 , new_object )
532548
@@ -543,7 +559,7 @@ def test_insert_kwargs_same_name(two_name_class_list: ClassList, new_values: dic
543559 ValueError ,
544560 match = f"Input arguments contain the { two_name_class_list .name_field } "
545561 f"'{ new_values [two_name_class_list .name_field ]} ', "
546- f"which is already specified in the ClassList" ,
562+ f"which is already specified at index 0 of the ClassList" ,
547563 ):
548564 two_name_class_list .insert (1 , ** new_values )
549565
@@ -702,7 +718,7 @@ def test_set_fields_same_name_field(two_name_class_list: ClassList, new_values:
702718 ValueError ,
703719 match = f"Input arguments contain the { two_name_class_list .name_field } "
704720 f"'{ new_values [two_name_class_list .name_field ]} ', "
705- f"which is already specified in the ClassList" ,
721+ f"which is already specified at index 1 of the ClassList" ,
706722 ):
707723 two_name_class_list .set_fields (0 , ** new_values )
708724
@@ -767,7 +783,7 @@ def test__validate_name_field(two_name_class_list: ClassList, input_dict: dict[s
767783 "input_dict" ,
768784 [
769785 ({"name" : "Alice" }),
770- ({"name" : "ALICE " }),
786+ ({"name" : "BOB " }),
771787 ({"name" : "alice" }),
772788 ],
773789)
@@ -777,18 +793,18 @@ def test__validate_name_field_not_unique(two_name_class_list: ClassList, input_d
777793 with pytest .raises (
778794 ValueError ,
779795 match = f"Input arguments contain the { two_name_class_list .name_field } "
780- f"'{ input_dict [two_name_class_list .name_field ]} ', "
781- f"which is already specified in the ClassList" ,
796+ f"'{ input_dict [two_name_class_list .name_field ]} ', which is already specified at index "
797+ f"{ two_name_class_list . index ( input_dict [ 'name' ]. lower ()) } of the ClassList" ,
782798 ):
783799 two_name_class_list ._validate_name_field (input_dict )
784800
785801
786802@pytest .mark .parametrize (
787803 "input_list" ,
788804 [
789- ([InputAttributes (name = "Alice " ), InputAttributes (name = "Bob " )]),
790- ([InputAttributes (surname = "Morgan " ), InputAttributes (surname = "Terwilliger " )]),
791- ([InputAttributes (name = "Alice " , surname = "Morgan " ), InputAttributes (surname = "Terwilliger " )]),
805+ ([InputAttributes (name = "Eve " ), InputAttributes (name = "Gareth " )]),
806+ ([InputAttributes (surname = "Polastri " ), InputAttributes (surname = "Mallory " )]),
807+ ([InputAttributes (name = "Eve " , surname = "Polastri " ), InputAttributes (surname = "Mallory " )]),
792808 ([InputAttributes ()]),
793809 ([]),
794810 ],
@@ -801,20 +817,59 @@ def test__check_unique_name_fields(two_name_class_list: ClassList, input_list: I
801817
802818
803819@pytest .mark .parametrize (
804- "input_list" ,
820+ [ "input_list" , "error_message" ] ,
805821 [
806- ([InputAttributes (name = "Alice" ), InputAttributes (name = "Alice" )]),
807- ([InputAttributes (name = "Alice" ), InputAttributes (name = "ALICE" )]),
808- ([InputAttributes (name = "Alice" ), InputAttributes (name = "alice" )]),
822+ (
823+ [InputAttributes (name = "Alice" ), InputAttributes (name = "Bob" )],
824+ (
825+ " 'alice' is shared between item 0 of the existing ClassList, and item 0 of the input list\n "
826+ " 'bob' is shared between item 1 of the existing ClassList, and item 1 of the input list"
827+ ),
828+ ),
829+ (
830+ [InputAttributes (name = "Alice" ), InputAttributes (name = "Alice" )],
831+ " 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list" ,
832+ ),
833+ (
834+ [InputAttributes (name = "Alice" ), InputAttributes (name = "ALICE" )],
835+ " 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list" ,
836+ ),
837+ (
838+ [InputAttributes (name = "Alice" ), InputAttributes (name = "alice" )],
839+ " 'alice' is shared between item 0 of the existing ClassList, and items 0 and 1 of the input list" ,
840+ ),
841+ (
842+ [InputAttributes (name = "Eve" ), InputAttributes (name = "Eve" )],
843+ " 'eve' is shared between items 0 and 1 of the input list" ,
844+ ),
845+ (
846+ [
847+ InputAttributes (name = "Bob" ),
848+ InputAttributes (name = "Alice" ),
849+ InputAttributes (name = "Eve" ),
850+ InputAttributes (name = "Alice" ),
851+ InputAttributes (name = "Eve" ),
852+ InputAttributes (name = "Alice" ),
853+ ],
854+ (
855+ " 'bob' is shared between item 1 of the existing ClassList, and item 0 of the input list\n "
856+ " 'alice' is shared between item 0 of the existing ClassList,"
857+ " and items 1, 3 and 5 of the input list\n "
858+ " 'eve' is shared between items 2 and 4 of the input list"
859+ ),
860+ ),
809861 ],
810862)
811- def test__check_unique_name_fields_not_unique (two_name_class_list : ClassList , input_list : Iterable ) -> None :
863+ def test__check_unique_name_fields_not_unique (
864+ two_name_class_list : ClassList , input_list : Sequence , error_message : str
865+ ) -> None :
812866 """We should raise a ValueError if an input list contains multiple objects with (case-insensitive) matching
813867 name_field values defined.
814868 """
815869 with pytest .raises (
816870 ValueError ,
817- match = f"Input list contains objects with the same value of the " f"{ two_name_class_list .name_field } attribute" ,
871+ match = f"The value of the '{ two_name_class_list .name_field } ' attribute must be unique for each item in the "
872+ f"ClassList:\n { error_message } " ,
818873 ):
819874 two_name_class_list ._check_unique_name_fields (input_list )
820875
@@ -837,12 +892,15 @@ def test__check_classes(input_list: Iterable) -> None:
837892 ([InputAttributes (name = "Alice" ), dict (name = "Bob" )]),
838893 ],
839894)
840- def test__check_classes_different_classes (input_list : Iterable ) -> None :
895+ def test__check_classes_different_classes (input_list : Sequence ) -> None :
841896 """We should raise a ValueError if an input list contains objects of different types."""
842897 class_list = ClassList ([InputAttributes ()])
843898 with pytest .raises (
844899 ValueError ,
845- match = (f"Input list contains elements of type other " f"than '{ class_list ._class_handle .__name__ } '" ),
900+ match = (
901+ f"This ClassList only supports elements of type { class_list ._class_handle .__name__ } . "
902+ f"In the input list:\n index 1 is of type { type (input_list [1 ]).__name__ } "
903+ ),
846904 ):
847905 class_list ._check_classes (input_list )
848906
0 commit comments