@@ -38,10 +38,23 @@ def test_handle_with_default_offset(
3838 self , mock_repository_contributor , mock_user , mock_member_profile
3939 ):
4040 """Test command execution with default offset."""
41- mock_member_profile .objects .get_or_create .return_value = (MagicMock (), False )
42- mock_user1 = MagicMock (id = 1 , title = "User 1" , contributions_count = 0 )
43- mock_user2 = MagicMock (id = 2 , title = "User 2" , contributions_count = 0 )
44- mock_user3 = MagicMock (id = 3 , title = "User 3" , contributions_count = 0 )
41+ mock_profile1 = MagicMock (contributions_count = 0 )
42+ mock_profile2 = MagicMock (contributions_count = 0 )
43+ mock_profile3 = MagicMock (contributions_count = 0 )
44+
45+ def get_or_create_side_effect (github_user ):
46+ if github_user .id == 1 :
47+ return mock_profile1 , False
48+ if github_user .id == 2 :
49+ return mock_profile2 , False
50+ if github_user .id == 3 :
51+ return mock_profile3 , False
52+ return MagicMock (), False
53+
54+ mock_member_profile .objects .get_or_create .side_effect = get_or_create_side_effect
55+ mock_user1 = MagicMock (id = 1 , title = "User 1" )
56+ mock_user2 = MagicMock (id = 2 , title = "User 2" )
57+ mock_user3 = MagicMock (id = 3 , title = "User 3" )
4558
4659 mock_users_queryset = MagicMock ()
4760 mock_users_queryset .count .return_value = 3
@@ -73,12 +86,16 @@ def test_handle_with_default_offset(
7386 mock_print .assert_any_call ("2 of 3 User 2" )
7487 mock_print .assert_any_call ("3 of 3 User 3" )
7588
76- assert mock_user1 .contributions_count == 10
77- assert mock_user2 .contributions_count == 20
78- assert mock_user3 .contributions_count == 30
89+ assert mock_profile1 .contributions_count == 10
90+ assert mock_profile2 .contributions_count == 20
91+ assert mock_profile3 .contributions_count == 30
7992
80- assert mock_user .bulk_save .call_count == 2
81- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_user1 , mock_user2 , mock_user3 ]
93+ assert mock_member_profile .bulk_save .call_count == 2
94+ assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == [
95+ mock_profile1 ,
96+ mock_profile2 ,
97+ mock_profile3 ,
98+ ]
8299
83100 @patch ("apps.github.management.commands.github_update_users.MemberProfile" )
84101 @patch ("apps.github.management.commands.github_update_users.User" )
@@ -88,9 +105,19 @@ def test_handle_with_custom_offset(
88105 self , mock_repository_contributor , mock_user , mock_member_profile
89106 ):
90107 """Test command execution with custom offset."""
91- mock_member_profile .objects .get_or_create .return_value = (MagicMock (), False )
92- mock_user1 = MagicMock (id = 2 , title = "User 2" , contributions_count = 0 )
93- mock_user2 = MagicMock (id = 3 , title = "User 3" , contributions_count = 0 )
108+ mock_profile1 = MagicMock (contributions_count = 0 )
109+ mock_profile2 = MagicMock (contributions_count = 0 )
110+
111+ def get_or_create_side_effect (github_user ):
112+ if github_user .id == 2 :
113+ return mock_profile1 , False
114+ if github_user .id == 3 :
115+ return mock_profile2 , False
116+ return MagicMock (), False
117+
118+ mock_member_profile .objects .get_or_create .side_effect = get_or_create_side_effect
119+ mock_user1 = MagicMock (id = 2 , title = "User 2" )
120+ mock_user2 = MagicMock (id = 3 , title = "User 3" )
94121
95122 mock_users_queryset = MagicMock ()
96123 mock_users_queryset .count .return_value = 3
@@ -117,11 +144,14 @@ def test_handle_with_custom_offset(
117144 mock_print .assert_any_call ("2 of 2 User 2" )
118145 mock_print .assert_any_call ("3 of 2 User 3" )
119146
120- assert mock_user1 .contributions_count == 20
121- assert mock_user2 .contributions_count == 30
147+ assert mock_profile1 .contributions_count == 20
148+ assert mock_profile2 .contributions_count == 30
122149
123- assert mock_user .bulk_save .call_count == 2
124- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_user1 , mock_user2 ]
150+ assert mock_member_profile .bulk_save .call_count == 2
151+ assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == [
152+ mock_profile1 ,
153+ mock_profile2 ,
154+ ]
125155
126156 @patch ("apps.github.management.commands.github_update_users.MemberProfile" )
127157 @patch ("apps.github.management.commands.github_update_users.User" )
@@ -130,10 +160,19 @@ def test_handle_with_custom_offset(
130160 def test_handle_with_users_having_no_contributions (
131161 self , mock_repository_contributor , mock_user , mock_member_profile
132162 ):
133- """Test command execution when users have no contributions."""
134- mock_member_profile .objects .get_or_create .return_value = (MagicMock (), False )
135- mock_user1 = MagicMock (id = 1 , title = "User 1" , contributions_count = 0 )
136- mock_user2 = MagicMock (id = 2 , title = "User 2" , contributions_count = 0 )
163+ mock_profile1 = MagicMock (contributions_count = 0 )
164+ mock_profile2 = MagicMock (contributions_count = 0 )
165+
166+ def get_or_create_side_effect (github_user ):
167+ if github_user .id == 1 :
168+ return mock_profile1 , False
169+ if github_user .id == 2 :
170+ return mock_profile2 , False
171+ return MagicMock (), False
172+
173+ mock_member_profile .objects .get_or_create .side_effect = get_or_create_side_effect
174+ mock_user1 = MagicMock (id = 1 , title = "User 1" )
175+ mock_user2 = MagicMock (id = 2 , title = "User 2" )
137176
138177 mock_users_queryset = MagicMock ()
139178 mock_users_queryset .count .return_value = 2
@@ -153,11 +192,14 @@ def test_handle_with_users_having_no_contributions(
153192 mock_print .assert_any_call ("1 of 2 User 1" )
154193 mock_print .assert_any_call ("2 of 2 User 2" )
155194
156- assert mock_user1 .contributions_count == 0
157- assert mock_user2 .contributions_count == 0
195+ assert mock_profile1 .contributions_count == 0
196+ assert mock_profile2 .contributions_count == 0
158197
159- assert mock_user .bulk_save .call_count == 1
160- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_user1 , mock_user2 ]
198+ assert mock_member_profile .bulk_save .call_count == 1
199+ assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == [
200+ mock_profile1 ,
201+ mock_profile2 ,
202+ ]
161203
162204 @patch ("apps.github.management.commands.github_update_users.MemberProfile" )
163205 @patch ("apps.github.management.commands.github_update_users.User" )
@@ -167,12 +209,15 @@ def test_handle_with_single_user(
167209 self , mock_repository_contributor , mock_user , mock_member_profile
168210 ):
169211 """Test command execution with single user."""
170- mock_member_profile .objects .get_or_create .return_value = (MagicMock (), False )
171- mock_user1 = MagicMock (id = 1 , title = "User 1" , contributions_count = 0 )
212+ mock_profile1 = MagicMock (contributions_count = 0 )
213+ mock_member_profile .objects .get_or_create .return_value = (mock_profile1 , False )
214+ mock_user1 = MagicMock (id = 1 , title = "User 1" )
172215
173216 mock_users_queryset = MagicMock ()
174217 mock_users_queryset .count .return_value = 1
175218 mock_users_queryset .__getitem__ .return_value = [mock_user1 ]
219+ mock_users_queryset .count .return_value = 1
220+ mock_users_queryset .__getitem__ .return_value = [mock_user1 ]
176221
177222 mock_user .objects .order_by .return_value = mock_users_queryset
178223
@@ -188,10 +233,10 @@ def test_handle_with_single_user(
188233
189234 mock_print .assert_called_once_with ("1 of 1 User 1" )
190235
191- assert mock_user1 .contributions_count == 15
236+ assert mock_profile1 .contributions_count == 15
192237
193- assert mock_user .bulk_save .call_count == 2
194- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_user1 ]
238+ assert mock_member_profile .bulk_save .call_count == 2
239+ assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_profile1 ]
195240
196241 @patch ("apps.github.management.commands.github_update_users.MemberProfile" )
197242 @patch ("apps.github.management.commands.github_update_users.User" )
@@ -218,8 +263,6 @@ def test_handle_with_empty_user_list(
218263
219264 mock_print .assert_not_called ()
220265
221- assert mock_user .bulk_save .call_count == 1
222- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == []
223266 assert mock_member_profile .bulk_save .call_count == 1
224267 assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == []
225268
@@ -231,9 +274,19 @@ def test_handle_with_exact_batch_size(
231274 self , mock_repository_contributor , mock_user , mock_member_profile
232275 ):
233276 """Test command execution when user count equals batch size."""
234- mock_member_profile .objects .get_or_create .return_value = (MagicMock (), False )
235- mock_user1 = MagicMock (id = 1 , title = "User 1" , contributions_count = 0 )
236- mock_user2 = MagicMock (id = 2 , title = "User 2" , contributions_count = 0 )
277+ mock_profile1 = MagicMock (contributions_count = 0 )
278+ mock_profile2 = MagicMock (contributions_count = 0 )
279+
280+ def get_or_create_side_effect (github_user ):
281+ if github_user .id == 1 :
282+ return mock_profile1 , False
283+ if github_user .id == 2 :
284+ return mock_profile2 , False
285+ return MagicMock (), False
286+
287+ mock_member_profile .objects .get_or_create .side_effect = get_or_create_side_effect
288+ mock_user1 = MagicMock (id = 1 , title = "User 1" )
289+ mock_user2 = MagicMock (id = 2 , title = "User 2" )
237290
238291 mock_users_queryset = MagicMock ()
239292 mock_users_queryset .count .return_value = 2
@@ -256,11 +309,14 @@ def test_handle_with_exact_batch_size(
256309 mock_print .assert_any_call ("1 of 2 User 1" )
257310 mock_print .assert_any_call ("2 of 2 User 2" )
258311
259- assert mock_user1 .contributions_count == 10
260- assert mock_user2 .contributions_count == 20
312+ assert mock_profile1 .contributions_count == 10
313+ assert mock_profile2 .contributions_count == 20
261314
262- assert mock_user .bulk_save .call_count == 2
263- assert mock_user .bulk_save .call_args_list [- 1 ][0 ][0 ] == [mock_user1 , mock_user2 ]
315+ assert mock_member_profile .bulk_save .call_count == 2
316+ assert mock_member_profile .bulk_save .call_args_list [- 1 ][0 ][0 ] == [
317+ mock_profile1 ,
318+ mock_profile2 ,
319+ ]
264320
265321 @patch ("apps.github.management.commands.github_update_users.MemberProfile" )
266322 @patch ("apps.github.management.commands.github_update_users.User" )
@@ -275,9 +331,6 @@ def test_handle_member_profile_created(
275331
276332 mock_user1 = MagicMock (
277333 id = 1 ,
278- contributions_count = 0 ,
279- is_owasp_staff = True ,
280- has_public_member_page = False ,
281334 )
282335
283336 mock_users_queryset = MagicMock ()
@@ -296,6 +349,4 @@ def test_handle_member_profile_created(
296349
297350 assert mock_profile .github_user == mock_user1
298351 assert mock_profile .contributions_count == 5
299- assert mock_profile .is_owasp_staff is True
300- assert mock_profile .has_public_member_page is False
301352 mock_member_profile .bulk_save .assert_called_once ()
0 commit comments