Best Python code snippet using autotest_python
batch_notifier_test.py
Source:batch_notifier_test.py
...35 self.send_email.assert_called_once_with(subject, message, sender, (receiver,))36 def test_send_single_failure(self):37 bn = BatchNotifier(batch_mode='all')38 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])39 bn.send_email()40 self.check_email_send(41 'Luigi: 1 failure in the last 60 minutes',42 '- Task(a=5) (1 failure)'43 )44 def test_do_not_send_single_failure_without_receiver(self):45 self.email().receiver = None46 bn = BatchNotifier(batch_mode='all')47 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])48 bn.send_email()49 self.send_email.assert_not_called()50 def test_send_single_failure_to_owner_only(self):51 self.email().receiver = None52 bn = BatchNotifier(batch_mode='all')53 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', ['owner@test.com'])54 bn.send_email()55 self.check_email_send(56 'Luigi: Your tasks have 1 failure in the last 60 minutes',57 '- Task(a=5) (1 failure)',58 receiver='owner@test.com',59 )60 def test_send_single_disable(self):61 bn = BatchNotifier(batch_mode='all')62 for _ in range(10):63 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])64 bn.add_disable('Task(a=5)', 'Task', {'a': 5}, [])65 bn.send_email()66 self.check_email_send(67 'Luigi: 10 failures, 1 disable in the last 60 minutes',68 '- Task(a=5) (10 failures, 1 disable)'69 )70 def test_send_multiple_disables(self):71 bn = BatchNotifier(batch_mode='family')72 for _ in range(10):73 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])74 bn.add_failure('Task(a=6)', 'Task', {'a': 6}, 'error', [])75 bn.add_disable('Task(a=5)', 'Task', {'a': 5}, [])76 bn.add_disable('Task(a=6)', 'Task', {'a': 6}, [])77 bn.send_email()78 self.check_email_send(79 'Luigi: 20 failures, 2 disables in the last 60 minutes',80 '- Task (20 failures, 2 disables)'81 )82 def test_send_single_scheduling_fail(self):83 bn = BatchNotifier(batch_mode='family')84 bn.add_scheduling_fail('Task()', 'Task', {}, 'error', [])85 bn.send_email()86 self.check_email_send(87 'Luigi: 1 scheduling failure in the last 60 minutes',88 '- Task (1 scheduling failure)',89 )90 def test_multiple_failures_of_same_job(self):91 bn = BatchNotifier(batch_mode='all')92 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])93 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])94 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])95 bn.send_email()96 self.check_email_send(97 'Luigi: 3 failures in the last 60 minutes',98 '- Task(a=5) (3 failures)'99 )100 def test_multiple_failures_of_multiple_jobs(self):101 bn = BatchNotifier(batch_mode='all')102 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])103 bn.add_failure('Task(a=6)', 'Task', {'a': 6}, 'error', [])104 bn.add_failure('Task(a=6)', 'Task', {'a': 6}, 'error', [])105 bn.send_email()106 self.check_email_send(107 'Luigi: 3 failures in the last 60 minutes',108 '- Task(a=6) (2 failures)\n'109 '- Task(a=5) (1 failure)'110 )111 def test_group_on_family(self):112 bn = BatchNotifier(batch_mode='family')113 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])114 bn.add_failure('Task(a=6)', 'Task', {'a': 6}, 'error', [])115 bn.add_failure('Task(a=6)', 'Task', {'a': 6}, 'error', [])116 bn.add_failure('OtherTask(a=6)', 'OtherTask', {'a': 6}, 'error', [])117 bn.send_email()118 self.check_email_send(119 'Luigi: 4 failures in the last 60 minutes',120 '- Task (3 failures)\n'121 '- OtherTask (1 failure)'122 )123 def test_group_on_unbatched_params(self):124 bn = BatchNotifier(batch_mode='unbatched_params')125 bn.add_failure('Task(a=5, b=1)', 'Task', {'a': 5}, 'error', [])126 bn.add_failure('Task(a=5, b=2)', 'Task', {'a': 5}, 'error', [])127 bn.add_failure('Task(a=6, b=1)', 'Task', {'a': 6}, 'error', [])128 bn.add_failure('Task(a=6, b=2)', 'Task', {'a': 6}, 'error', [])129 bn.add_failure('Task(a=6, b=3)', 'Task', {'a': 6}, 'error', [])130 bn.add_failure('Task(a=6, b=4)', 'Task', {'a': 6}, 'error', [])131 bn.add_failure('OtherTask(a=5, b=1)', 'OtherTask', {'a': 5}, 'error', [])132 bn.add_failure('OtherTask(a=6, b=1)', 'OtherTask', {'a': 6}, 'error', [])133 bn.add_failure('OtherTask(a=6, b=2)', 'OtherTask', {'a': 6}, 'error', [])134 bn.add_failure('OtherTask(a=6, b=3)', 'OtherTask', {'a': 6}, 'error', [])135 bn.send_email()136 self.check_email_send(137 'Luigi: 10 failures in the last 60 minutes',138 '- Task(a=6) (4 failures)\n'139 '- OtherTask(a=6) (3 failures)\n'140 '- Task(a=5) (2 failures)\n'141 '- OtherTask(a=5) (1 failure)'142 )143 def test_include_one_expl_includes_latest(self):144 bn = BatchNotifier(batch_mode='family', error_messages=1)145 bn.add_failure('Task(a=1)', 'Task', {'a': 1}, 'error 1', [])146 bn.add_failure('Task(a=2)', 'Task', {'a': 2}, 'error 2', [])147 bn.add_failure('TaskB(a=1)', 'TaskB', {'a': 1}, 'error', [])148 bn.send_email()149 self.check_email_send(150 'Luigi: 3 failures in the last 60 minutes',151 '- Task (2 failures)\n'152 '\n'153 ' error 2\n'154 '\n'155 '- TaskB (1 failure)\n'156 '\n'157 ' error'158 )159 def test_include_two_expls(self):160 bn = BatchNotifier(batch_mode='family', error_messages=2)161 bn.add_failure('Task(a=1)', 'Task', {'a': 1}, 'error 1', [])162 bn.add_failure('Task(a=2)', 'Task', {'a': 2}, 'error 2', [])163 bn.add_failure('TaskB(a=1)', 'TaskB', {'a': 1}, 'error', [])164 bn.send_email()165 self.check_email_send(166 'Luigi: 3 failures in the last 60 minutes',167 '- Task (2 failures)\n'168 '\n'169 ' error 1\n'170 '\n'171 ' error 2\n'172 '\n'173 '- TaskB (1 failure)\n'174 '\n'175 ' error'176 )177 def test_limit_expl_length(self):178 bn = BatchNotifier(batch_mode='family', error_messages=1, error_lines=2)179 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'line 1\nline 2\nline 3\nline 4\n', [])180 bn.send_email()181 self.check_email_send(182 'Luigi: 1 failure in the last 60 minutes',183 '- Task (1 failure)\n'184 '\n'185 ' line 3\n'186 ' line 4'187 )188 def test_expl_varies_by_owner(self):189 bn = BatchNotifier(batch_mode='family', error_messages=1)190 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'msg1', owners=['a@test.com'])191 bn.add_failure('Task(a=2)', 'Task', {'a': '2'}, 'msg2', owners=['b@test.com'])192 bn.send_email()193 send_calls = [194 mock.call(195 'Luigi: Your tasks have 1 failure in the last 60 minutes',196 '- Task (1 failure)\n'197 '\n'198 ' msg1',199 'sender@test.com',200 ('a@test.com',),201 ),202 mock.call(203 'Luigi: Your tasks have 1 failure in the last 60 minutes',204 '- Task (1 failure)\n'205 '\n'206 ' msg2',207 'sender@test.com',208 ('b@test.com',),209 ),210 mock.call(211 'Luigi: 2 failures in the last 60 minutes',212 '- Task (2 failures)\n'213 '\n'214 ' msg2',215 'sender@test.com',216 ('r@test.com',),217 ),218 ]219 self.send_email.assert_has_calls(send_calls, any_order=True)220 def test_include_two_expls_html_format(self):221 self.email().format = 'html'222 bn = BatchNotifier(batch_mode='family', error_messages=2)223 bn.add_failure('Task(a=1)', 'Task', {'a': 1}, 'error 1', [])224 bn.add_failure('Task(a=2)', 'Task', {'a': 2}, 'error 2', [])225 bn.add_failure('TaskB(a=1)', 'TaskB', {'a': 1}, 'error', [])226 bn.send_email()227 self.check_email_send(228 'Luigi: 3 failures in the last 60 minutes',229 '<ul>\n'230 '<li>Task (2 failures)\n'231 '<pre>error 1</pre>\n'232 '<pre>error 2</pre>\n'233 '<li>TaskB (1 failure)\n'234 '<pre>error</pre>\n'235 '</ul>'236 )237 def test_limit_expl_length_html_format(self):238 self.email().format = 'html'239 bn = BatchNotifier(batch_mode='family', error_messages=1, error_lines=2)240 bn.add_failure('Task(a=1)', 'Task', {'a': 1}, 'line 1\nline 2\nline 3\nline 4\n', [])241 bn.send_email()242 self.check_email_send(243 'Luigi: 1 failure in the last 60 minutes',244 '<ul>\n'245 '<li>Task (1 failure)\n'246 '<pre>line 3\n'247 'line 4</pre>\n'248 '</ul>'249 )250 def test_send_clears_backlog(self):251 bn = BatchNotifier(batch_mode='all')252 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])253 bn.add_disable('Task(a=5)', 'Task', {'a': 5}, [])254 bn.add_scheduling_fail('Task(a=6)', 'Task', {'a': 6}, 'scheduling error', [])255 bn.send_email()256 self.send_email.reset_mock()257 bn.send_email()258 self.send_email.assert_not_called()259 def test_email_gets_cleared_on_failure(self):260 bn = BatchNotifier(batch_mode='all')261 bn.add_failure('Task(a=5)', 'Task', {'a': 1}, '', [])262 self.send_email.side_effect = SMTPServerDisconnected('timeout')263 self.assertRaises(SMTPServerDisconnected, bn.send_email)264 self.send_email.reset_mock()265 bn.send_email()266 self.send_email.assert_not_called()267 def test_send_clears_all_old_data(self):268 bn = BatchNotifier(batch_mode='all', error_messages=100)269 for i in range(100):270 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error {}'.format(i), [])271 bn.add_disable('Task(a=5)', 'Task', {'a': 5}, [])272 bn.add_scheduling_fail('Task(a=6)', 'Task', {'a': 6}, 'scheduling error {}'.format(i), [])273 bn.send_email()274 self.check_email_send(275 'Luigi: 1 failure, 1 disable, 1 scheduling failure in the last 60 minutes',276 '- Task(a=5) (1 failure, 1 disable)\n'277 '\n'278 ' error {}\n'279 '\n'280 '- Task(a=6) (1 scheduling failure)\n'281 '\n'282 ' scheduling error {}'.format(i, i),283 )284 self.send_email.reset_mock()285 def test_auto_send_on_update_after_time_period(self):286 bn = BatchNotifier(batch_mode='all')287 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])288 for i in range(60):289 bn.update()290 self.send_email.assert_not_called()291 self.incr_time(minutes=1)292 bn.update()293 self.check_email_send(294 'Luigi: 1 failure in the last 60 minutes',295 '- Task(a=5) (1 failure)'296 )297 def test_auto_send_on_update_after_time_period_with_disable_only(self):298 bn = BatchNotifier(batch_mode='all')299 bn.add_disable('Task(a=5)', 'Task', {'a': 5}, [])300 for i in range(60):301 bn.update()302 self.send_email.assert_not_called()303 self.incr_time(minutes=1)304 bn.update()305 self.check_email_send(306 'Luigi: 1 disable in the last 60 minutes',307 '- Task(a=5) (1 disable)'308 )309 def test_no_auto_send_until_end_of_interval_with_error(self):310 bn = BatchNotifier(batch_mode='all')311 for i in range(90):312 bn.update()313 self.send_email.assert_not_called()314 self.incr_time(minutes=1)315 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])316 for i in range(30):317 bn.update()318 self.send_email.assert_not_called()319 self.incr_time(minutes=1)320 bn.update()321 self.check_email_send(322 'Luigi: 1 failure in the last 60 minutes',323 '- Task(a=5) (1 failure)'324 )325 def test_no_auto_send_for_interval_after_exception(self):326 bn = BatchNotifier(batch_mode='all')327 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])328 self.send_email.side_effect = SMTPServerDisconnected329 self.incr_time(minutes=60)330 self.assertRaises(SMTPServerDisconnected, bn.update)331 self.send_email.reset_mock()332 self.send_email.side_effect = None333 bn.add_failure('Task(a=5)', 'Task', {'a': 5}, 'error', [])334 for i in range(60):335 bn.update()336 self.send_email.assert_not_called()337 self.incr_time(minutes=1)338 bn.update()339 self.assertEqual(1, self.send_email.call_count)340 def test_send_batch_failure_emails_to_owners(self):341 bn = BatchNotifier(batch_mode='all')342 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'error', ['a@test.com', 'b@test.com'])343 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'error', ['b@test.com'])344 bn.add_failure('Task(a=2)', 'Task', {'a': '2'}, 'error', ['a@test.com'])345 bn.send_email()346 send_calls = [347 mock.call(348 'Luigi: 3 failures in the last 60 minutes',349 '- Task(a=1) (2 failures)\n'350 '- Task(a=2) (1 failure)',351 'sender@test.com',352 ('r@test.com',),353 ),354 mock.call(355 'Luigi: Your tasks have 2 failures in the last 60 minutes',356 '- Task(a=1) (1 failure)\n'357 '- Task(a=2) (1 failure)',358 'sender@test.com',359 ('a@test.com',),360 ),361 mock.call(362 'Luigi: Your tasks have 2 failures in the last 60 minutes',363 '- Task(a=1) (2 failures)',364 'sender@test.com',365 ('b@test.com',),366 ),367 ]368 self.send_email.assert_has_calls(send_calls, any_order=True)369 def test_send_batch_disable_email_to_owners(self):370 bn = BatchNotifier(batch_mode='all')371 bn.add_disable('Task(a=1)', 'Task', {'a': '1'}, ['a@test.com'])372 bn.send_email()373 send_calls = [374 mock.call(375 'Luigi: 1 disable in the last 60 minutes',376 '- Task(a=1) (1 disable)',377 'sender@test.com',378 ('r@test.com',),379 ),380 mock.call(381 'Luigi: Your tasks have 1 disable in the last 60 minutes',382 '- Task(a=1) (1 disable)',383 'sender@test.com',384 ('a@test.com',),385 ),386 ]387 self.send_email.assert_has_calls(send_calls, any_order=True)388 def test_batch_identical_expls(self):389 bn = BatchNotifier(error_messages=1, group_by_error_messages=True)390 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'msg1', [])391 bn.add_failure('Task(a=2)', 'Task', {'a': '2'}, 'msg1', [])392 bn.add_failure('Task(a=3)', 'Task', {'a': '3'}, 'msg1', [])393 bn.add_failure('Task(a=4)', 'Task', {'a': '4'}, 'msg2', [])394 bn.add_failure('Task(a=4)', 'Task', {'a': '4'}, 'msg2', [])395 bn.send_email()396 self.check_email_send(397 'Luigi: 5 failures in the last 60 minutes',398 '- Task(a=1) (1 failure)\n'399 ' Task(a=2) (1 failure)\n'400 ' Task(a=3) (1 failure)\n'401 '\n'402 ' msg1\n'403 '\n'404 '- Task(a=4) (2 failures)\n'405 '\n'406 ' msg2'407 )408 def test_batch_identical_expls_html(self):409 self.email().format = 'html'410 bn = BatchNotifier(error_messages=1, group_by_error_messages=True)411 bn.add_failure('Task(a=1)', 'Task', {'a': '1'}, 'msg1', [])412 bn.add_failure('Task(a=2)', 'Task', {'a': '2'}, 'msg1', [])413 bn.add_failure('Task(a=3)', 'Task', {'a': '3'}, 'msg1', [])414 bn.add_failure('Task(a=4)', 'Task', {'a': '4'}, 'msg2', [])415 bn.add_failure('Task(a=4)', 'Task', {'a': '4'}, 'msg2', [])416 bn.send_email()417 self.check_email_send(418 'Luigi: 5 failures in the last 60 minutes',419 '<ul>\n'420 '<li>Task(a=1) (1 failure)\n'421 '<br>Task(a=2) (1 failure)\n'422 '<br>Task(a=3) (1 failure)\n'423 '<pre>msg1</pre>\n'424 '<li>Task(a=4) (2 failures)\n'425 '<pre>msg2</pre>\n'426 '</ul>'427 )428 def test_unicode_error_message(self):429 bn = BatchNotifier(error_messages=1)430 bn.add_failure('Task()', 'Task', {}, 'Ãrror', [])431 bn.send_email()432 self.check_email_send(433 'Luigi: 1 failure in the last 60 minutes',434 '- Task() (1 failure)\n'435 '\n'436 ' Ãrror'437 )438 def test_unicode_error_message_html(self):439 self.email().format = 'html'440 bn = BatchNotifier(error_messages=1)441 bn.add_failure('Task()', 'Task', {}, 'Ãrror', [])442 bn.send_email()443 self.check_email_send(444 'Luigi: 1 failure in the last 60 minutes',445 '<ul>\n'446 '<li>Task() (1 failure)\n'447 '<pre>Ãrror</pre>\n'448 '</ul>'449 )450 def test_unicode_param_value(self):451 for batch_mode in ('all', 'unbatched_params'):452 self.send_email.reset_mock()453 bn = BatchNotifier(batch_mode=batch_mode)454 bn.add_failure('Task(a=á)', 'Task', {'a': 'á'}, 'error', [])455 bn.send_email()456 self.check_email_send(457 'Luigi: 1 failure in the last 60 minutes',458 '- Task(a=á) (1 failure)'459 )460 def test_unicode_param_value_html(self):461 self.email().format = 'html'462 for batch_mode in ('all', 'unbatched_params'):463 self.send_email.reset_mock()464 bn = BatchNotifier(batch_mode=batch_mode)465 bn.add_failure('Task(a=á)', 'Task', {'a': 'á'}, 'error', [])466 bn.send_email()467 self.check_email_send(468 'Luigi: 1 failure in the last 60 minutes',469 '<ul>\n'470 '<li>Task(a=á) (1 failure)\n'471 '</ul>'472 )473 def test_unicode_param_name(self):474 for batch_mode in ('all', 'unbatched_params'):475 self.send_email.reset_mock()476 bn = BatchNotifier(batch_mode=batch_mode)477 bn.add_failure('Task(á=a)', 'Task', {'á': 'a'}, 'error', [])478 bn.send_email()479 self.check_email_send(480 'Luigi: 1 failure in the last 60 minutes',481 '- Task(á=a) (1 failure)'482 )483 def test_unicode_param_name_html(self):484 self.email().format = 'html'485 for batch_mode in ('all', 'unbatched_params'):486 self.send_email.reset_mock()487 bn = BatchNotifier(batch_mode=batch_mode)488 bn.add_failure('Task(á=a)', 'Task', {'á': 'a'}, 'error', [])489 bn.send_email()490 self.check_email_send(491 'Luigi: 1 failure in the last 60 minutes',492 '<ul>\n'493 '<li>Task(á=a) (1 failure)\n'494 '</ul>'495 )496 def test_unicode_class_name(self):497 bn = BatchNotifier()498 bn.add_failure('Tásk()', 'Tásk', {}, 'error', [])499 bn.send_email()500 self.check_email_send(501 'Luigi: 1 failure in the last 60 minutes',502 '- Tásk() (1 failure)'503 )504 def test_unicode_class_name_html(self):505 self.email().format = 'html'506 bn = BatchNotifier()507 bn.add_failure('Tásk()', 'Tásk', {}, 'error', [])508 bn.send_email()509 self.check_email_send(510 'Luigi: 1 failure in the last 60 minutes',511 '<ul>\n'512 '<li>Tásk() (1 failure)\n'513 '</ul>'...
tests.py
Source:tests.py
1# pylint: disable=missing-docstring2import datetime3import mock4from freezegun import freeze_time5from django.test import TestCase6from django.utils import timezone7from accounts.factories import UserProfileFactory8from emails.jobs.daily.send_follow_up_email import Job as FollowUpJob9from emails.jobs.hourly.send_referral_program_email import Job as ReferralJob10from emails.jobs.hourly.send_welcome_email import Job11from .utils import AccountEmail12class EmailUtilsTests(TestCase):13 def setUp(self):14 UserProfileFactory.create_batch(10)15 self.hourly_job = Job()16 @mock.patch("emails.mailer.EmailMultiAlternatives.send")17 @freeze_time("2021-02-01")18 def test_get_user_signed_up_from_the_past(self, send_email):19 users = AccountEmail.get_signed_up_users()20 self.assertEqual(users.count(), 0)21 self.hourly_job.execute()22 self.assertEqual(send_email.call_count, 0)23 @mock.patch("emails.mailer.EmailMultiAlternatives.send")24 def test_get_user_just_signed_up_now(self, send_email):25 users = AccountEmail.get_signed_up_users()26 self.assertEqual(users.count(), 0)27 self.hourly_job.execute()28 self.assertEqual(send_email.call_count, 0)29 @mock.patch("emails.mailer.EmailMultiAlternatives.send")30 def test_get_user_signed_up_greater_than_6_hours(self, send_email):31 seven_hours_after = timezone.now() + datetime.timedelta(hours=7)32 with freeze_time(seven_hours_after):33 users = AccountEmail.get_signed_up_users()34 self.assertEqual(users.count(), 0)35 self.hourly_job.execute()36 self.assertEqual(send_email.call_count, 0)37 @mock.patch("emails.mailer.EmailMultiAlternatives.send")38 def test_get_user_signed_up_less_than_6_hours(self, send_email):39 five_hours_after = timezone.now() + datetime.timedelta(hours=5)40 with freeze_time(five_hours_after):41 users = AccountEmail.get_signed_up_users()42 self.assertEqual(users.count(), 0)43 self.hourly_job.execute()44 self.assertEqual(send_email.call_count, 0)45 @mock.patch("emails.mailer.EmailMultiAlternatives.send")46 def test_get_user_signed_up_6_hours_before(self, send_email):47 six_hours_after = timezone.now() + datetime.timedelta(hours=6)48 with freeze_time(six_hours_after):49 users = AccountEmail.get_signed_up_users()50 self.assertEqual(users.count(), 10)51 self.hourly_job.execute()52 self.assertEqual(send_email.call_count, 1)53class ReferralProgramEmailTests(TestCase):54 def setUp(self):55 UserProfileFactory.create_batch(10)56 self.hourly_job = ReferralJob()57 @mock.patch("emails.mailer.EmailMultiAlternatives.send")58 @freeze_time("2021-02-01")59 def test_get_user_signed_up_from_the_past(self, send_email):60 users = AccountEmail.get_signed_up_users(3)61 self.assertEqual(users.count(), 0)62 self.hourly_job.execute()63 self.assertEqual(send_email.call_count, 0)64 @mock.patch("emails.mailer.EmailMultiAlternatives.send")65 def test_get_user_just_signed_up_now(self, send_email):66 users = AccountEmail.get_signed_up_users(3)67 self.assertEqual(users.count(), 0)68 self.hourly_job.execute()69 self.assertEqual(send_email.call_count, 0)70 @mock.patch("emails.mailer.EmailMultiAlternatives.send")71 def test_get_user_signed_up_greater_than_3_hours(self, send_email):72 four_hours_after = timezone.now() + datetime.timedelta(hours=4)73 with freeze_time(four_hours_after):74 users = AccountEmail.get_signed_up_users(3)75 self.assertEqual(users.count(), 0)76 self.hourly_job.execute()77 self.assertEqual(send_email.call_count, 0)78 @mock.patch("emails.mailer.EmailMultiAlternatives.send")79 def test_get_user_signed_up_less_than_3_hours(self, send_email):80 two_hours_after = timezone.now() + datetime.timedelta(hours=2)81 with freeze_time(two_hours_after):82 users = AccountEmail.get_signed_up_users(3)83 self.assertEqual(users.count(), 0)84 self.hourly_job.execute()85 self.assertEqual(send_email.call_count, 0)86 @mock.patch("emails.mailer.EmailMultiAlternatives.send")87 def test_get_user_signed_up_3_hours_before(self, send_email):88 three_hours_after = timezone.now() + datetime.timedelta(hours=3)89 with freeze_time(three_hours_after):90 users = AccountEmail.get_signed_up_users(3)91 self.assertEqual(users.count(), 10)92 self.hourly_job.execute()93 self.assertEqual(send_email.call_count, 1)94class FollowUpEmailTests(TestCase):95 def setUp(self):96 self.users = UserProfileFactory.create_batch(10, is_waitlisted=False)97 self.daily_job = FollowUpJob()98 @mock.patch("emails.mailer.EmailMultiAlternatives.send")99 @freeze_time("2021-01-10")100 def test_get_users_deactivated_empty(self, send_email):101 users = AccountEmail.get_deactivated_users()102 self.assertEqual(users.count(), 0)103 self.daily_job.execute()104 self.assertEqual(send_email.call_count, 0)105 @mock.patch("emails.mailer.EmailMultiAlternatives.send")106 def test_get_users_deactivated_now(self, send_email):107 date = timezone.now()108 self.users[1].last_posting_time = date109 self.users[1].save()110 self.users[2].last_posting_time = date111 self.users[2].save()112 users = AccountEmail.get_deactivated_users()113 self.assertEqual(users.count(), 0)114 self.daily_job.execute()115 self.assertEqual(send_email.call_count, 0)116 @mock.patch("emails.mailer.EmailMultiAlternatives.send")117 def test_get_users_deactivated_less_than_2_weeks(self, send_email):118 date = timezone.now() - datetime.timedelta(days=20)119 self.users[1].last_posting_time = date120 self.users[1].save()121 self.users[2].last_posting_time = date122 self.users[2].save()123 users = AccountEmail.get_deactivated_users()124 self.assertEqual(users.count(), 0)125 self.daily_job.execute()126 self.assertEqual(send_email.call_count, 0)127 @mock.patch("emails.mailer.EmailMultiAlternatives.send")128 def test_get_users_deactivated_greater_than_2_weeks(self, send_email):129 date = timezone.now() - datetime.timedelta(days=22)130 self.users[1].last_posting_time = date131 self.users[1].save()132 self.users[2].last_posting_time = date133 self.users[2].save()134 users = AccountEmail.get_deactivated_users()135 self.assertEqual(users.count(), 0)136 self.daily_job.execute()137 self.assertEqual(send_email.call_count, 0)138 @mock.patch("emails.mailer.EmailMultiAlternatives.send")139 def test_get_users_deactivated_2_weeks_before(self, send_email):140 two_weeks_before = timezone.now() - datetime.timedelta(days=21)141 self.users[1].last_posting_time = two_weeks_before142 self.users[1].save()143 self.users[2].last_posting_time = two_weeks_before144 self.users[2].save()145 users = AccountEmail.get_deactivated_users()146 self.assertEqual(users.count(), 2)147 self.daily_job.execute()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!