@@ -1032,3 +1032,146 @@ def test_model_level_cascading_deletion(self, mailbox):
1032
1032
assert not models .Label .objects .filter (name = "Work/Meetings" ).exists ()
1033
1033
assert not models .Label .objects .filter (name = "Work/Projects/Urgent" ).exists ()
1034
1034
assert models .Label .objects .count () == 0
1035
+
1036
+ def test_list_labels_alphabetical_order_by_slug (self , api_client , mailbox , user ):
1037
+ """Test that labels are returned in alphabetical order by slug."""
1038
+ # Create labels in random order to test ordering
1039
+ LabelFactory (mailbox = mailbox , name = "Zebra" )
1040
+ LabelFactory (mailbox = mailbox , name = "Alpha" )
1041
+ LabelFactory (mailbox = mailbox , name = "Charlie" )
1042
+ LabelFactory (mailbox = mailbox , name = "Beta" )
1043
+
1044
+ url = reverse ("labels-list" )
1045
+ response = api_client .get (url )
1046
+ assert response .status_code == status .HTTP_200_OK
1047
+ data = response .json ()
1048
+
1049
+ # Verify labels are ordered alphabetically by slug
1050
+ assert len (data ) == 4
1051
+ assert data [0 ]["slug" ] == "alpha"
1052
+ assert data [1 ]["slug" ] == "beta"
1053
+ assert data [2 ]["slug" ] == "charlie"
1054
+ assert data [3 ]["slug" ] == "zebra"
1055
+
1056
+ def test_list_labels_alphabetical_order_with_numbers (
1057
+ self , api_client , mailbox , user
1058
+ ):
1059
+ """Test that labels with numbers in slugs are ordered correctly."""
1060
+ # Create labels with numbers in different positions
1061
+ LabelFactory (mailbox = mailbox , name = "Label 10" )
1062
+ LabelFactory (mailbox = mailbox , name = "Label 1" )
1063
+ LabelFactory (mailbox = mailbox , name = "Label 2" )
1064
+ LabelFactory (mailbox = mailbox , name = "10 Label" )
1065
+ LabelFactory (mailbox = mailbox , name = "1 Label" )
1066
+
1067
+ url = reverse ("labels-list" )
1068
+ response = api_client .get (url )
1069
+ assert response .status_code == status .HTTP_200_OK
1070
+ data = response .json ()
1071
+
1072
+ # Verify alphabetical ordering (not numerical)
1073
+ assert len (data ) == 5
1074
+ assert data [0 ]["slug" ] == "1-label"
1075
+ assert data [1 ]["slug" ] == "10-label"
1076
+ assert data [2 ]["slug" ] == "label-1"
1077
+ assert data [3 ]["slug" ] == "label-10"
1078
+ assert data [4 ]["slug" ] == "label-2"
1079
+
1080
+ def test_list_labels_alphabetical_order_with_accents (
1081
+ self , api_client , mailbox , user
1082
+ ):
1083
+ """Test that labels with accented characters are ordered correctly."""
1084
+ # Create labels with accented characters
1085
+ LabelFactory (mailbox = mailbox , name = "État civil" )
1086
+ LabelFactory (mailbox = mailbox , name = "Enfance" )
1087
+ LabelFactory (mailbox = mailbox , name = "Urbanisme" )
1088
+
1089
+ url = reverse ("labels-list" )
1090
+ response = api_client .get (url )
1091
+ assert response .status_code == status .HTTP_200_OK
1092
+ data = response .json ()
1093
+
1094
+ # Verify alphabetical ordering by slug
1095
+ assert len (data ) == 3
1096
+ assert data [0 ]["slug" ] == "enfance"
1097
+ assert data [1 ]["slug" ] == "etat-civil"
1098
+ assert data [2 ]["slug" ] == "urbanisme"
1099
+
1100
+ def test_list_labels_alphabetical_order_hierarchical (
1101
+ self , api_client , mailbox , user
1102
+ ):
1103
+ """Test that hierarchical labels maintain alphabetical order within each level."""
1104
+ # Create hierarchical labels in random order
1105
+ LabelFactory (mailbox = mailbox , name = "Work/Meetings" )
1106
+ LabelFactory (mailbox = mailbox , name = "Work/Projects" )
1107
+ LabelFactory (mailbox = mailbox , name = "Personal/Family" )
1108
+ LabelFactory (mailbox = mailbox , name = "Personal/Friends" )
1109
+
1110
+ url = reverse ("labels-list" )
1111
+ response = api_client .get (url )
1112
+ assert response .status_code == status .HTTP_200_OK
1113
+ data = response .json ()
1114
+
1115
+ # Verify top-level labels are ordered alphabetically
1116
+ assert len (data ) == 2
1117
+ assert data [0 ]["slug" ] == "personal"
1118
+ assert data [1 ]["slug" ] == "work"
1119
+
1120
+ # Verify children within each parent are ordered alphabetically
1121
+ personal_children = data [0 ]["children" ]
1122
+ assert len (personal_children ) == 2
1123
+ assert personal_children [0 ]["slug" ] == "personal-family"
1124
+ assert personal_children [1 ]["slug" ] == "personal-friends"
1125
+
1126
+ work_children = data [1 ]["children" ]
1127
+ assert len (work_children ) == 2
1128
+ assert work_children [0 ]["slug" ] == "work-meetings"
1129
+ assert work_children [1 ]["slug" ] == "work-projects"
1130
+
1131
+ def test_list_labels_alphabetical_order_mixed_mailboxes (
1132
+ self , api_client , mailbox , user
1133
+ ):
1134
+ """Test that labels from different mailboxes maintain alphabetical order."""
1135
+ # Create another mailbox
1136
+ other_mailbox = MailboxFactory ()
1137
+ other_mailbox .accesses .create (user = user , role = models .MailboxRoleChoices .ADMIN )
1138
+
1139
+ # Create labels in both mailboxes
1140
+ LabelFactory (mailbox = mailbox , name = "Zebra" )
1141
+ LabelFactory (mailbox = mailbox , name = "Alpha" )
1142
+ LabelFactory (mailbox = other_mailbox , name = "Charlie" )
1143
+ LabelFactory (mailbox = other_mailbox , name = "Beta" )
1144
+
1145
+ url = reverse ("labels-list" )
1146
+ response = api_client .get (url )
1147
+ assert response .status_code == status .HTTP_200_OK
1148
+ data = response .json ()
1149
+
1150
+ # Verify all labels are ordered alphabetically by slug regardless of mailbox
1151
+ assert len (data ) == 4
1152
+ assert data [0 ]["slug" ] == "alpha"
1153
+ assert data [1 ]["slug" ] == "beta"
1154
+ assert data [2 ]["slug" ] == "charlie"
1155
+ assert data [3 ]["slug" ] == "zebra"
1156
+
1157
+ def test_list_labels_alphabetical_order_case_insensitive (
1158
+ self , api_client , mailbox , user
1159
+ ):
1160
+ """Test that label ordering is case-insensitive."""
1161
+ # Create labels with mixed case
1162
+ LabelFactory (mailbox = mailbox , name = "ZEBRA" )
1163
+ LabelFactory (mailbox = mailbox , name = "alpha" )
1164
+ LabelFactory (mailbox = mailbox , name = "Charlie" )
1165
+ LabelFactory (mailbox = mailbox , name = "BETA" )
1166
+
1167
+ url = reverse ("labels-list" )
1168
+ response = api_client .get (url )
1169
+ assert response .status_code == status .HTTP_200_OK
1170
+ data = response .json ()
1171
+
1172
+ # Verify case-insensitive alphabetical ordering
1173
+ assert len (data ) == 4
1174
+ assert data [0 ]["slug" ] == "alpha"
1175
+ assert data [1 ]["slug" ] == "beta"
1176
+ assert data [2 ]["slug" ] == "charlie"
1177
+ assert data [3 ]["slug" ] == "zebra"
0 commit comments